Skip to main content
gamedev.net
🔒 Locked

Class delete itself ??

Started by Metal Typhoon 10 replies 1.3k views
Original Post
Metal Typhoon
Metal Typhoon
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
Metal Typhoon
Ranok
Ranok
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".

Try these for starters:

STL's AutoPtr


Boost
Boost's Smart Pointers


[edited by - Ranok on January 30, 2004 7:03:47 PM]
---Ranok---
zaidgs
zaidgs
use vc.net,
garbage collected classes

btw, your way dont work,
i tested it this way
(thats an answer to the question of how
to work around the scope thing)


#define _CRTDBG_MAP_ALLOC
#include <iostream.h>
#include <stdlib.h>
#include <crtdbg.h>
#include <iostream.h>
class foo{
public:
~foo ();
};
foo::~foo (){delete this;}
void creat(){
foo *t= new foo;
}
int main (){
creat();
cout<<_CrtDumpMemoryLeaks()< return 0;
}



[edited by - zaidgs on January 30, 2004 7:42:49 PM]
Fruny
Fruny
A member function can do delete this; so long as

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)
"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
Metal Typhoon
Metal Typhoon
Well my bad.. Objects.. not classes hehee.. So ok.. what you guys r saying is .i gota use smart pointers ?
Metal Typhoon
Fruny
Fruny
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)
"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
antareus
antareus
Might I add that writing smart pointers isn''t as trivial as you''d think it is. (In my experience at least, your mileage may vary).
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Doc
Doc
Look for std::auto_ptr, it''s pretty simple and can probably do all you describe.
Fruny
Fruny
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)
"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
Greatwolf
Greatwolf
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!}
--{You fight like a dairy farmer!}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.