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 4040968
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:49:14+00:00 2026-05-20T12:49:14+00:00

i am new to boost::shared_ptr using it the first time now. I have a

  • 0

i am new to boost::shared_ptr using it the first time now. I have a std::vector containing boost::shared_ptr which i “filled” with objects created from a custom class.

In Code: std::vector<boost::shared_ptr<Foo>> data;

The vector is created inside a function on the stack. I want to access some elements inside my vector using vector.at(k). The retured shared_ptr will be send to another thread, lets call him T1. My vector runs out of scope while T1 is still processing the shared_ptr.

In Code:


void myFunction(){

   //lets get my object
   boost::shared_ptr<Foo> obj = data.at(k);

   //emit it to Thread T1 (i am using QT)
   emit sendObjToThread1(obj);

}

I previously thought this would give no problem, but since my program is acting very strange, i am about to changing my mind. 🙂 To clarify this i am asking you on this platform now. 🙂

Is it right, that my object will be destroyed if my vector runs out of scope? If yes, how can i manage to get a deep copy of the shared_ptr (but not the object it holds). Just to have mentioned it: My application is based on QT.

Thx for reading.

  • 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-20T12:49:15+00:00Added an answer on May 20, 2026 at 12:49 pm

    No. It returns a reference.

    boost::shared_ptr<Foo> obj = data.at(k);
    

    But in this statement you are making a copy by creating obj.

    boost::shared_ptr<Foo>& obj = data.at(k);
                         ^^^
    

    Now there are no copies made.

    Edit:

    What about this line?

    sendObjToThread1(obj);
    

    Are you passing be reference or value? If you pass by reference to another thread then this is a problem as the local copy of obj is about to die (as soon as the function returns). So you must pass obj by value to make sure the other thread has its own copy and increments the shared_pointer counter appropriately.

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

Sidebar

Related Questions

I have the following code: #include <iostream> #include boost/shared_ptr.hpp using boost::shared_ptr; class Base {
Consider the following code. using boost::shared_ptr; struct B; struct A{ ~A() { std::cout <<
I have a C++ RAII class for managing Win32 HANDLEs using boost::shared_ptr<> that looks
I am new to boost python. I have to first init a cpp class
New to javascript/jquery and having a hard time with using this or $(this) to
New to both Ruby and Rails but I'm book educated by now (which apparently
Is the following code thread safe when using boost shared_ptr. Thanks! class CResource {
We've pretty much moved over to using boost::shared_ptr in all of our code, however
I have this example code: #include template<class T> class Class { public: typedef boost::shared_ptr<Class<T>
I am creating a new boost::thread using boost::bind , and storing it in a

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.