Mem leak
-
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.
-
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.
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
-
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.
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.
-
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
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] -
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]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
-
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.
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; }
-
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; }
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]
-
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]
actually I think it is correct but on eof the memory analysis tools complains still the same amount of leak. HENCE the question