Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > "delete this" for object in Stack

Reply
Thread Tools

"delete this" for object in Stack

 
 
thomas
Guest
Posts: n/a
 
      10-15-2010
Hi,

------------
class A {
public:
~A(){}
void Release(){ delete this; }
};
-----------
A *a = new A();
a->Release(); --> Method I
A a2;
a2.Release(); --> Method II
-----------

a is created in Heap while a2 is in Stack.
When I call "Release()", thus "delete this;" to these two different
kinds of object, what will happen?
If "delete this" just involves calling the destructor, I suppose they
are equivalent.
Any suggestions? Thanks.

tom
 
Reply With Quote
 
 
 
 
Prasoon Saurav
Guest
Posts: n/a
 
      10-15-2010
On Oct 15, 9:22*am, thomas <(E-Mail Removed)> wrote:
> Hi,
>
> ------------
> class A {
> public:
> * * * ~A(){}
> * * * *void Release(){ delete this; }};
>
> -----------
> A *a = new A();
> a->Release(); * * * --> * *Method I
> A a2;
> a2.Release(); * * *--> * *Method II
> -----------
>
> a is created in Heap while a2 is in Stack.
> When I call "Release()", thus "delete this;" to these two different
> kinds of object, what will happen?
> If "delete this" just involves calling the destructor, I suppose they
> are equivalent.
> Any suggestions? Thanks.
>
> tom


Calling delete on an object for which memory has not been allocated
explicitly using *new* invokes Undefined Behaviour.

Undefined Behaviour means *anything* can happen. Don't write such code.
 
Reply With Quote
 
 
 
 
Prasoon Saurav
Guest
Posts: n/a
 
      10-15-2010
On Oct 15, 9:22*am, thomas <(E-Mail Removed)> wrote:
> Hi,
>
> ------------
> class A {
> public:
> * * * ~A(){}
> * * * *void Release(){ delete this; }};
>
> -----------
> A *a = new A();
> a->Release(); * * * --> * *Method I
> A a2;
> a2.Release(); * * *--> * *Method II
> -----------
>
> a is created in Heap while a2 is in Stack.
> When I call "Release()", thus "delete this;" to these two different
> kinds of object, what will happen?
> If "delete this" just involves calling the destructor, I suppose they
> are equivalent.
> Any suggestions? Thanks.
>
> tom


Calling delete on something,memory for which, has not been allocated
explicitly using *new* invokes Undefined Behaviour.
Undefined Behaviour means *anything* can happen. Don't write such
code.
 
Reply With Quote
 
werasm
Guest
Posts: n/a
 
      10-15-2010
On Oct 15, 6:22*am, thomas <(E-Mail Removed)> wrote:
> Hi,
>
> ------------
> class A {
> public:
> * * * ~A(){}
> * * * *void Release(){ delete this; }};
>
> -----------
> A *a = new A();
> a->Release(); * * * --> * *Method I
> A a2;
> a2.Release(); * * *--> * *Method II
> -----------
>
> a is created in Heap while a2 is in Stack.
> When I call "Release()", thus "delete this;" to these two different
> kinds of object, what will happen?
> If "delete this" just involves calling the destructor, I suppose they
> are equivalent.
> Any suggestions? Thanks.
>
> tom


Hi,

Make sure you read this FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-16.15

You cannot call delete for objects allocated on the stack (amongst
others - see FAQ). This means that you have to make sure your
objects are heap allocated. You could do this by making the
constructor private and having a static function that returns
a heap instance.

class A
{
public:
void Release(){ delete this; }
static std::auto_ptr<A> makeA()
{
return std::auto_ptr<A>( new A );
}
private:
A(){ }
};

I just use an auto pointer here because it makes
it clear that the caller takes ownership of the
pointer (assuming he calls the function that eventually
performs deallocation.

Kind regards,

Werner
 
Reply With Quote
 
Goran
Guest
Posts: n/a
 
      10-15-2010
On Oct 15, 6:22*am, thomas <(E-Mail Removed)> wrote:
> Hi,
>
> ------------
> class A {
> public:
> * * * ~A(){}
> * * * *void Release(){ delete this; }};
>
> -----------
> A *a = new A();
> a->Release(); * * * --> * *Method I
> A a2;
> a2.Release(); * * *--> * *Method II
> -----------
>
> a is created in Heap while a2 is in Stack.
> When I call "Release()", thus "delete this;" to these two different
> kinds of object, what will happen?
> If "delete this" just involves calling the destructor, I suppose they
> are equivalent.


Not at all. operator delete invokes the destrucctor and then frees
allocated memory. Just like operator new allocates memory, then calls
the constructor. That's a rather fundamental aspect of what they do.

> Any suggestions? Thanks.


Others already explained.

I'll add this: "delete this" is in general a bloody stupid idea with
rare situations where it's expedient. Your snippet is just a massive
bug.

C++ language, however, does allow you to design a type so that clients
have to use it on the heap, and that you have to call e.g. a Release
function (name is incidental). E.g.

class heap_only_please
{ // intentionally private:
heap_only_please() {...}
heap_only_please(const heap_only_please& ) {...};
~heap_only_please() {...}
heap_only_please(params) {...}
public:
static heap_only_please* create(params)
{
return new heap_only_please(params);
}
void Release() { delete this; }
// Want refcounting? Put it in Release!
};

Goran.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does std::stack::pop() not throw an exception if the stack is empty? Debajit Adhikary C++ 36 02-10-2011 08:54 PM
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
stack frame size on linux/solaris of a running application stack Surinder Singh C Programming 1 12-20-2007 01:16 PM
Why stack overflow with such a small stack? Kenneth McDonald Ruby 7 09-01-2007 04:21 AM
"stack level too deep"... because Threads keep their "starting" stack Sam Roberts Ruby 1 02-11-2005 04:25 AM



Advertisments