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:
~fullpage()
called onparentpage
instance. after that, parentpage isn't valid fullpage object anymore!~widget()
called, leaving qobject.~qobject()
called, deletes iconworking object (because of parent-child relationship)~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!)- crash
to make approach work, ~fullpage() have delete iconworking objects manually, instead of relying on qobject parent-child relationship.
Comments
Post a Comment