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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:36:02+00:00 2026-05-15T17:36:02+00:00

So say I have a class like this: class A { public: A( SomeHugeClass*

  • 0

So say I have a class like this:

class A {
    public:
        A( SomeHugeClass* huge_object)
            : m_huge_object(huge_object) {}
    private:
        SomeHugeClass* m_huge_object;        
};

If someone uses the constructor like this:

A* foo = new A(new SomeHugeClass());

Who’s responsibility is it to call delete on the object newed in the constructor? In this case, the scope in which the A constructor was called can only delete foo since the SomeHugeClass is anonymous.

However, what if someone uses the constructor like this?

SomeHugeClass* hugeObj = new SomeHugeClass();
A* foo = new A(hugeObj);

Then, the caller can call delete hugeObj at some point, right?

Does this implementation of A leak memory on destruction?

I’m working on a project with a lot of object composition done this way and as much as I would love to use smart pointers, I have to talk to the project leads about changing old code to take advantage of that before I can.

  • 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-15T17:36:03+00:00Added an answer on May 15, 2026 at 5:36 pm

    I try to follow this simple rule whenever it is possible: The one who calls new should call delete as well. Otherwise the code soon becomes too messy to keep track of what is deleted and what is not.

    In your case, if A::A receives the pointer, it must not delete it. Think of this simple case:

    SomeHugeClass* hugeObj = new SomeHugeClass();
    A * a1 = new A(hugeObj);
    A * a2 = new A(hugeObj);
    

    Class A can not know who else is using that pointer!

    If you want class A to take care of the pointer, it should create it itself.

    Of course, you could handle both cases, but that might be an overkill, something like this:

    A::A() : own_huge_object(true) {
        m_huge_object = new SomeHugeClass();
    }
    A::A(SomeHugeClass * huge_object ) : own_huge_object(false) {
        m_huge_object = huge_object;
    }
    A::~A() { if(own_huge_object) delete m_huge_object; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

lets say I have my owndefined datatype like this: public class InformationType { private
Say I have a class like this: class Person { private $value; public function
Lets say I have a class like this: public class Foo { public Foo
lets say I have a class like this: public class MyClass { private String
Let say I have a class Foo like this public class Foo { public
Let's say I have a class like this one: public class MyClass { private
Let say I have a class like this: public sealed class Foo { public
Let's say I have a class like this: public class Person { private String
say, I have a partial class that contains a custom property like this public
So, say I have models like this: class Foo(Model): name = CharField(max_length=200) def latest_comment(self):

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.