Nisum Technologies Interview Question
1)The this object must have been allocated via new not by new[] nor by placement new nor by a local object on the stack nor by a global nor by a member of another object. It has to have been allocated by plain, ordinary new.
2) The member function that contains delete this; must be the last member function that is invoked on the this object.
3) The remainder of the member function after the delete this; line must not touch any piece of the this object, including calling any other member functions or touching any data members.
4) No other piece of code should even examine the this pointer itself after the delete this; line. No one may examine it, compare it with another pointer, compare it with NULL, print it, cast it, do anything with it.
5) Make sure no one else does a delete on the object. For example, if the object is still being held by an auto_ptr (which would be a good thing!), the release() member function must be called on the auto_ptr; otherwise the auto_ptr will delete the object again, which would be a disaster. For example:
#include <iostream>
using namespace std;
class Fred {
public:
Fred();
virtual ~Fred();
virtual void discard() throw();
};
void Fred::discard() throw()
{ delete this; }
1. Any object which has delete this; in dtor (i.e. objects created by new, by new[], on stack, global objects, static objects) will overfill stack by calling dtor()->dcot()->... and as result it will crash programm. Look inside delete statement or just try to create object on stack with @delete this@ in dtor.
destructor will go into infinate loop....
- sekhar740 November 29, 2010