correct way to avoid Memory leakage

Hi, i wanna ask you what is the correct way for avoid memory leakage..


INSIDE A CLASS
The only situation in wich i could have memory leakage problem is when i am working with pointer.
The only thing that i have to do is to release dynamic memory in the destructor.


OUTSIDE THE CLASS
The only thing that i have to guarantee is that for each new i have a delete.
The } don't call the destructor for object allocated with the operator NEW.

1 Problem) It's all true what i have wrote?
2 Problem) if there is an exception before i call the DELETE?
how can i manage it?

Thanks in advance

It's mostly right. You seem to be generalizing a bit and that raises an alarm or two.

The only situation in wich i could have memory leakage problem is when i am working with pointer.

While it's true that all dynamically allocated memory requires a pointer, not all pointers are to dynamically allocated memory. Sort of how like every square is a rectangle but not ever rectangle is a square.

The only thing that i have to do is to release dynamic memory in the destructor.

While this is true it may not always be that obvious. Say for instance I have an array of base pointer objects and I don't know how many objects I need to store until run-time. I can create each of the objects dynamically and store what I need into the array but when do you call their destructor?
While it's true that all dynamically allocated memory requires a pointer, not all pointers are to dynamically allocated memory. Sort of how like every square is a rectangle but not ever rectangle is a square.


So, the messagge is, you could have pointer that refer to a local variable for example. In this case you will not have memory leakage problem because the memory pointed from the pointer will be automatically release at '}'.
correct?


Probably i don't get the second point :(
I have to call the destructor when i don't need my array of object.
And i have to call it for each and every valid pointer..
You have the first point correct. Don't worry too much about the second point I was trying to make, it's probably more of a design issue.

I guess I missed your second question. You should really only delete dynamically allocated memory at the end of a function, that way if an exception is thrown, you catch it and move on to the end of the function where you would have deleted them anyway. There are of course exceptions (no pun intended) to this as there are to every generalization but keeping to this guideline will simplify about 80% of what you have to write.
Last edited on
Ok ,i had understand.
My 2 question is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

TRY {
,,
,,
NEW OF SOMETHING A
NEW OF SOMETHING B 


DELETE OF SOMETHING A


//exception here!

DELETE OF SOMETHING B 

} CATCH (EXCEPTION){

DELETE OF SOMETHING A
DELETE OF SOMETHING B 

}


In this case i wil release A 's memory, 2 times. How can i avoid it?
Last edited on
In this example right here there is no chance that an exception would be thrown before the first deletion of 'A' occurs, so you would simply remove that delete command from the catch block. In the event that an exception being thrown before the first deletion of 'A' is possible you could have multiple catch blocks.
Last edited on
Ok thank you :)
Last edited on
> The only thing that i have to do is to release dynamic memory in the destructor.
you have to make sure that you've got a valid pointer to be deleted. So you need to code a proper copy/move constructor and assignment operator.


> if there is an exception before i call the DELETE?
> how can i manage it?
given your example, I don't see why you are using dynamic allocation instead of simply constructing an object.
1
2
3
4
5
6
7
8
{
   T *bar = new T(foo);
   delete bar;
}

{
   T bar(foo);
}


if that were not the case, and you do have a valid reason to be creating things with new, the you could simply convert the "outside the class" case into "inside the class".
Simply let an object to manage that memory.
Topic archived. No new replies allowed.