Reputation: 688
Is it possible to create memory leak in C++ without heap allocation, through very bad design ?
One example I had in mind, please correct me if it does not do what I think it does:
#include <iostream>
#include <string>
void WhatIsYourName()
{
std::string name;
std::cout << "What is your name? ";
getline (std::cin, name);
std::cout << "Hello, " << name << "!\n";
WhatIsYourName();
}
int main()
{
WhatIsYourName();
}
To me it looks like WhatIsYourName()
is initializing a new std::string name
on every call, but the function never really goes out of scope so the memory is never released.
Is that right ? Or is the compiler smart enough to see that the variable will not be used in the future, so it deletes it without the function going out of scope ?
What kind of other bad design would create a memory leak using only stack allocation ?
Upvotes: 5
Views: 7284
Reputation: 11
If the compiler has a bug, you could have a stack memory leak. For some obscure reason it "forgets" to call the destructor of a local variable. Check GCC bug 66139.
Upvotes: 1
Reputation: 121427
I think you have misunderstood what a memory leak is in C and C++ worlds. Given your condition "without heap allocation", it's not possible to leak memory.
A memory leak is typically associated with dynamic memory allocation(s) and then losing the ability to deallocate it. For example:
// Memory leak
int x;
int *q = new int;
q = &x; // memory leak because the "allocated" q is lost.
With RAII and not using new
+ delete
for memory management, it's easily avoidable in C++.
Upvotes: 1
Reputation: 62613
The question is more interesting than you probably thought. There is no memory leak by definition, since memory leak can only be happening if a) a pointer to the allocated memory is lost (not the case here) or b) the pointer is never freed until program exits normally. The second is not the case either, since this program will never exit normally.
However, there is an interesting question of what actually happens to the string variable. Since the recursive call happens in the end of the function, from the glance it might seem that the code will be a candidate for tail-call optimization. However, if you check the assembly, it does not happen, because compiler needs to call string destructor after the return from the tail-call. I personally see this is a flaw in the optimization, since potentially compiler should be able to see that string variable is not used after the call and destruct it in advance.
Upvotes: 2
Reputation: 181068
No it is not possible to create a memory leak with objects that have automatic storage duration (some people call this stack). When you have a variable with automatic storage duration it is cleaned up by the program automatically. Once the scope ends that it was declared in it gets destroyed and any resources it had are freed (as long as it was designed properly). In your example even though you never exit the scope due to infinite recursion it is not a memory leak. If the infinite recursion was removed and instead it ended at some condition then all the variables you declared would be cleaned up.
To get a memory leak you need to create a object with dynamic storage duration (what some people call heap) and never clean it up. When you have a object with dynamic storage duration the compiler does not clean it up for you. You need to make sure you clean it up yourself. It is this forgetting or leaving out of the clean up code that causes a memory leak.
Upvotes: 6
Reputation: 56577
You have a recursive call that doesn't stop (there's no exit condition), so a new stack page is created for every function invocation, until of course the program crashes due to stack overflow. The compiler cannot optimize that away.
Regarding your last question, whether is possible to create a memory leak without heap allocation (and without this kind of infinite recursion), I believe it is not, the local variables are automatically released, that's why this type of storage duration is called automatic storage duration.
Upvotes: 8