473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

delete this, Arrrrrgh!!


In a job interview I was asked about the statement below:

delete this;

Naturally I was horrified, yet they claimed they had used it.
Personally I'm pretty damn sure I could never justify this. So
is this a case of Lord Bryon, "Mad, bad, and dangerous to know",
or do you think you could ever justify using it?

JB
Nov 22 '05 #1
10 1439
"n2xssvv g02gfr12930" <n2*****************@ntlworld.com> wrote in message
news:Px*******************@newsfe4-gui.ntli.net...

In a job interview I was asked about the statement below:

delete this;

Naturally I was horrified, yet they claimed they had used it.
Personally I'm pretty damn sure I could never justify this. So
is this a case of Lord Bryon, "Mad, bad, and dangerous to know",
or do you think you could ever justify using it?

JB


Perhaps for some factory object. Or an object that only intended to be a
temporary or something.

Actually, if an object is on some list of pointers and it determines to
delete itself from the list, perhaps it could remove itself from the list
then delete itself.

I'm not sure if I'd ever do that though because it would throw off the list
iterators.

Hard to say. I'm sure somewhere someone has come up with a good reason to
use it, whether we agree with them or not.
Nov 22 '05 #2
* n2xssvv g02gfr12930:

In a job interview I was asked about the statement below:

delete this;

Naturally I was horrified, yet they claimed they had used it.
Personally I'm pretty damn sure I could never justify this. So
is this a case of Lord Bryon, "Mad, bad, and dangerous to know",
or do you think you could ever justify using it?


It's common and practically necessary for e.g. objects that represent
windows in a graphical user interface. A different design choice is to
allow such objects to have a nullstate. That leads to an awful lot of
nullstate checking and in general a non-OO system architecture.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #3
* /Gogineni/:
http://www.progsoc.uts.edu.au/lists/.../msg00010.html
Referring to a lot of dis-information by
* Anthony Langsworth, Mon, 2 Oct 2000:
Generally speaking, I'd recommend against using "delete this" in code. The
main problem is that the object can only be created via "new".
Which is not a problem.

If the object is declared "auto" (i.e. not a pointer or reference) and "delete
this" is called it will fail because "this" was not allocated with new and the
program will crash.
Which is not true: it's Undefined Behavior and there is no crash
guarantee.

If you really want to use "delete this" in your own code, make the destructor
protected,
Generally good advice.

preventing other classes from deleting the object and causing a compiler error if
an auto variable of this class is ever declared.

If you want to use in it a constructor, I'd recommend against it for the above reason
Which is meaningless or just false.

and also because the calling function has no way of knowing whether the object
encountered an error - calling one of the object's functions will result in a crash.
Which is false.

The object fails to instantiate itself will have to modify some form of global
variable such as errno, SetLastError(), IErrorInfo, etc, which, although acceptable,
is hardly ideal.
Which is an argument based on a false premise.

A much better way to report an error in an object's constructor is to use a static
"factory" function to encapsulate the construct. E.g.
static Dog* Dog::CreateDog(int legs) which can return a NULL pointer on an error.
Which is UnGood advice; generally an exception is preferable.

Alternatively, the constructor could throw an exception. Don't worry about memory
leaks, ANSI-compliant compilers should automatically deallocate the object with
calling the constructor [presumably he means: destructor] if a newed object throws
an exception in its constructor.
Which is false. Memory is reclaimed, the destructor isn't (and should
not be) called.

Check your compiler doco for this. The only situation I've seen "delete this" where I consider it an acceptable use
is the the thread or window classes from MFC.
MFC is hardly an example of good OO design; it's often used as a
counter-example.

The intention with these classes is to instantiate them, remove all references to them
and let the objects manages their own lifespans.
Also a good idea to check for null after any member function
that could cause the object to be destroyed.

I haven't got ARM (or a similar tome) handly but I suspect that modifying "this" in a
member funcion is illegal.
It is.

Either way, changing "this" in a member function will not affect the value of the
pointer the function was called through. E.g. void zero_ptr(char* s)
{
// This line only changes the local version of s.
s = NULL;
} ... char *s = "Hello, world!";
Using the deprecated conversion to char* (instead of char const*) is
abolutely not an example to follow.

zero_ptr(s);
printf(s); // Shows "Hello, world!"

Anthony Langsworth
Software Developer
Computing Edge


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #5
Alf P. Steinbach wrote:
* n2xssvv g02gfr12930:
In a job interview I was asked about the statement below:

delete this;

Naturally I was horrified, yet they claimed they had used it.
Personally I'm pretty damn sure I could never justify this. So
is this a case of Lord Bryon, "Mad, bad, and dangerous to know",
or do you think you could ever justify using it?

It's common and practically necessary for e.g. objects that represent
windows in a graphical user interface. A different design choice is to
allow such objects to have a nullstate. That leads to an awful lot of
nullstate checking and in general a non-OO system architecture.

I'm still not happy destroying an object this way. Why not use a
static function passing the pointer and waiting for a member flag to
signal that the class can be destroyed safely then exiting without ever
returning? Then what about the implications for descendant classes? The
knots that could be tied by misuse due to misunderstanding or sheer
incompetence don't bear thinking about.

JB
Nov 23 '05 #6
* n2xssvv g02gfr12930:
Alf P. Steinbach wrote:
* n2xssvv g02gfr12930:
In a job interview I was asked about the statement below:

delete this;

Naturally I was horrified, yet they claimed they had used it.
Personally I'm pretty damn sure I could never justify this. So
is this a case of Lord Bryon, "Mad, bad, and dangerous to know",
or do you think you could ever justify using it?

It's common and practically necessary for e.g. objects that represent
windows in a graphical user interface. A different design choice is to
allow such objects to have a nullstate. That leads to an awful lot of
nullstate checking and in general a non-OO system architecture.

I'm still not happy destroying an object this way. Why not use a
static function passing the pointer and waiting for a member flag to
signal that the class can be destroyed safely then exiting without ever
returning?


Then you have one thread or at least fiber per object. We can disregard
that standard C++ doesn't support threads, because they're part of
practical C++ usage. But you really don't want one thread per object.

Then what about the implications for descendant classes?
The main implication is that derived classes ideally should ensure
dynamic allocation, e.g. by each descendant class declaring a protected
destructor, but often one just relies on the client code programmer's
good sense -- and of course that incorrect usage will be caught by
even the most cursory testing, such as trying to run the program.

The
knots that could be tied by misuse due to misunderstanding or sheer
incompetence don't bear thinking about.


That's a general problem in software development, yes, but isn't more of
a problem here than elsewhere, as far as I can see.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 23 '05 #7
On 2005-11-22 02:12:47 -0500, n2xssvv g02gfr12930
<n2*****************@ntlworld.com> said:

In a job interview I was asked about the statement below:

delete this;

Naturally I was horrified, yet they claimed they had used it.
Personally I'm pretty damn sure I could never justify this. So
is this a case of Lord Bryon, "Mad, bad, and dangerous to know",
or do you think you could ever justify using it?


Imagine a reference counted class that automatically deletes itself
when all of it's clients drop their references:

class Refcounted
{
int reference_count;
protected:
virtual ~Refcounted() { assert(reference_count <= 0); }
public:
Refcounted() : reference_count(0) {}
Refcounted(Refcounted const &) : reference_count(0) {}

void add_reference() { ++reference_count; }
void drop_reference() { if(--reference_count <= 0) delete this; }
};
--
Clark S. Cox, III
cl*******@gmail.com

Nov 23 '05 #8

Clark S. Cox III wrote:
On 2005-11-22 02:12:47 -0500, n2xssvv g02gfr12930
<n2*****************@ntlworld.com> said:

In a job interview I was asked about the statement below:

delete this;

Naturally I was horrified, yet they claimed they had used it.
Personally I'm pretty damn sure I could never justify this. So
is this a case of Lord Bryon, "Mad, bad, and dangerous to know",
or do you think you could ever justify using it?
Imagine a reference counted class that automatically deletes itself
when all of it's clients drop their references:

class Refcounted
{
int reference_count;
protected:
virtual ~Refcounted() { assert(reference_count <= 0); }


"assert ( reference_count < 0 )" or "reference_count (1)"
public:
Refcounted() : reference_count(0) {}
Refcounted(Refcounted const &) : reference_count(0) {}

void add_reference() { ++reference_count; }
void drop_reference() { if(--reference_count <= 0) delete this; }
same here.
};
--
Clark S. Cox, III
cl*******@gmail.com


Nov 23 '05 #9
On Tue, 22 Nov 2005 04:59:04 -0500, Clark S. Cox III
<cl*******@gmail.com> wrote:
On 2005-11-22 02:12:47 -0500, n2xssvv g02gfr12930
<n2*****************@ntlworld.com> said:

In a job interview I was asked about the statement below:

delete this;

Naturally I was horrified, yet they claimed they had used it.
Personally I'm pretty damn sure I could never justify this. So
is this a case of Lord Bryon, "Mad, bad, and dangerous to know",
or do you think you could ever justify using it?


Imagine a reference counted class that automatically deletes itself
when all of it's clients drop their references:

class Refcounted
{
int reference_count;
protected:
virtual ~Refcounted() { assert(reference_count <= 0); }
public:
Refcounted() : reference_count(0) {}
Refcounted(Refcounted const &) : reference_count(0) {}

void add_reference() { ++reference_count; }
void drop_reference() { if(--reference_count <= 0) delete this; }
};


Is it safe to "delete this;" from any method, as long as you don't
access any more class members? I imagine it just deletes itself, gets
to the end of the method and returns safely, right?
Nov 23 '05 #10
Hi,

I delete my threads that way.

This so I don't have to think about some fancy thing that
determines that my thread is done stopping. At first I deleted
the thread after calling stop, but that meant that I deleted it's
inner guts before he could finish calling all it's close statments.
(now the deleting and the closing happens in the same thread,
and it's easier to think about the flow)

Nov 23 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alexander Stippler | last post by:
Hi, I have some question about the usage of delete. I have some reference counted classes, all derived from SharedObject. It's destructor looks like this: ~SharedObject() { if...
10
by: solosnake | last post by:
Whilst browsing Flipcode I noticed this code: class CConsole: public I_TextOutput { public: ... void Release() { delete this; } ... };
5
by: Yongming Wang | last post by:
FuncA is called to delete itself with B's help. In vc 7.0, case 1 seems invalid, but case 2 is valid. Could anyone explain for me? class A { public: void FuncA() { B b(this);
16
by: Martin Vorbrodt | last post by:
some butthole asked me an interview question: can you delete "this" pointer in a member function of a class, like this: class C { public: void foo() { delete this; } }; my answer was......
31
by: Anamika | last post by:
Hello friends.... can anyone tell me what will happen when we do..."delete this"...
4
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Just interested to learn how C++ implements delete this statement internally. From logical point of view, I can not imagine how to implement a component which destroy itself. ...
1
by: MorgyTheMole | last post by:
I have a base class: class A { protected: static void* operator new (size_t size); void operator delete (void *p); public: void Delete(); }
5
by: WaterWalk | last post by:
Hello. The question about "deleting this inside the class's member function" is discussed many times in this group and in the "C++ FAQs". But I still have two more questions. 1. Take the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.