By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 1,646 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

returning to deleted classes

P: n/a
Hi,
I was wondering about a problem, that might occur when you delete a class
in a method which was called by the deleted class. This is not actually a
problem I ran accross (I would probably restructure if possible). It is
rather one of those "What if" Questions.

Here is some code to explain what I mean:

class B;

class A {
private B* b;

public void registerB(B* _b) {
b=_b;
}

public void doStuff(void) {
// during this call the class will be destroyed
b->doStuff();
}
}

class B {
public A* a;

public B(A* _a) {
a=_a;
}

public void doStuff() {
delete a;
// when we return there will be no A to return to
}
}

int main(void) {
A* a = new A();
B* b = new B(a);
a->registerB(b);
a->doStuff();
return 0;
}

Is this undefined behaviour or are there any safety measures in the
language, like returning to the last existing caller or anything?

Till

--
Please add "Salt and Peper" to the subject line to bypass my spam filter

Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Fri, 30 Jul 2004 17:01:58 +0200, Till Crueger wrote:
Hi,
I was wondering about a problem, that might occur when you delete a class
in a method which was called by the deleted class. This is not actually a
problem I ran accross (I would probably restructure if possible). It is
rather one of those "What if" Questions.

Here is some code to explain what I mean:

class B;

class A {
private B* b;

public void registerB(B* _b) {
b=_b;
}

public void doStuff(void) {
// during this call the class will be destroyed
b->doStuff();
}
}

class B {
public A* a;

public B(A* _a) {
a=_a;
}

public void doStuff() {
delete a;
// when we return there will be no A to return to
}
}

int main(void) {
A* a = new A();
B* b = new B(a);
a->registerB(b);
a->doStuff();
return 0;
}

Is this undefined behaviour or are there any safety measures in the
language, like returning to the last existing caller or anything?

Till


whoops, I just noticed I messed up the syntax a bit. I hope you can get
the idea from what I wrote, if not then I'll repost correct code.

--
Please add "Salt and Peper" to the subject line to bypass my spam filter

Jul 22 '05 #2

P: n/a
Till Crueger wrote:
Hi,
I was wondering about a problem, that might occur when you delete a class
in a method which was called by the deleted class. This is not actually a
problem I ran accross (I would probably restructure if possible). It is
rather one of those "What if" Questions.

Here is some code to explain what I mean:

class B;

class A {
private B* b;

public void registerB(B* _b) {
b=_b;
}

public void doStuff(void) {
// during this call the class will be destroyed
b->doStuff();
}
}

class B {
public A* a;

public B(A* _a) {
a=_a;
}

public void doStuff() {
delete a;
// when we return there will be no A to return to
}
}

int main(void) {
A* a = new A();
B* b = new B(a);
a->registerB(b);
a->doStuff();
return 0;
}

Is this undefined behaviour or are there any safety measures in the
language, like returning to the last existing caller or anything?


It does not seem like you're using the deleted pointer's address hence
it is not undefined. As long as after calling b->doStuff you NEVER use
the a address, you're OK.

I would probably do this
public void doStuff(void) {
// during this call the class will be destroyed
b->doStuff();
// this IS DELETED - don't use this !
}

public void doStuff() {
delete a;
// when we return there will be no A to return to
a = 0;
}

The comment "// when we return there ..." is somewhat misstating the
event. You don't return to an object, you return to a method which has
"this" pointing to an object, as long as after control returns to a
method with a deleted this pointer, the object pointed to by this is
never accessed, there is no undefined behaviour.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.