http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52339

             Bug #: 52339
           Summary: using delete ptr1->ptr2 where ptr2 destructor deletes
                    a const ptr1 fails
    Classification: Unclassified
           Product: gcc
           Version: 4.6.2
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: romb...@hotmail.co.uk


The following code crashes and triggers the valgrind error with gcc 4.4 and
4.6. With clang it works just fine. And it also works fine if you remove
"const" on line 15 

*******
struct B;
struct A {
  B* b;
};

struct B {
  A* a;
  B() : a(new A{this}) {}
  ~B() { delete a; }
};

int main() {
  B* b = new B;
  // A* a = b->a;
  const A* a = b->a;
  delete a->b;
}
******

I have no idea if is it undefined behaviour to delete a->b if b deletes a.
Expressions should be evaluated only once, but for some reason this line:
  delete a->b;
is basically acting as if was written like:
  a->b->~B();
  operator delete(a->b); // a is already deleted

Reply via email to