1

I want to know when or if I have to delete this object. Here is the constructor of a basic class Object Object.cpp:

Objects::Objects{
    Obj one = new Obj;
    Obj two = new Obj;
}

I know when allocating memory you are supposed to delete it at some point, but I have allocated memory in the constructor and want to use the variables one and two again. When do I delete them?

Striezel
  • 3,693
  • 7
  • 23
  • 37
  • 1
    Typically you will delete them in the corresponding destructor (in this case, `~Objects`.) – 0x5453 Mar 25 '21 at 14:33
  • 2
    those are local objects, hence you should delete them before you leave the constructor, because otherwise any reference to them is lost and you have a leak. Having said this, most likely you do not need any pointers, and most certainly you do not need to use `new` (and I suppose you actually wanted to initialize members, but thatss not what the code does, please clarify) – 463035818_is_not_an_ai Mar 25 '21 at 14:34
  • Maybe you don't want to use raw pointers and instead you wanted std::unique_ptr. Also are you sure you want to create local objects in the constructor? – drescherjm Mar 25 '21 at 14:34
  • `Obj one = new Obj;` would be a bug. `one` is not a pointer. Maybe you wanted `Obj one;` but I question why you want to create local objects that don't exist after the constructor is done. – drescherjm Mar 25 '21 at 14:35
  • @drescherjm sorry for going a bit offtopic, but a way to make this legal is when `Obj`s constructor takes a pointer to an `Obj`. THough even then one shouldnt write code like this imho – 463035818_is_not_an_ai Mar 25 '21 at 14:37
  • Good point. I did not consider it because its an odd way to do things. – drescherjm Mar 25 '21 at 14:39
  • 1
    try to use the search engine here, you will find many useful references about pointers,https://stackoverflow.com/questions/tagged/pointers?tab=Votes and https://stackoverflow.com/questions/655065/when-should-i-use-the-new-keyword-in-c – ma1169 Mar 25 '21 at 14:45

2 Answers2

4

To make it simple, anytime you make a new, you should make a corresponding delete.

In your case, you have to delete the allocated objects Obj at latest before your objects Objects are deleted, which means in the destructor of Objects, which also means that you have to keep a way to access to the one and two pointers at the destructor (one way might be to make them as a member of Objects). Your current case fails to do so because one and two are local variables and are not pointers.

It may look like the following:

class Objects
{
public:
  Objects();
  virtual ~Objects();
private:
  Obj* one;
  Obj* two;
};

Objects::Objects{
    one = new Obj;
    two = new Obj;
}

Objects::~Objects{
    delete one;
    delete two;
}

There are some rules you need to be aware of, when coping with resource management and it is the rule of three/five/zero which will guide you to create robust code in terms of resource management.

Another remark: It might be better to use smart pointers as even with the approach I mentionned in my post, if an exception or a crash happenned before your code executes the delete lines, you would still have a memory leak as explained here

Pat. ANDRIA
  • 2,330
  • 1
  • 13
  • 27
  • 1
    This will also leak memory if `new Obj` throws an exception. And that is why you should always use smart pointers or container classes. Never try to do `new`/`delete` on your own. You will most likely mess it up. But even better, when possible (as in this case), you should just inline the object. – HAL9000 Mar 25 '21 at 15:04
  • Thanks for your review, I agree with you concerning smart pointers. Howver I provided this answer and did not talk about `smart pointers` because the original question was `I would like to know when and if I need to use delete` and he provided a similar example. I add your remark in my response. – Pat. ANDRIA Mar 25 '21 at 15:08
1

I want to know when or if I have to delete this object.

If you allocate with new and don't deallocate, then you leak memory.

I have allocated memory in the constructor and want to use the variables one and two again, when do I delete them ?

If you want to initialise variables in constructor and use them later, then you should use non-static member variables instead of automatic variables in the constructor.

If you were to allocate memory in constructor, and point to it with a member, then typically you should deallocate in the destructor. For more information, see Resource Acquisition Is Initialisation idiom. However, you should not use bare owning pointers, and you should not use dynamic allocation unnecessarily. Here is a simple example that I recommend as your first option:

struct Objects {
    Obj objects[2];
};
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • fyi, I made an attempt to fix OPs question, but my edit was not in sync with your answer. If you feel like you can rollback my rollback ;) – 463035818_is_not_an_ai Mar 25 '21 at 14:44
  • 1
    @largest_prime_is_463035818 I wouldn't assume that OP is aware of the distinction between initialising an automatic variable, and initialising a member variable in the constructor, so the "fix" may be confusing. With the type being wrong as well, it's hard to guess either way. – eerorika Mar 25 '21 at 14:46