Click here to Skip to main content
15,881,638 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
What I understand the reason for memory leak is because an object is created on the heap and is not cleaned by using the delete keyword. So if in a project a class is always initialized in the stack there will be no memory leaks for this class. Is it possible to have memory leaks for a class if the objects of this class are always initialized without the new keyword?

For example, I have a class CStudent and in my code I always use CStudent stu to initialize this class. I have some vectors to store objects of CStudent but I define them as vector<CStudent>. I never use new keyword to create a CStudent object. So is it possible to have memory leaks of CSudent objects?
Posted
Updated 24-May-11 15:41pm
v5
Comments
Sergey Alexandrovich Kryukov 24-May-11 21:01pm    
You question has nothing to do with C++/CLI? Do you also need advice about memory leak in managed platform? (Yes, you can get memory leak in managed platform as well.)
If not, remove C++/CLI tag.
--SA
[no name] 25-May-11 5:34am    
One should release all WINDOWS HANDLE for objects been used.

Your vectors are probably using heap memory to allocate space for your CStudent objects so you could probably still find a way to leak memory.

Why do you ask?? Depending on the size of the objects and how many there is, the stack may not be the appropriate place for them. Plus your stack based objects only last for the duration of the function they are created in.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-May-11 21:16pm    
I agree, my 5.
My answer actually answers original question: if memory leak is possible without new.
I overview the situations when it happens, please see.
--SA
Mark Salsbery 24-May-11 21:19pm    
Thanks, and a 5 back at you. I did see your post!
Sergey Alexandrovich Kryukov 24-May-11 21:21pm    
Sure, and thank you.
--SA
Yes, you can have a memory leak without the key word new.

The example you explain does not look like it can cause a memory leak if the vector is cleaned up correctly, but there can be different scenarios. I would classify them into two classes: memory allocation below the new level and above the new level. By new level I mean memory allocation/deallocation embedded in the language.

Below: C++ new/delete is implemented on top of OS platform. Language binding with OS is usually based on allocation and deallocation of system heap. For optimization and reduce of fragmentation a sub-allocator can be used: a block of memory can be allocated on the system heap in big chunks; and this memory is sub-allocated for smaller objects. Your code can also use language binding to the OS directly and allocate memory without using new. Is this memory is not properly reclaimed, memory leak may occur. Also, some OS API can allocate memory indirectly. It requires some clean-up OS calls. If not cleaned-up correctly, such calls can cause some memory leaks. Such indirect memory leak cause by incorrect use of OS binding is closely related to the problem of resource leak.

Above: you can use third-party libraries which can allocate memory at the language level (new) or at the level of OS. If you make bugs in using of such libraries or if the libraries have bugs, it can cause memory leak. You may not be able to detect the presence of new in such libraries when their source code is not available to you.

One practical note: very typical source of memory leaks as a result of programming bug is the failure to reclaim memory in case of exception. Proper use of try-finally construct helps to avoid such situations.

—SA
 
Share this answer
 
v3
Comments
Albert Holguin 24-May-11 23:55pm    
Excellent answer!... To give an example, I use a third party license manager to manage my software licenses, it currently leaks a small amount of memory internally because it looks like they don't do a good job of cleaning their resources even though I'm calling everything I need. Therefore, unfortunately, I have a small leak that's caused by my use of a third party library.
Sergey Alexandrovich Kryukov 25-May-11 0:03am    
Thank you, Albert.
--SA
Sergey Alexandrovich Kryukov 25-May-11 0:04am    
Aha. Good third party -- dead third party.
--SA
Albert Holguin 25-May-11 0:06am    
lol... only leaks on program close and a very small amount, so I can live with it for now.
Sergey Alexandrovich Kryukov 25-May-11 0:12am    
Leaks a very small amount,
I can live with it for now!
.......
.......

:-)

Leak on close is not a true lick (but potentially it can become a true like, depending on how you use it).
--SA
 
Share this answer
 
v2
In fact, you take the memory and you should free the memory.
You can use different debuggers to find out where there is a leak and then think about its removal. I use deleaker? as my debugger.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900