Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7579237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:37:38+00:00 2026-05-30T17:37:38+00:00

This question always troubles me especially when I’m programming with Qt. Since Qt uses

  • 0

This question always troubles me especially when I’m programming with Qt.
Since Qt uses Object Ownership trees, passing a pointer e.g. via myBoostSharedPtr.get() can implicitly transfer ownership.
Now consider the case where some Qt Object gets destroyed and the whole Object Tree is destroyed but the smart-pointer is still alive e.g. as member of a different class.
What happens if the smart-pointer gets deleted afterwards ?
Double deletion with all the nasty consequences ?
Do some smart-pointer implementations prevent this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:37:39+00:00Added an answer on May 30, 2026 at 5:37 pm

    I’m so tempted to do a rant on the weaknesses of Qt’s memory model where a lot of the API still accepts raw pointers with the expectation that the client allocates it while the QObject that accepted the pointer deletes it.

    The answer to your question is undefined behavior. shared_ptr has no mechanism to detect if the pointer is deleted by something other than shared_ptr itself, so it will generally try to free the pointer a second time (calling delete on a dangling pointer). If you want to use shared_ptr, you have to stick with shared_ptr as the sole memory manager. This is true even for Qt’s own QSharedPointer.

    What I normally did to try to get my code reasonably exception-safe when using something like Qt was to use the now deprecated auto_ptr (unique_ptr replaces it and is much safer if you have C++11 available). It’s the only place where I was ever tempted to use auto_ptr before, as it provides a release method.

    unique_ptr<QListWidget> widget(new QListWidget(...));
    // do stuff with the widget to set it up for your GUI
    some_layout.addWidget(widget.release()); // <-- release ownership so that 
                                             // the layout now becomes responsible 
                                             // for memory management
    // ^^ auto_ptr works above if we don't have C++11
    

    If you need to keep a persistent pointer to your object after it is already being memory-managed by Qt (ex: a pointer to your widget after you inserted it into a layout), just use a regular pointer. There’s not really much better you can do since Qt is now the memory manager for that object.

    However, you can detect when the object is destroyed (and therefore when the pointer is invalidated) through the QObject::destroyed signal.

    If you want to get really sophisticated, you could build a shared pointer type which only stores subclasses of QObject. Since QObject provides a destroyed signal, this kind of custom smart pointer could detect when QObject is destroyed through the destroy signal and avoid trying to delete the object a second time. However, it might get hairy relying on this signal in multithreaded code, and if you do implement a shared pointer, it can become quite a burden dealing with atomic reference counting, capturing a deletion function at the site the pointer is constructed (to avoid module boundary new/delete mismatches), etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is a question that when programming I always wonder: What to use when
I always had this question: When i dont mind the exact floating number Which
This question could be a can of worms, but I've always wondered if it
This question has been bugging me for some time. I always picture launching my
When I asked this question I got almost always a definite yes you should
This is a question that I've always wanted to know the answer, but never
This is likely a stupid question but I always find myself wondering which is
This is a question that's been nagging me for some time. I always thought
This seems like a pretty softball question, but I always have a hard time
This question has always troubled me. On Linux, when asked for a password, if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.