SirJay
SirJay

Reputation: 119

Delete object being pointed to

I have a for loop that I am setting an object dynamically in using a pointer. I'd like to reuse the same pointer for the next instance but I'm worried that there is a memory leak.

myPointer *obj;
for (int i = 0; i <= 10; i++) {
    obj = new Object(i);
    obj->doSomeStuff();
}

Should I set obj to NULL at the end of the for statement? delete obj only seems to cause severe issues in this case.

Edit: I added a reset method to the class to reset the values the private variables in order to take another pass. Seems to be working a bit better/faster.

Upvotes: 0

Views: 5130

Answers (4)

Nard
Nard

Reputation: 1006

To add on, new Object(i); creates a new unnamed Object on the heap, a space in memory that you as the programmer are entrusted to manage.

With obj = new Object(i); you are keeping a handle to the object by recording its location in memory, i.e. its address, in the pointer obj, which are what pointers store.

Then the code goes into the second loop and overwrites the recorded location in obj with the location of a new Object(i).

Now you've lost the location of the first Object. Since you don't know where it is, you cannot possibly clean it up, which results in a memory leak.

As in the example by Cyber, all these messy stuff with the heap can be avoided by simply creating on the stack instead. The stack is memory that is managed for you. You can read more about it here .

Now if you insist on using pointers and allocating on the heap, you'll need to learn to clean up after yourself, like everyone else here has suggested.

The idea, again mentioned by Cyber, is to have a delete for every new that you have. To delete something, you need its location, which is why you'll need a pointer to remember its location until it is deleted.

You will also need the correct type, so that we can know how big a chunk of memory we should delete. Notice that your pointer is of a different type from the object you're trying to delete.

Also, please do not be mistaken that pointers only work with the heap. They work with objects on the stack as well.

Object obj(10);
Object obj2(20);
Object* pointer_to_obj = &obj; //address of obj
pointer_to_obj = &obj2; //Now store the address of obj2

I hope I've been of help to you.

P.S. Setting obj, a pointer, to NULL or nullptr(C++11) basically means that the pointer is not storing the location of any object.

Upvotes: 2

ravi
ravi

Reputation: 10733

Actually right thing in this case would be to delete the pointer OR you will end up with memory leak.

myPointer *obj;
for (int i = 0; i <= 10; i++) {
    obj = new Object(i);
    obj->doSomeStuff();
    delete obj;
}

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 117856

Anything you new you need to delete or you will leak it.

Object* obj;
for (int i = 0; i <= 10; i++) {
    obj = new Object(i);
    obj->doSomeStuff();
    delete obj;
}

Otherwise as soon as the next iteration comes around, you new another Object, assign that to the obj pointer, and the previous object is now leaked.

In this case you do not even need a pointer if you do not want to worry about managing memory yourself.

for (int i = 0; i <= 10; i++) {
    Object obj {i};
    obj.doSomeStuff();
}  // Now obj will fall out of scope and be automatically cleaned up.

Upvotes: 5

David Schwartz
David Schwartz

Reputation: 182753

Setting the pointer to NULL ensures a memory leak, as now there's no way to delete the allcoated object. If delete obj causes issues, then you need to fix those issues, otherwise you'll have memory leaks. (Unless the object somehow deletes itself.)

Upvotes: 2

Related Questions