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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:36:53+00:00 2026-06-11T09:36:53+00:00

I’m new to the concept so don’t be hard on me. why doesn’t this

  • 0

I’m new to the concept so don’t be hard on me.
why doesn’t this code produce a destructor call ?
The names of the classes are self-explanatory.
The SString will print a message in ~SString().
It only prints one destructor message.

int main(int argc, TCHAR* argv[])
{
smart_ptr<SString> smt(new SString("not lost"));
 new smart_ptr<SString>(new SString("but lost")); 
return 0;
}

Is this a memory leak?
The impl. for smart_ptr is from here

edited:

//copy ctor
    smart_ptr(const smart_ptr<T>& ptrCopy) 
    {
        m_AutoPtr = new T(ptrCopy.get());
    }
    //overloading = operator
    smart_ptr<T>& operator=(smart_ptr<T>& ptrCopy) 
    {
        if(m_AutoPtr)
            delete m_AutoPtr;
        m_AutoPtr = new T(*ptrCopy.get());
        return *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-06-11T09:36:54+00:00Added an answer on June 11, 2026 at 9:36 am

    By new smart_ptr<SString>(new SString("but lost")); you are creating a new, dynamically allocated smart pointer. You don’t store the result of the allocation (a pointer to a shared_ptr to a SString) anywhere, it’s dangling… since you don’t store the result, you also can not call delete for it – therefore it’s destructor won’t be called, and in turn also the SString destructor of the contained object won’t be called!

    If you try

    smart_ptr<SString> *p = new smart_ptr<SString>(new SString("but lost")); 
    delete p;
    

    instead, you will see the destructor called also for this case.

    However, that’s no sensible use of a smart_ptr. smart_ptr were created so that you don’t need to call delete manually; therefore, don’t use them that way; use them as in your first statement!

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.