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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:00:17+00:00 2026-06-12T05:00:17+00:00

I have the following situation: Foo1 foo1* = new Foo1(…); Foo2 foo2* = new

  • 0

I have the following situation:

Foo1 foo1* = new Foo1(...);
Foo2 foo2* = new Foo2(foo1, ...);

or

Foo1 foo1(...);
Foo2 foo2(foo1, ...);

I need to delete Foo1 and Foo2. If I understand correctly, I should free memory in the reverse order it was allocated. So I should do:

delete(foo2);
delete(foo1);

I can’t do this however, because foo1 is being set to NULL in foo2’s destructor. So when I try to delete foo2, it’s trying to delete a NULL reference and triggering an assert. Is there a way around this? The best solution here would allow me to still allocate the memory to the stack but it’s definitely not necessary.


EDIT:

See this thread: Will a shared_ptr automatically free up memory?

Thank you for the responses. I was (clearly) wrong about what the problem was. I need to use a shared_ptr here because I can’t change the API.

Foo1 *foo1 = new Foo1(...);
shared_ptr<Foo2> foo2(foo1);

Is the shared_ptr here going to handle freeing the memory used by foo1? If I understand correctly, I shouldn’t have to call delete on foo1 correct?

I will ask as a seperate question and link to it.

  • 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-12T05:00:18+00:00Added an answer on June 12, 2026 at 5:00 am

    First, you only need to delete objects created using new. Generally speaking, there is no order dependency for objects created by new. There may be a constructor/destructor dependency, though, such as if foo2 calls functions on foo1 in foo2’s destructor. In this case, order of destruction is important.

    Second, even if the foo1 pointer in foo2 is set to null, that is just the local copy of the pointer held in foo2. It will not effect other copies of the pointer. Setting foo1 to to null in foo2 is not a bad idea, since it signals the object should no longer be used but this is different to calling its destructor or freeing it.

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

Sidebar

Related Questions

I have following situation: I need to join VendInvoiceJour.InvoiceAccount <- VendTable.AccountNum and take VendTable.Vendgroup.
I have the following situation: I need to save data from an Intent's putExtra
I have the following situation: if (condition) { std::unique_ptr<AClass> a(new AClass); // code that
I have the following situation on my new ASP.Net page: I am using a
I have the following situation, I'm using jquery and I need to sum up
I have the following situation: class A { public: A(int whichFoo); int foo1(); int
I am new to DotNetNuke; I have following situation please suggest what are the
I have following situation - Have a MongoService class, which reads host, port, database
I have following situation. In a constructor of a pseudo class I attach a
I have following situation (simplified, of course): MyDomain.groovy: class MyDomain { MyAnotherDomain anotherDomain //

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.