returning to deleted classes

T

Till Crueger

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
 
T

Till Crueger

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.
 
G

Gianni Mariani

Till said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top