473,290 Members | 1,897 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,290 software developers and data experts.

returning to deleted classes

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
2 1134
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
3
by: kamran | last post by:
Hi, I have a web service that may return a very large amount of data. I want that data to return in chunks, like first return 10% of data than return the next 10% and so on, until all is...
4
by: Naren | last post by:
Hi , Is it correct to return reference of a member variable. because it can be like we have already deleted the object and still holding the reference of member variable. A *a = new A; int...
15
by: Joseph Geretz | last post by:
I'm a bit puzzled by the current recommendation not to send Datasets or Datatables between application tiers. http://support.microsoft.com/kb/306134 ...
17
by: Cralis | last post by:
I am trying to populate a ListView with a list of 'Models' of cars. I have a data object class for my models, which has a function, 'getListOfModels', which I want to retuyrn a <Listof models. ...
3
by: =?iso-8859-9?B?RGlu52F5IEFr5/ZyZW4=?= | last post by:
Following function void mdelr(int *ar1, int a, int b, int d ) { int i,j,tmp; int *temp; for (i=0; i<a; i++) { for(j=0; j<b; j++)
14
by: Mark | last post by:
Hi, I would like to check if my object has been deleted or not, but my program keeps crashing. Here's the simplified code (in infinite loop) delete tetromino; //if(tetromino==NULL)...
1
by: krishna81m | last post by:
In the following code, I am trying to return a char, a char* (a type of non-const without using new, what do we call this type of pointer?) and char* created using new operator. What I do not know at...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.