Connecting Tech Pros Worldwide Forums | Help | Site Map

Is "delete this" valid in these case?

Yongming Wang
Guest
 
Posts: n/a
#1: Jul 22 '05
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 ~


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----

Gianni Mariani
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Is "delete this" valid in these case?


Yongming Wang wrote:[color=blue]
> 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?
>[/color]
....[color=blue]
>
> //case 1
> ~B()
> { m_pThis.FuncB();}
>
> //case 2
> ~B()
> {delete pThis;}[/color]

I suspect you mean "delete m_pThis" ?
[color=blue]
> }
>[/color]

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 ?

Duncus Colossus
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Is "delete this" valid in these case?


"Yongming Wang" <notify@126.com> wrote in message
news:m9m420596v5as3gm7sqb5vdu9lfubr1kiq@4ax.com...[color=blue]
> 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();}[/color]


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

[color=blue]
> //case 2
> ~B()
> {delete pThis;}[/color]

did you mean m_pThis?




Yongming Wang
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Is "delete this" valid in these case?


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.
Daniel T.
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Is "delete this" valid in these case?


In article <m9m420596v5as3gm7sqb5vdu9lfubr1kiq@4ax.com>,
Yongming Wang <notify@126.com> wrote:
[color=blue]
> 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;}
> }[/color]

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?
Daniel T.
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Is "delete this" valid in these case?


notify@126.com (Yongming Wang) wrote:
[color=blue]
> 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.[/color]

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?

[color=blue]
> Call "delete this" in the class member function is
> not a good design.[/color]

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