mfc >> Memory Leaks... ?

by Doug Harrison [MVP] » Mon, 01 Dec 2003 04:37:35 GMT

Maximus wrote:

>Hi,
>I'm having trouble with some memory leaks. I have a class named CDirectX
>that contains all my directdraw functions so I initialize the class in the
>begining of my program:
>CDirectX* cDirectX = new(CDirectX);
>
>After when my program leaves:
>#define SAFE_DELETE(p) { if(p) { delete(p); } }

The macro doesn't accomplish anything useful, as deleting a null pointer is
harmless.

>cDirectX->DD_Term(); // SAFE_RELEASE my directdraw object,surfaces...

What does SAFE_RELEASE do?

>SAFE_DELETE(cDirectX);
>
>Is there somthing wrong or can it be deep within my CDirectX class?
>Here is the output:
>c:\dxengine\mainfrm.cpp(16) : {49} normal block at 0x007C2F68, 132 bytes
>long. // Thats the line where I'm initializing CDirectX
> Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>Object dump complete.
>The program '[1284] DXEngine.exe: Native' has exited with code 0 (0x0).

MFC is know to dump leaks prematurely, instead of waiting for the CRT to
dump them after all static duration objects have been destroyed. When do you
call DD_Term? If it's from the dtor of some static duration object, in a DLL
that doesn't link to MFC but is used in an MFC app, this could be such a
case.

--
Doug Harrison
Microsoft MVP - Visual C++


mfc >> Memory Leaks... ?

by Maximus » Mon, 01 Dec 2003 07:10:44 GMT


Hi,
I'm having trouble with some memory leaks. I have a class named CDirectX
that contains all my directdraw functions so I initialize the class in the
begining of my program:
CDirectX* cDirectX = new(CDirectX);

After when my program leaves:
#define SAFE_DELETE(p) { if(p) { delete(p); } }
cDirectX->DD_Term(); // SAFE_RELEASE my directdraw object,surfaces...
SAFE_DELETE(cDirectX);

Is there somthing wrong or can it be deep within my CDirectX class?
Here is the output:
c:\dxengine\mainfrm.cpp(16) : {49} normal block at 0x007C2F68, 132 bytes
long. // Thats the line where I'm initializing CDirectX
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Object dump complete.
The program '[1284] DXEngine.exe: Native' has exited with code 0 (0x0).

Thanks for any help,
Max.








mfc >> Memory Leaks... ?

by Jonathan Wood » Tue, 02 Dec 2003 05:53:32 GMT

The dumped data is just zeros so that is no help at all. You really need to
look hard at your code.

From the code you've listed, it makes me wonder if SAFE_DELETE should be
defined as:

#define SAFE_DELETE(p) { if (p) { delete p; p = 0; }

But, for that matter, I'd definitely change your macro to a member function
with a name like Empty() and be sure that you call Empty from your
destructor. Other than that, it's impossible to know where you code is
calling SAFE_DELETE or what else is may be doing.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com







Memory Leaks... ?

by Stephen Kellett » Tue, 02 Dec 2003 07:34:43 GMT

>#define SAFE_DELETE(p) { if (p) { delete p; p = 0; }

There is no need for the if (p), it is valid to call delete on a NULL
pointer.
Be sure to always NULL a deleted pointer after deleting the object -
saves a lot of trouble when debugging.

Stephen
--
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk
RSI Information: http://www.objmedia.demon.co.uk/rsi.html


Memory Leaks... ?

by Jonathan Wood » Tue, 02 Dec 2003 10:02:33 GMT

It's not clear if you meant this for me but I'm the one you replied to.


Either way causes no harm.


As did the code snippet you quoted.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com




Similar Threads

1. How to detect memory leak and detect who causes a memory leak

2. Memory leak even after deleting memory pointers from vector

3. Memory leak indiaction in .NET app with C++ interop dll's

We are working on a .NET program which uses several dlls, both pure c# DLL'S 
and dll's written in C++ interop and in addition 3 party SW from several 
vendors.
The managed C++ dll are wrappers for interfacing native C++ code which we 
have link in as static libraries.
When we exit the program we get very many indications on memroy leaks dumped 
by the function _CrtDumpMemoryLeaks. After debugging a bit, the problem is 
that at the time _CrtDumpMemoryLeaks is called (destructor of afxState class 
inside MFC 8.0 dll), several of the managed C++ dll's are not unloaded, thus 
their static/global C++ objects destructors have not been called. 
Since we use system multithreaded dll's they share the same heap. 
Is there a way to be sure that _CrtDumpMemoryLeaks are called after all 
destructors have been called for all dll's? On stop the _CrtDumpMemoryLeaks 
called from the destructor of afxState?


4. Memory Leak in C++ Excel RTD (same as in VCRTDServer)

5. Memory leak hosting ATL ActiveX control in C# App

6. Memory Leak in C# Windows Service

7. System.String memory leaks C# application

tHi,
I have application with memory leaks and I used .Net memory profiler. It 
showed that every time I use string, gc does not clear that string and at the 
end I have many instances and bytes allocated for strings, many trnsaction.
why heppend System.String memory leaks?

8. Memory Leak in C# Windows Service - Microsoft .NET Framework