returning to deleted classes

Discussion in 'C++' started by Till Crueger, Jul 30, 2004.

  1. Till Crueger

    Till Crueger Guest

    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
     
    Till Crueger, Jul 30, 2004
    #1
    1. Advertising

  2. Till Crueger

    Till Crueger Guest

    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
     
    Till Crueger, Jul 30, 2004
    #2
    1. Advertising

  3. 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.
     
    Gianni Mariani, Jul 30, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lhak
    Replies:
    0
    Views:
    523
  2. David

    deleted ASPNET account by accident

    David, Aug 6, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,669
    GaneshMali
    Nov 18, 2008
  3. Steven Green

    Pointer to classes when deleted

    Steven Green, Dec 26, 2003, in forum: C++
    Replies:
    3
    Views:
    306
    Steven Green
    Dec 26, 2003
  4. Replies:
    11
    Views:
    670
    Christos Georgiou
    May 2, 2006
  5. Chumley the Walrus
    Replies:
    2
    Views:
    259
    Tom Gosselin
    Aug 10, 2004
Loading...

Share This Page