Archived

This topic is now archived and is closed to further replies.

the_grip

memset or ZeroMemory

Recommended Posts

Does memset or ZeroMemory (when used on a pointer) allocate new memory space? i just added a function into my program that creates and returns a DD7 surface, and now i''m getting memory leaks. In the process, i pass a pointer to a DDSURFACEDESC2 and initialize it via a macro (typical) through memset. Something like this:
  
#define INIT_DDSTRUCT(ddstruct) {memset(ddstruct, sizeof(DDSURFACEDESC2)); ddstruct->dwSize = sizeof(DDSURFACEDESC2);}

LPDIRECTDRAW7 foo(DDSURFACEDESC2 *ddsd, ...)
{
     INIT_DDSTRUCT(ddsd);  //<-does this allocate new memory since it is a pointer?
     ...
}

 
A bit sloppy, but i''m at work so i can''t remember the exact coding. i don''t know if it is this or if a DD7 surface is not getting released somewhere, but i think they all are (the function i created creates and returns a DD7 surface within the scope of itself, but the return value is caught by another DD7 surface that is released at the shutdown point of the program). Any help out there? Thanks! "You call him Dr. Grip, doll!"

Share this post


Link to post
Share on other sites
memset() and zeromemory() do not allocate any memory - why would they need to?

it is probably a unreleased surface or additional reference.
You can use the return value of Release() to get the number of remaining references to each of the COM objects.

Share this post


Link to post
Share on other sites
Release()is defined as returning type ULONG - the value being the value of the reference counter. So in your cleanup routine you could just check this value, and post an error or something...

  
#define RELEASE(x) if(x){ULONG rc = x->Release();ASSERT(rc == 0);x = 0;}

void Cleanup()
{
RELEASE(lpSurfaceXXX);
// etc.

RELEASE(lpDev);
RELEASE(lpD3D);
}


If the reference count is non-zero, this will throw up an assert.

Share this post


Link to post
Share on other sites
memset and ZeroMemory are the same thing. The just take the pointer and set the next X amount of byte to the value (in the case of ZeroMemory, 0!).

They don''t allocate memory..

Hence..
char szText[10];
ZeroMemory(szText, 15);

Will over right the next five bytes AFTER szText.. which can cause major bugs.. So be careful!

Share this post


Link to post
Share on other sites
It''s not a good idea to use the return value of Release() for anything. Sometimes another object will have a reference to your object, which will be included in the return value (i.e. the return value is not always = to the number of times you called AddRef() - the number of times you call Release().)

If you want to know if there are still surfaces around when you exit (or whatever), install the debug versions of the DirectX libraries (it''s in the install program, you can choose if you want release or debug). Then, in the DirectX control panel applet, set the debug level of both DirectDraw and DirectX to 4. This will cause DirectX to print a whole bunch of debugging info to your debugger window.

Usually though, a memory leak will not be caused by DirectX (you won''t see it if there is, anyway, since it''s allocated by ddhelp.exe I think)


War Worlds - A 3D Real-Time Strategy game in development.

Share this post


Link to post
Share on other sites
Well, i found the leak. Something stupid... in a new function that defaults a clipper to the backbuffer surface, i used the clipper but never released it... doh! All fixed now. Something interesting... Release() returns a ULONG, which is a type of DWORD. i tried to output the value to the debug window on MSVC++ 6, but it doesn''t work. Interesting...

"You call him Dr. Grip, doll!"

Share this post


Link to post
Share on other sites