Why segmentation fault occurs when using a window in a QSharedPointer?

I suspect the problem is that when you close the window, the data structure pointed to by pPreferencesWindow is deleted without the QSharedPointer 's knowledge. When the QSharedPointer itself is later destroyed, it double-deletes the window, and you get the segfault.

Up vote 2 down vote favorite share g+ share fb share tw.

I am developing a gui proram using Qt 4.7.4 (64 bit). I have tried to isolate the problem as follows: I have a window: class PreferencesWindow : public QMainWindow and in another class I initialize and show it as QSharedPointer pPreferencesWindow = QSharedPointer(new PreferencesWindow()); pPreferencesWindow->show(); it is all good, then I close the window either by pressing ESC or clicking the x button on the window. And then I call QApplication::quit(); to terminate the whole program.

It terminates but gives a segmentation fault just before terminating. The question here is why it terminates cleanly when I use regular pointer instead of QSharedPointer and how to use QSharedPointer properly in this case? Thanks c++ qt pointers segmentation-fault link|improve this question asked Feb 6 at 22:00destan3758 90% accept rate.

– JimR Feb 6 at 22:04 When I debug after calling QApplication::quit() it goes deep into qt library codes then I get lost so I can't say much about whether it goes out the scope or not. But I really need a managed pointer since somethimes I manually delete the window. But I haven't tried scopedPointer.

Anyways I am now curious about 'why' it is in this way. – destan Feb 6 at 22:26 You're destroying something twice. When the window is destroyed by quit, X etc, it's done.

There's no more reason to delete anything. I am probably wrong about the scoped pointer. – JimR Feb 6 at 22:29 @Ernest Friedman-Hill is right about his answer(2nd paragraph).

And I think the window gets deleted when QApplication::quit() is called not when closed by pressing 'x' – destan Feb 6 at 22:35.

I suspect the problem is that when you close the window, the data structure pointed to by pPreferencesWindow is deleted without the QSharedPointer's knowledge. When the QSharedPointer itself is later destroyed, it double-deletes the window, and you get the segfault. Basically, as with all shared pointer implementations, either everybody plays, or nobody does.

Since the Qt internals will never know you're using a smart pointer to manage the window, you can't use one. This is a blessing in disguise, however; it means that Qt itself takes possession of the pointer and agrees to manage it for you, so you don't need a smart pointer after all!

I am not an expert with Qt but my first thoughts would be that QMainWindow deletes itself upon destruction and the QSharedPointer object will also delete the object when it's destroyed (i.e. The object is deleted twice). If this is true you don't need to use the QSharedPointer at all.

EDIT: It looks like the QtWidget Qt::WA_DeleteOnClose flag will cause the behaviour I have described.

About your edit I can say that I don't set the flag and it is false by default afaik – destan Feb 6 at 22:27.

Sounds like you're missing a delete somewhere in the code.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions