Memory Leaks

Started by
5 comments, last by leet bix 14 years, 11 months ago
I'm abit worried about the things im hearing about having memory leaks. Its said that if I dont properly release my textures, devices etc. then the memory becomes inaccesible and takes up memory. Does this mean that the memory will not clear itself even if I restart my computer and it will never go away? I've tried to look it up but i cant seem to find any info. Im getting abit worried now because my memory usage seems pretty high even when it's idle with no programs running. Im not sure what the memory usage readings were before i started programing directx and im not sure if it's just a normal reading for this computer so I was just wondering if by inaccesible they REALLY mean inaccesible.
Advertisement
All the memory is freed when the program terminates.
As WeetBix says, the operating system will free all the memory that that application was given once the application terminates, just be careful to release you objects, it's not that difficult, and if you have visual studio, it will tell you if you have memory leaks, how big the leak is, and at what addresses memory has been leaked.
Also, memory is a type olf volatile storage, this means that it requires electricity to store and constantly update it, once the power is cut everything on that storage device is lost so don't worry about memory being taken up even after a shutdown or restart.
In terms of actually releasing objects in directx, typically people use a common macro called SAFE_RELEASE, as a learner, you will typically only use it when your application shuts down or when loading and copying objects like Mesh's, sufaces and textures, this is what the macro looks like:


#define SAFE_RELEASE(pObject) { if (pObject) { (pObject)->Release(); (pObject)=NULL; } }


So if I had the following


LPDIRECT3DTEUXTURE9 pTexture;


and I wanted to release it, I would simply call this


SAFE_RELEASE(pTexture);


Now pTexture will not cause a memory leak unless it is used further in the application.
Quote:Original post by leet bix
As WeetBix says, the operating system will free all the memory that that application was given once the application terminates, just be careful to release you objects, it's not that difficult, and if you have visual studio, it will tell you if you have memory leaks, how big the leak is, and at what addresses memory has been leaked.
Also, memory is a type olf volatile storage, this means that it requires electricity to store and constantly update it, once the power is cut everything on that storage device is lost so don't worry about memory being taken up even after a shutdown or restart.
In terms of actually releasing objects in directx, typically people use a common macro called SAFE_RELEASE, as a learner, you will typically only use it when your application shuts down or when loading and copying objects like Mesh's, sufaces and textures, this is what the macro looks like:


#define SAFE_RELEASE(pObject) { if (pObject) { (pObject)->Release(); (pObject)=NULL; } }


So if I had the following


LPDIRECT3DTEUXTURE9 pTexture;


and I wanted to release it, I would simply call this


SAFE_RELEASE(pTexture);


Now pTexture will not cause a memory leak unless it is used further in the application.


Much better and easier to use a smart pointer for COM interfaces however, such as CComPtr that comes with ATL.
I'll try out that safe_release thing but right now im using
ptexture->Release();
Would there be any problem with that?

Also my release functions all occur at the end of my main program so if my program has a runtime error while debuging and i break the program does that mean that the everything wont be released?
Quote:Original post by eflatmajor
I'll try out that safe_release thing but right now im using
ptexture->Release();
Would there be any problem with that?

Also my release functions all occur at the end of my main program so if my program has a runtime error while debuging and i break the program does that mean that the everything wont be released?


Correct. If your program ends unexpectedly, you have no way of ensuring everything was released properly from your application.

Not that it really matters, since the OS always cleans up after applications - once a process ends all resources are reclaimed by the OS.
NextWar: The Quest for Earth available now for Windows Phone 7.
pTexture->Relase is exactly the same as SAFE_RELEASE(pTexture) as SAFE_RELEASE just calls the Release() function of the object passed to it.

It doesn't matter if you have unfreed resources and your program stops with a run time error as when your program is terminated, the chunck of memory allocated for your program by the OS will be 'given back' to the OS.

This topic is closed to new replies.

Advertisement