Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Mem leak

Mem leak

Scheduled Pinned Locked Moved C / C++ / MFC
performance
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tom groezer
    wrote on last edited by
    #1

    There is a class A which is defined like #include "stdafx.h" class A { public: A(); ~A(); }; static A a; A::A() { int * i = new int[10]; } A::~A() { } int _tmain(int argc, _TCHAR* argv[]) { return 0; } Is there a memory leak here; The only diff is here we create the object of the class A as static.

    N J 2 Replies Last reply
    0
    • T tom groezer

      There is a class A which is defined like #include "stdafx.h" class A { public: A(); ~A(); }; static A a; A::A() { int * i = new int[10]; } A::~A() { } int _tmain(int argc, _TCHAR* argv[]) { return 0; } Is there a memory leak here; The only diff is here we create the object of the class A as static.

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      tom groezer wrote:

      int * i = new int[10];

      tom groezer wrote:

      Is there a memory leak here; The only diff is here we create the object of the class A as static.

      This (*i) should be deleted right?

      Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

      CPalliniC 1 Reply Last reply
      0
      • T tom groezer

        There is a class A which is defined like #include "stdafx.h" class A { public: A(); ~A(); }; static A a; A::A() { int * i = new int[10]; } A::~A() { } int _tmain(int argc, _TCHAR* argv[]) { return 0; } Is there a memory leak here; The only diff is here we create the object of the class A as static.

        J Offline
        J Offline
        Jijo Raj
        wrote on last edited by
        #3

        tom groezer wrote:

        A::A() { int * i = new int[10]; }

        Even though the object is static, its constructor will be called and the memory allocation in constructor is not deleted else where. So its definitely a leak. Regards, Jijo.

        _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

        T 1 Reply Last reply
        0
        • N Nibu babu thomas

          tom groezer wrote:

          int * i = new int[10];

          tom groezer wrote:

          Is there a memory leak here; The only diff is here we create the object of the class A as static.

          This (*i) should be deleted right?

          Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          That is a memory leak by design. :-D

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          N 1 Reply Last reply
          0
          • CPalliniC CPallini

            That is a memory leak by design. :-D

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            N Offline
            N Offline
            Nibu babu thomas
            wrote on last edited by
            #5

            CPallini wrote:

            That is a memory leak by design.

            Thought that was a typo, ;) was just too obvious to miss out.

            Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

            1 Reply Last reply
            0
            • J Jijo Raj

              tom groezer wrote:

              A::A() { int * i = new int[10]; }

              Even though the object is static, its constructor will be called and the memory allocation in constructor is not deleted else where. So its definitely a leak. Regards, Jijo.

              _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

              T Offline
              T Offline
              tom groezer
              wrote on last edited by
              #6

              Yes just send that code to raise some interest. Now to fix that I did something like this below:-\ I added that delete statement in bold. I guess this should be perfect. Comments. #include "stdafx.h" class A { public: A(); ~A(); private: int* i; }; static A a; A::A() { int * i = new int[10]; } A::~A() { delete[] i; } int _tmain(int argc, _TCHAR* argv[]) { return 0; }

              T 1 Reply Last reply
              0
              • T tom groezer

                Yes just send that code to raise some interest. Now to fix that I did something like this below:-\ I added that delete statement in bold. I guess this should be perfect. Comments. #include "stdafx.h" class A { public: A(); ~A(); private: int* i; }; static A a; A::A() { int * i = new int[10]; } A::~A() { delete[] i; } int _tmain(int argc, _TCHAR* argv[]) { return 0; }

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #7

                tom groezer wrote:

                I guess this should be perfect. Comments.

                perfect. but a question anyway : why didn't you just test it with your debugger ? ps: also, please format your posts correctly using <pre></pre> tags when you post code snippets...

                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                T 1 Reply Last reply
                0
                • T toxcct

                  tom groezer wrote:

                  I guess this should be perfect. Comments.

                  perfect. but a question anyway : why didn't you just test it with your debugger ? ps: also, please format your posts correctly using <pre></pre> tags when you post code snippets...

                  [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                  T Offline
                  T Offline
                  tom groezer
                  wrote on last edited by
                  #8

                  actually I think it is correct but on eof the memory analysis tools complains still the same amount of leak. HENCE the question

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups