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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:50:47+00:00 2026-05-17T02:50:47+00:00

Why can I not do this? boost::shared_ptr<QueuList> next; void QueuList::SetNextPtr(QueuList* Next) { boost::mutex mtx;

  • 0

Why can I not do this?

boost::shared_ptr<QueuList> next;

void QueuList::SetNextPtr(QueuList* Next)
{
    boost::mutex mtx;

    boost::mutex::scoped_lock lock(mtx);
    {// scope of lock
        //if (next == NULL)  // is this needed on a shared_ptr??
        next = Next;  // Why can I not assign a raw ptr to a shared_ptr????
    }

}

How should I do it instead??

EDIT: Calling this method when the next variable is assigned properly, it still causes an error when the QueuList object is destroyed for some reason. I get a debug assertion. The destructor of the object does nothing in particular. It only crashes when I call this function:

    QueuList li;
    QueuList lis;

    li.SetNextPtr(&lis);

When main goes out of scope, I get a debug assertion… Any ideas??

  • 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-17T02:50:47+00:00Added an answer on May 17, 2026 at 2:50 am

    Putting a pointer inside a shared_ptr transfers ownership of the pointer to the shared_ptr, so the shared_ptr is responsible for deleting it. This is conceptually an important operation, so the designers of shared_ptr didn’t want it to just happen as part of a normal-looking assignment. For example, they wanted to prevent code like:

    some_shared_ptr = some_other_smart_pointer.get();
    

    which looks fairly innocuous, but would mean that both smart pointers thought they had responsibility for cleaning up the pointer, and would likely double-delete the pointer or something similar.

    This is what’s happening with your debug assertion. Calling SetNextPtr(&lis) passes ownership of &lis to the shared_ptr, and “ownership” means that the shared_ptr will call delete on its pointee when the last copy of the shared_ptr goes out of scope. So you’re effectively deleting a local (stack) variable – lis – which corrupts the stack and causes the crash.

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

Sidebar

Related Questions

I am not using boost libraries. How can i do this using STL? class
So on my VS2010 I can compile code like : boost::shared_ptr<boost::thread> internal_thread; boost::packaged_task<void> internal_task_w(boost::bind(&thread_pool::internal_run,
Any reasons why this can not be standard behavior of free() ? multiple pointers
I suppose this is not so hard but I can not get it. For
foo\r\nbar.replace(/(foo).+/m, bar) Hello. I can not understand why this code does not replace foo
Can not parse json using this var res = eval('(' + response + ')');
I can not get to make this comparison in this simple code error ..
there's this interesting problem i can not solve myself. I will be very glad,
The error shows this code can not rollback: class AddCountToTag < ActiveRecord::Migration def change
I am not professional programmer so i can not be sure about this.How many

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.