c++ - Calling member->function(this); in Destructor causes Segfault -


here's offending code:

class fullpage : public qwidget {     q_object public:     explicit fullpage(const appdata* appdata, qwidget* parent = 0);     virtual void     addiconworking(iconworking* temp);     virtual void  removeiconworking(iconworking* temp);     ... }  class iconworking : public qlabel {     q_object public:     explicit iconworking(fullpage* parent = 0);     virtual ~iconworking();     ... }  iconworking::iconworking(fullpage* parent) : qlabel(parent) {     ...     parentpage = parent;     parentpage->addiconworking(this);     ... }  iconworking::~iconworking() {     parentpage->removeiconworking(this); //segfault     qmessagebox::information(0, "todo", "reminder message"); } 
  • the marked line segfaults before call. (breakpoint there hit, 1 inside function isn't)
  • parentpage never deleted, , has non-zero value @ point.
  • fullpage::add/removeiconworking(iconworking*) don't object itself; add/remove qlist. similar qt's native object system, except want guarantee iconworking's in there special processing.

what missing?


update:

i added test code per comments see if parentpage ever changes. doesn't. i'm using newly created variable assigned in constructor , checked in destructor.

the segfault message not specify address. it'd nice if did. checking pointers directly gives non-zero value, both original , added test, they're not null.

i discovered when add functionality, new segfault in unrelated location references same fullpage instance passed in arguments around program.

assuming ~iconworking() gets called when it's parentpage destroyed, parent-child relationship suggests:

when parentpage object destroyed, things happen in following order:

  1. ~fullpage() called on parentpage instance. after that, parentpage isn't valid fullpage object anymore!
  2. ~widget() called, leaving qobject.
  3. ~qobject() called, deletes iconworking object (because of parent-child relationship)
  4. ~iconworking() executed calls fullpage::removeiconworking() on parentpage, which, assume, accesses fullpage-specific members destroyed in step 1. (the object parentpage points @ point valid qobject, nothing else!)
  5. crash

to make approach work, ~fullpage() have delete iconworking objects manually, instead of relying on qobject parent-child relationship.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -