I keep running into memory leak problems.. well i''m certain that most if not all it''s due to forgeting to delete the class after it was allocated... well what i''m asking is is there a way so that a class can delete itself after it goes out of scope ??? something like
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
class foo
{
public:
~foo ();
};
foo::~foo ()
{delete this;}
int main ()
{
foo *t = new foo;
_CrtDumpMemoryLeaks();
return 0;
}
but i know that the code above doesnt work... i''m using the way that msdn show to dump memory leaks.. well while typing this question i just saw a problem.. since foo won''t go out of scope after the program finishes, could it be the problem that t was still allocated ???? how do i get around this problem.. thx a lot
How About: int main () { foo *t = new foo; delete t;
_CrtDumpMemoryLeaks(); return 0; }
All kidding aside, if you want your pointers to automagically be deallocated when they are no longer needed you should use some sort of "Smart Pointer".
a) The object has been allocated with new. b) The object is not used in any way in the remainder of the function.
However, it is a horrible mistake do to it in the destructor because
a) The destructor is called for all objects, regardless of whether they have been allocated with new, as automatic (local) variables or as static variables. b) The destructor is itself actually called when you do delete this; ...
As the AP (and Ranok) said, you want smart pointers here, not self-deleting objects. Try boost::shared_ptr.
Oh, and in C++, you cannot actually delete classes, only objects. If you want to actually delete classes, learn python (or some other language).
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
quote:Original post by Metal Typhoon Well my bad.. Objects.. not classes hehee.. So ok.. what you guys r saying is .i gota use smart pointers ?
Pretty much, yes.
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
quote:Original post by Doc Look for std::auto_ptr, it''s pretty simple and can probably do all you describe.
std::auto_ptr''s copy semantics make it unsuitable to most usages. In particular, it cannot be used with standard containers.
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
What about that smart pointer I wrote awhile back that makes use of reference counting? I had some trouble overloading the << to work properly but got it fixed now.
I wrote it for practice and just for the heck of it but didn''t find it too difficult overall.
--{You fight like a dairy farmer!}
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform.
You can accept analytics or continue with necessary cookies only.
Learn more