Memory leakage

Started by
5 comments, last by Zahlman 17 years, 4 months ago
Hello I want to use C++ for game programming even though I dont' see any real advantage vs a script language at my maturish level :) just for fun and and exercise However I dont want to have useless troubles In particualar I am scared of memory leakage My question is Should I use static objects only. may I assume that I dont take any risk ? Thanks in advance
Advertisement
If you're afraid of memory leaks, why not use C# instead?
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
You could use either the memory leak detection in Visual Studio or MMGR from Paul Nettle (paulnettle.com, choose Code, then MMGR) to detect memory leaks in your program.

I see no reason why you should be scared of memory leaks. Sure, your program might run a little less stable, but as soon as your program ends, the memory is freed. No reason to use only statics, etc.
The golden rule is that if you write new then you need to write delete somewhere. So if you declare a pointer and you have it allocate memory on the heap then (which you must write new for) then you have to delete it somewhere. As long as you delete it when you're done with it your okay. if you declare a pointer and it points to something that already exists then there is no need for an extra delete. But remember you must call delete on that memory once you're done with it, irregardless of which pointer is now the pointer that is pointing to that memory. Once you're done with that memory you must delete it.

Remember that you're not deleting pointers, you're deleting memory. so take this for example.

int * foo();int main(){   int * b = new int;  //created memory on the heap with new   *b = 5;   delete b;  //done with the memory, b still points to the memory               //call delete on b.   int * c = foo();   //call foo                      //after function return c now points to the memory                      //that you allocated on the heap.  you still have a                      //pointer pointing to it   delete c;          //now delete that memory.}int * foo(){   int * d = new int;  //create a pointer that poitns to memory on the heap   *d = 90;            //assign it 90   return d;           //return the value of d (which is the address)                       //d is destroy because it's out of scope.  oh no, you                       //didn't call delete but it's okay.}


I'm not sure how good that example is but see how you only call new twice and you delete twice, once for each new. and the delete doesn't have to be called on the same pointer that allocated the memory, but the delete must be called on that memory once your done with it. in my example the pointers aren't used at all, they're assigned but never used, they may even be optimized out by the compiler and you'd probably see a warning by the compiler but this was a demonstration.

My point is if you follow this rule, think it through you shouldn't have any problems. you only need to delete when you write new and remember your deleting the memory not the pointer so who knows where that memory is? call delete on that one. if there's two, call once and set the other to null. I think you're supposed to set all pointers to null once your completely done with them and deleting them if applicable.

man that's long winded, I meant to just write a line or two. This is probably everything you know too. well, at least it only stole thirty min of my life or so. heh heh.
Quote:Original post by AlbertoT
Hello

I want to use C++ for game programming even though I dont' see any real advantage vs a script language at my maturish level :) just for fun and and exercise
However I dont want to have useless troubles
In particualar I am scared of memory leakage
My question is
Should I use static objects only. may I assume that I dont take any risk ?

Thanks in advance


Simply make it a rule for yourself, to always use shared pointers/reference counted objects, that should keep you out of trouble 99% of the time
Using static memory is:
a) Wrong, it goes pretty much against any good coding style
b) Unneccessary in most cases
c) Very memory consuming, since you need to allocate all the memory at the start, even if that would mean you would be using only 10% of it all during runtime of your programming

If you don't want to be bothered with memory management, go with C# or any Garbage Collected language(Such as Java, all of .NET, etc.) or pick up a smart pointer class, such as boost::shared_ptr(Might have the name mistaken, haven't used boost in ages).

Toolmaker

In modern C++ usage, it's actually fairly unusual to have to write 'delete', because it's unusual to have to write 'new' - the memory management work has already been wrapped up for you with cleverly designed classes, at least to cover the most common cases. You should make sure you are familiar with the standard library, and look to Boost when the standard library seems inadequate.

This topic is closed to new replies.

Advertisement