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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:43:53+00:00 2026-05-15T19:43:53+00:00

I guess I don’t fully understand how destructors work in C++. Here is the

  • 0

I guess I don’t fully understand how destructors work in C++. Here is the sample program I wrote to recreate the issue:

#include <iostream>
#include <memory>
#include <vector>

using namespace std;

struct Odp
{
    int id;

    Odp(int id)
    {
        this->id = id;
    }

    ~Odp()
    {
        cout << "Destructing Odp " << id << endl;
    }
};

typedef vector<shared_ptr<Odp>> OdpVec;

bool findOdpWithID(int id, shared_ptr<Odp> shpoutOdp, OdpVec& vec)
{
    shpoutOdp.reset();

    for (OdpVec::iterator iter = vec.begin(); iter < vec.end(); iter++)
    {
        Odp& odp = *(iter->get());
        if (odp.id == id)
        {
            shpoutOdp.reset(iter->get());
            return true;
        }
    }

    return false;
}

int main()
{
    OdpVec vec;

    vec.push_back(shared_ptr<Odp>(new Odp(0)));
    vec.push_back(shared_ptr<Odp>(new Odp(1)));
    vec.push_back(shared_ptr<Odp>(new Odp(2)));

    shared_ptr<Odp> shOdp;
    bool found = findOdpWithID(0, shOdp, vec);
    found = findOdpWithID(1, shOdp, vec);
}

Just before main() concludes, the output of this program is:

Destructing Odp 0
Destructing Odp 1

Why does this happen? I’m retaining a reference to each of the Odp instances within the vector. Does it have something to do with passing a shared_ptr by reference?

UPDATE I thought that shared_ptr::reset decremented the ref count, based on MSDN:

The operators all decrement the
reference count for the resource
currently owned by *this

but perhaps I’m misunderstanding it?

UPDATE 2: Looks like this version of findOdpWithID() doesn’t cause the destructor to be called:

bool findOdpWithID(int id, shared_ptr<Odp> shpoutOdp, OdpVec& vec)
{
    for (OdpVec::iterator iter = vec.begin(); iter < vec.end(); iter++)
    {
        Odp& odp = *(iter->get());
        if (odp.id == id)
        {
            shpoutOdp = *iter;
            return true;
        }
    }

    return false;
}
  • 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-15T19:43:54+00:00Added an answer on May 15, 2026 at 7:43 pm

    This line right here is probably what is tripping you up.

    shpoutOdp.reset(iter->get());
    

    What you’re doing here is getting (through get()) the naked pointer from the smart pointer, which won’t have any reference tracking information on it, then telling shpoutOdp to reset itself to point at the naked pointer. When shpoutOdp gets destructed, it’s not aware that there is another shared_ptr that points to the same thing, and shpoutOdp proceeds to destroy the thing it’s pointed to.

    You should just do

    shpoutOdp = *iter;
    

    which will maintain the reference count properly. As an aside, reset() does decrement the reference counter (and only destroys if the count hits 0).

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

Sidebar

Related Questions

I guess the real question is: If I don't care about dirty reads, will
I guess I don't need the web browser, since network capabilities are built into
I hate writing error condition code. I guess I don't have a good approach
I don't know if this is to do with Raphael, ColorBox or jQuery. Here's
Is there some built-in way to share files between Xen guests? I don't currently
My guess is that class variables (class var) are truly global in storage (that
I guess this is an odd one, and the answer is most likely it
I guess I'll illustrate with an example: In this game you are able to
I guess I'm really after an aid to help people who forget, Cruise Control
I guess it should be a common technique, However, I tried the following two

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.