Is "delete this" valid in these case?

Discussion in 'C++' started by Yongming Wang, Feb 5, 2004.

  1. 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 ~
     
    Yongming Wang, Feb 5, 2004
    #1
    1. Advertisements

  2. 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 ?
     
    Gianni Mariani, Feb 5, 2004
    #2
    1. Advertisements


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

    did you mean m_pThis?
     
    Duncus Colossus, Feb 5, 2004
    #3
  4. 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.
     
    Yongming Wang, Feb 6, 2004
    #4
  5. Yongming Wang

    Daniel T. Guest

    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., Feb 6, 2004
    #5
  6. Yongming Wang

    Daniel T. Guest

    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?

    Not generally, but sometimes it is exactly what you need.
     
    Daniel T., Feb 6, 2004
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.