Is "delete this" valid in these case?

Y

Yongming Wang

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);
};
void FuncB()
{
delete this;
};
}

class B
{
public:
B(A* pThis): m_pThis(pThis){};

//case 1
~B()
{ m_pThis.FuncB();}

//case 2
~B()
{delete pThis;}
}


~ Let us linux ~
 
G

Gianni Mariani

Yongming said:
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?
....

//case 1
~B()
{ m_pThis.FuncB();}

//case 2
~B()
{delete pThis;}

I suspect you mean "delete m_pThis" ?

These (case 1 & 2 ) destructors do exactly the same thing and there is
nothing invalid about either of them.

Perhaps you could explain why you think they're different ?
 
D

Duncus Colossus

Yongming Wang said:
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);
};
void FuncB()
{
delete this;
};
}

class B
{
public:
B(A* pThis): m_pThis(pThis){};

//case 1
~B()
{ m_pThis.FuncB();}


Assuming that m_pThis is A*?
This should be m_pThis->FuncB();

//case 2
~B()
{delete pThis;}

did you mean m_pThis?
 
Y

Yongming Wang

Hi, sorry for my 2 mistakes.
It should be m_pThis->FuncB() and delete m_pThis.
But It is no effection with my problem.
I found the case 1 incorrect because when I do this, the CPU usage is
abnormally high to nearly 100%. But if I use case 2, all is OK.
Now I think the reason is FuncA called FuncB in it's body, so FuncB is
called multitimes. Call "delete this" in the class member function is
not a good design.

Thank you all.
 
D

Daniel T.

Yongming Wang said:
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);
};
void FuncB()
{
delete this;
};
}

class B
{
public:
B(A* pThis): m_pThis(pThis){};

//case 1
~B()
{ m_pThis.FuncB();}

//case 2
~B()
{delete pThis;}
}

It is always best to post compiled code that demonstrates the problem
you are having, or if the code won't compile, then show us the code and
the error messages you are getting. I took the liberty of doing this for
the above:


class A
{
public:
void FuncA();
void FuncB() { delete this; }
};

class B
{
A* m_pThis;
public:
B(A* pThis): m_pThis(pThis){}

// comment out one or the other d_tor
//case 1
~B() { m_pThis->FuncB();}

//case 2
//~B() {delete m_pThis;}
};

void A::FuncA()
{
B b(this);
}

int main()
{
A* a( new A );
a->FuncA();
}

In both cases, the program exits normally (at least on my system.) Why
is it that you think one of the d_tor's isn't valid? What is the message
you are getting from the compiler/run-time system?
 
D

Daniel T.

Hi, sorry for my 2 mistakes.
It should be m_pThis->FuncB() and delete m_pThis.
But It is no effection with my problem.
I found the case 1 incorrect because when I do this, the CPU usage is
abnormally high to nearly 100%. But if I use case 2, all is OK.
Now I think the reason is FuncA called FuncB in it's body, so FuncB is
called multitimes.

So FuncA would create a B object (passing a pointer to itself,) then
delete itself, then the B object would get destroyed which would call a
member function on a deleted object (with d_tor 1) or would delete the A
object a second time (with d_tor 2).

Either way, both cases are in error. Maybe you should post your code?

Call "delete this" in the class member function is
not a good design.

Not generally, but sometimes it is exactly what you need.
 

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

Forum statistics

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

Latest Threads

Top