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

  • Home
  • SEARCH
  • 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 6133407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:13:29+00:00 2026-05-23T17:13:29+00:00

This might be a dumb question but I’m just not sure about the answer.

  • 0

This might be a dumb question but I’m just not sure about the answer. The following code read a file, and for each line of the file, a smart pointer is created by “new”. If the smart pointer will be used in the future, it’s stored in a list, otherwise it’s not stored.

My question is that: if the smart pointer is not stored, will that cause potential memory leak? Thank you.

int main(){
    .....;
    std::list<SomeClass> aList;
    while(inFile >> ss){
         std::tr1::shared_ptr<SomeClass> aPtr(new SomeClass());
         //do something in foo(aPtr) to aPtr, 
         //if aPtr will be used later, then it's stored in aList
         //otherwise, it's not stored
         foo(aPtr);
    }
    .....;
}
  • 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-23T17:13:30+00:00Added an answer on May 23, 2026 at 5:13 pm

    As long as you’re storing it with a copy of the smart pointer, this will not leak memory. When the aPtr object falls off the stack (at the end of each while loop execution), it will be destroyed. If it is the only holder to the allocated object, it will delete it. But if you stored a copy of aPtr elsewhere, then it is not the only holder to the allocated object, and it will not delete it.

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

Sidebar

Related Questions

This might be a dumb question, but considering the following semi-pseudo code. How do
This might be a dumb question but I have not find the answer when
Ok, this might just be a dumb question, but I was wondering if Bluetooth
This might sound very dumb question but I am just confused. I am quite
This might be (read: probably is) a dumb question, but here goes... Is there
You might see this as a dumb question, but I'm curious about how I
This might be a dumb question. I think I already know the answer, just
This might sound like a reaaaally dumb question but... why do browsers have a
Ok sorry this might seem like a dumb question but I cannot figure this
This might be a dumb question, but I don't get it: I have 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.