Is this considered a memory leak? - c++

Suppose you have a simple class like this:
class foo{
private:
int* mData;
int mSize;
public:
foo(int size){
mSize = size;
mData = new int [mSize];
}
~foo() {
mSize = 0;
delete [] mData;
}
};
Then inside main you do:
int main () {
static int HUGE = 100000000;
foo a(HUGE);
// do something useful with a
// .
// .
// .
// Now I'm done with a; I do not need it anymore ...
foo b(HUGE);
// do something useful with b
// Ok we are done with b
return 0;
}
As you can see a is no longer needed after b, but since it is created on the stack, the destructor won't be called up until the end of the program. Now, I know this is not the same as allocating with new and forgetting to call delete, however this is still wasting memory. Do you consider this as "memory leak" or just a bad programming?
Also, How would you avoid situations like this? One way would be to manually call the destructor when the object is not needed anymore, but, besides looking ugly and unfamiliar!, you get into trouble of double free unless you change the destructor to something like:
foo::~foo(){
if (mData != NULL){
delete [] mData;
mData = NULL;
mSize = 0;
}
}
Another way is to create a on the heap via foo *pa = new foo (HUGE) and then call delete pa once the object is no longer needed. This works but at the danger of introducing another possible memory leak (if one forgets to call delete pa).
Is there any better way to get rid of unneeded objects?

Destructors are called when an object goes out of scope. C++ allows arbitrary scopes inside function bodies. Write your main function this way:
int main () {
static int HUGE = 100000000;
{
foo a(HUGE);
// do something useful with a
// Now I'm done with a; I do not need it anymore ...
}
{
foo b(HUGE);
// do something useful with b
// Ok we are done with b
}
// etc.
return 0;
}
I see your example is simplified, but in a real program, don't forget to either
implement an appropriate copy constructor and operator= for foo or
add a declaration for a private copy constructor and operator= so it cannot be called.

Just place your huge a and b objects into their own braces if you are worried about scope.
And this isn't technically a memory leak, but it is very poor memory management as you have stated.
{
{
foo a(HUGE);
}
...
{
foo b(HUGE);
}

No, it's definetely not a memory leak.
A memory leak is when you allocate memory and you lose its handle, so you can't free it afterwards. It doesn't matter where or when you free the memory, as long as you do.
You could add an enclosing scope to force memory freeing:
{
foo a(HUGE);
}
{
foo b(HUGE);
}

This is not a memory leak, because you don't loose track of your allocated memory. However this is slightly ineffective, especially when the program is running longer, and should be avoided.
You can use scopes to shorten the lifetime of an object:
int main () {
static int HUGE = 100000000;
{
foo a(HUGE);
// do something useful with a
// .
// .
// .
// Now I'm done with a; I do not need it anymore ...
}
{
foo b(HUGE);
// do something useful with b
// Ok we are done with b
}
return 0;
}
Also, it is worth reconsidering if this two parts of code should be in separate functions, then the allocated objects will be freed when returning from function.

The constructor of the class could also take a block of memory you allocated in your 'main()' function as a parameter. That way, once 'a' is done with the use of the memory block, you can pass it into 'b' as well. 'foo' destructor does not need to release any memory at all, and you don't need to be worried about wasting memory, or object lifetimes at all.

Do you consider this as "memory leak" or just a bad programming?
No, it is not memory leak.
How would you avoid situations like this?
Write small functions, few lines. Your code will be more readable and the unused variable allocated on the stack will be freed.

It's not a memory leak; however it precisely the sort of memory usage that Firefox developers have spent a long time fixing.
Scope is probably the easiest way to fix this, as Dark Falcon suggests. Alternatively move the allocations and related code into separate functions.
Also pointers can be more safely handled with std::auto_ptr so that they are freed when the scope is released.

Do you consider this as "memory leak"
No, unless you do something like longjmp in the middle.
or just a bad programming?
I consider using new[] to allocate array in your class bad programming practice, because you have std::vector for that.
Also, How would you avoid situations like this?
Enclose foo into scope:
{
foo a(HUGE);
}
unless you change the destructor to something like:
delete ignores null pointers. Destructor is called only once, so no need to zero variables. Calling destructor manually is a VERY BAD IDEA - it isn't meant for that. If you want to reinitialize the structure, implement clear() or resize() methods.
Is there any better way to get rid of unneeded objects?
Yes, enclose them into scopes.

It's not a memory leak, but if you have a variable that you need the
first half of a function, and not the second, there's a good chance that
the function is doing to much, and should be refactored into two (or
more) separate functions.

Extract functions to reduce the scope. Give them good names:
void do_a(int amount)
{
foo a(amount);
// ask `a` to be useful
}
void do_b(int amount)
{
foo b(amount);
// ask `b` to be useful
}
int main () {
static int HUGE = 100000000;
do_a(HUGE);
do_b(HUGE);
return 0;
}

Related

In the case of using a std::unique_ptr to automatically deallocate memory upon exiting a scoped block, why not just use the stack?

This is a great answer about smart pointers, such as unique pointers: What is a smart pointer and when should I use one?.
Here is an example they provide as the simplest use of a unique pointer:
void f()
{
{
std::unique_ptr<MyObject> ptr(new MyObject(my_constructor_param));
ptr->DoSomethingUseful();
} // ptr goes out of scope --
// the MyObject is automatically destroyed.
// ptr->Oops(); // Compile error: "ptr" not defined
// since it is no longer in scope.
}
However, this begs the question: in cases such as this, where the goal is to simply delete the object (free the memory) the unique pointer points to when it goes out of scope, why not just put the whole object on the stack instead, like this??
void f()
{
{
MyObject myobj(my_constructor_param);
myobj.DoSomethingUseful();
} // myobj goes out of scope --
// and is automatically destroyed.
// myobj.Oops(); // Compile error: "myobj" not defined
// since it is no longer in scope.
}
It seems to me the only logic can be that some objects are so stinking large they may overflow the stack, as stacks seem to be limited to a few dozen KB to a few MB (C/C++ maximum stack size of program), whereas a heap can be hundreds of GB!
What's the logic? Give me some insight here into this seemingly unnecessary use case of the unique pointer. What am I missing?
Related:
"Another feature of the stack to keep in mind, is that there is a limit (varies with OS) on the size of variables that can be stored on the stack. This is not the case for variables allocated on the heap." (https://gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html)
While this is not a terrible useful example per-se, it becomes with some slight variations.
Polymorphism
struct Base { void blah() { std::cout << "Base\n";}};
struct Derived : Base { void blah() {std::cout << "Derived\n";}};
void blub(bool which) {
std::unique_ptr<Base> ptr = which ? new Base : new Derived;
ptr->blah();
}
Non-standard deleter
{
auto close = [] (FILE* fp) { fclose(fp);};
std::unique_ptr<FILE, decltype(close)> ptr(fopen("name"), close);
} // closes file
Dynamic Array (you can arguably use a vector)
{
std:: unique_ptr<int[]> ptr( new int [n]);
// From C++14 on, prefer if it is no problem to value-initialize the array
auto ptr = std::make_unique<int[]>(n);
// From C++20 on, there is no reason for the naked new
auto ptr = std::make_unique_for_overwrite<int[]>(n);
// is equivalent to the first line
}
EDIT: This also reasonable for big arrays, even if the size is know at compile time. If the size of the array would be really big (and this should be very, very rare) and you really do not want to run the risk of a stack overflow, this would be a safer possibility. But very probably std::vector is the better alternative still. Only if your object type is neither moveable nor copyable, the vector will have problems (since it cannot reallocate itself if necessary, and hence you can call basically no modifying member function).
Conditional creation of an object. (Only in C++11 and 14, after that use std::optional)
void blah (bool smth)
{
std::unique_ptr<T> opt;
if (smth) {
opt = std::unique_ptr<T>(new T);
}
}
If you want to return a pointer to an object, then without smart pointers, we had to do this:
MyClass* someFunc() {
MyClass* myObjPtr = new MyClass();
.
.
return myObjPtr;
}
void otherFunc() {
MyClass* myPtr = someFunc();
.
.
delete myPtr; // delete explicitely before return.
return;
}
Instead if we use smart pointers, we can do this:
typedef std::shared_ptr<MyClass> MyClassPtr;
MyClassPtr someFunc() {
MyClassPtr myObjPtr(new MyClass());
.
.
return myObjPtr;
}
void otherFunc() {
MyClassPtr myPtr = someFunc();
.
.
return; // when smart pointer goes out of scope,
// the heap allocated object which it contains is deallocated properly.
}
Size is not the primary concern, although it may be important if you have recursion, for example (I saw a library allocating 64 KiB buffer on stack, in a recursive function. But Musl provides 128 KiB stacks [to make threads lightweight], so...) But the object may be polymorphic, and not even created “right there” but rather returned from some function (as a pointer); unique_ptr may be handy to store that.
Besides that, unique_ptr (unlike auto_ptr AFAIK) is not restricted to on-stack usage. It can be a class member as well. Also it is not required to actually store anything, you may assign and reset it at any time.
Moreover, it is not restricted to C++ classes, you can store anything requiring cleanup there, like file descriptor or FILE*, for example.

Deleting object with template for int and object

Alright so Say I have a class with all its definition, bla bla bla...
template <class DT>
class Foo{
private:
DT* _data;
//other stuff;
public:
Foo(DT* data){ _data = data; }
virtual ~Foo(){ delete _data; }
//other methods
};
And then I have in the main method:
int main(){
int number = 12;
Foo<anyRandomClass>* noPrimitiveDataObject = new Foo<anyRandomClass>(new anyRandomClass());
Foo<int>* intObject = new Foo<int>(number);
delete noPrimitiveDataObject; //Everything goes just fine.
delete intObject; //It messes up here, I think because primitive data types such as int are allocated in a different way.
return 0;
}
My question is: What could I do to have both delete statements in the main method work just fine?
P.S.: Although I have not actually compiled/tested this specific code, I have reviewed it extensively (as well as indented), so if you find a mistake, please be nice.
You're taking the address of a literal and then calling delete on it later, which is wrong. It was not allocated with new, therefore you cannot deallocate it with delete (nor would it make any sense to).
If you had written new int(12) instead it would be ok, however, there are other problems.
First, your class violates The Rule of Three. What happens if I copy intObject and then call delete on both of them? You end up calling delete on the same pointer twice.
Second, why are you allocating these things dynamically to begin with? You create an RAII style wrapper to handle deallocation for you... and then proceed to allocate it manually. What problem is that solving?
I suppose this is an exercise for you, and that's great. Just remember what problem you're trying to solve with this code.
If I am using a std::vector<T> I am certainly not going to use it like this:
std::vector<int> *v = new std::vector<int>;
It defeats the entire purpose of using a vector! Now I have to manually manage this pointer/memory, and that's the problem that the vector class (and other RAII style classes) were created to solve.
So, to use it properly, you do this:
void foo()
{
std::vector<int> v;
// do stuff with v
// it allocates its memory dynamically so you don't have to.
// when we exit the function the destructor is called, the memory
// deallocated, and life continues as it should.
}
Use automatic storage duration to your advantage, that's the whole point. Also be very clear about who owns the memory. If it is not clear from your class design who owns a given chunk of memory then it is not safe to delete it in your destructor.
Ok, you changed the code to this now:
int number = 12;
// ...
Foo<int>* intObject = new Foo<int>(number);
Same problem; you are taking the address of a variable allocated with automatic storage duration and then calling delete on it. This is wrong. Anything you allocate with new you deallocate with delete, but nothing else. Ever. That's it.
It seem like you didn't know you can do new int(12). So for example you could change your code to the following:
Foo<int>* intObject = new Foo<int>(new int(12));
(I'm assuming this is just for learning, as not using new altogether would be better).
Also, I just noticed your code is wrong, perhaps you wanted the following:
Foo(DT* data){ _data = data; }
virtual ~Foo(){ delete _data; }
Side Note
Before posting a question, at least try to compile your examples.

C++ delete an object

I am not experienced in handling of the memory in a C++ program, so I would like a piece of advice in that case:
I want to create a new Object in a function in a class which is essential till the end of the program. As far as I am concerned, if I use the operator new, I should sometimes delete it. Taking into account that it must be initialized inside a class, when and how must I finally delete it?
I suggest the smart pointer idiom
#include <memory>
struct X
{
void foo() { }
};
std::share_ptr<X> makeX() // could also be a class member of course
{
return std::make_shared<X>();
}
int main()
{
std::share_ptr<X> stayaround = makeX();
// can just be used like an ordinary pointer:
stayaround->foo();
// auto-deletes <sup>1</sup>
}
If the pointer is truly a static variable, you can substitute a unique_ptr (which works similarly, but passes ownership on assignment; this means that the pointer doesn't have to keep a reference count)
Note To learn more about C++ smart pointers in general, see smart pointers (boost) explained
Note If you don't have the TR1/C++0x support for this, you can just use Boost Smartpointer
1 unless you are leaking copies of the shared_ptr itself; that would be some strange use of smart pointers previously unseen :)
Edit: Using some sort of smart pointer is often a good idea, but I believe it is still essential to have a solid understanding of manual memory management in C++.
If you want an object in a class to persist until the end of the program, you can simply make it a member variable. From what you've said, there's nothing to suggest you need to use new or delete here, just make it an automatic variable. If you did want to use new and delete for practice, you should read up on constructors and destructors for a class (you can and will use new and delete outside of classes, but I'm trying to keep this relevant to your question). Here's one I prepared earlier:
class Foo
{
public:
Foo(); // Default constructor.
~Foo(); // Destructor.
private:
int *member;
}
Foo::Foo() // Default constructor definition.
{
member = new int; // Creating a new int on the heap.
}
Foo::~Foo() // Destructor.
{
delete member; // Free up the memory that was allocated in the constructor.
}
This is a simple example, but it will hopefully help you out. Note that the variable will only persist as long as the object is alive. If the object is destroyed or goes out of scope, the destructor will be called and the memory will be freed.
You can use the smart pointer as suggested by Sehe or you can create a static object in the function and return a reference to it. You need not explictly delete the object, when the process terminates the object will be deleted. Like:
struct X {};
X& makeX() // could also be a class member of course
{
static X x;
return x;
}
int main()
{
X& stayaround = makeX();
}
On most operating systems (in particular Linux), if you allocate an object pointer with new Object, and don't bother delete-ing because you'll need it till the program ends, no harm is really done. There is some memory leak inside your program (you can use valgrind to hunt such leaks) but the kernel will release all the memory used by a process when it has ended.
A better alternative is to have a singleton class for the application data, like e.g. QApplication in Qt, ahd construct a single instance in that class early in your main, and have that class contain a smart or dumb pointer to your Object. The destructor should delete that object.

Am I using delete correctly here?

I've just started combining my knowledge of C++ classes and dynamic arrays. I was given the advice that "any time I use the new operator" I should delete. I also know how destructors work, so I think this code is correct:
main.cpp
...
int main()
{
PicLib *lib = new PicLib;
beginStorage(lib);
return 0;
}
void beginStorage(PicLib *lib)
{
...
if (command != 'q')
{
//let's assume I add a whole bunch
//of stuff to PicLib and have some fun here
beginStorage(lib);
}
else
{
delete lib;
lib = NULL;
cout << "Ciao" << endl;
}
}
PicLib.cpp
...
PicLib::PicLib()
{
database = new Pic[MAX_DATABASE];
num_pics = 0;
}
PicLib::~PicLib()
{
delete[] database;
database = NULL;
num_pics = 0;
}
...
I fill my PicLib with a Pic class, containing more dynamic arrays. Pic's destructor deletes them in the same manner seen above. I think that delete [] database gets rid of all those classes properly.
So is the delete in main.cpp necessary? Everything looking hunky dory here?
There are a couple of problems:
int main()
{
PicLib *lib = new PicLib;
beginStorage(lib);
return 0;
}
It is best to allocate and delete memory in the same scope so that it is easy to spot.
But in this case just declare it locally (and pass by reference):
int main()
{
PicLib lib;
beginStorage(lib);
return 0;
}
In beginStorage()
But I see no reason to manipulate a pointer. Pass it by reference and just use it locally.
void beginStorage(PicLib& lib)
{
....
}
In the PicLib class you have a RAW pointer: databases.
If you have a RAW pointer that you own (you create and destroy it) then you must override the compiler generated versions of the copy constructor and assignment operator. But in this case I see no reason touse a pointer it would be easier to just use a vector:
class PivLib
{
private:
std::vector<Pic> databases;
};
Yes, anything you create with new must be deleted with delete, and anything created with new[] must be deleted with delete[], at some point.
There are some things I'd point out in your code though:
Prefer std::vector<> over using new[] and delete[]. It'll do the memory management for you. Also have a look at smart pointers like auto_ptr, shared_ptr and in C++0x unique_ptr for automatic memory management. These help save you from forgetting a delete and causing a memory leak. If you can, don't use new/new[]/delete/delete[] at all!
If you have to new and delete something, it's a very good idea to new in a class constructor, and delete in a class destructor. When exceptions are thrown, objects go out of scope etc., their destructors are called automatically, so this helps prevent memory leaks. It's called RAII.
Having beginStorage delete its parameter is potentially a bad idea. It could crash if you call the function with a pointer not created with new, because you can't delete any pointer. It'd be better if main() created a PicLib on the stack rather than using new and delete, and for beginStorage to take a reference and not delete anything.
Yes it is necessary, unless you use an auto_ptr (and read up on the semantics of auto_ptr before you use it -- you can't copy it arround).
for example :
int main()
{
auto_ptr<PicLib> lib = new PicLib;
beginStorage(lib);
return 0;
} // auto_ptr goes out of scope and cleans up for you
else doesn't go with while. You'd want something more like:
void beginStorage(PicLib *lib)
{
while (command != 'q')
{
//let's assume I add a whole bunch
//of stuff to PicLib and have some fun here
}
delete lib;
lib = NULL; // Setting to NULL is not necessary in this case,
// you're changing a local variable that is about
// to go out of scope.
cout << "Ciao" << endl;
}
The delete looks good, but you should make sure to document that beginStorage takes ownership of the PicLib object. That way anyone using beginStorage knows that they don't have to delete it later.
The delete in main.cpp is necessary.
This is probably a matter of personal preference, but I would advise against calling new and delete in separate logical parts (here the delete call on the PicLib instance is in a separate function). Usually it's better to have the responsibility for allocation and deallocation given to just one part.
#AshleysBrain has a better suggestion (about creating PicLib the stack), although this might cause problems if PicLib takes up too much memory.
Generally you want to delete in the same place as you new. It makes the accounting easier. Better is to use a smart pointer (scoped_ptr in this case), which means that your code is still correct even if the body of the while throws an exception and terminates prematurely.
Everything looks good.
The delete in main.cpp is necessary because if you didn't call delete then the destructor is not run and your array is not deleted. You'd also be leaking the memory for the class, not just the array.
Yes, otherwise the destructor for PicLib will not be called.
One stylistic note though: If you new a method-scope object in a function, try and delete it in the same function. As a junior engineer that has had to go through large projects fixing other people's memory leaks...this makes things a lot easier for other people to read. From the looks of it, you could delete *lib after beginStorage() returns. Then it would be easier to see the scope of *lib in one place.

Return pointer to data declared in function

I know this won’t work because the variable x gets destroyed when the function returns:
int* myFunction()
{
int x = 4;
return &x;
}
So how do I correctly return a pointer to something I create within the function, and what do I have to take care with? How do I avoid memory leaks?
I've also used malloc:
int* myFunction2()
{
int* x = (int*)malloc(sizeof int); *x = 4; return x;
}
How do you correctly do this - in C and C++?
For C++, you can use a smart pointer to enforce the ownership transfer. auto_ptr or boost::shared_ptr are good options.
Your second approach is correct. You just need to clearly document that the caller "owns" the result pointer, and is responsible for freeing it.
Because of this extra complexity, it is rare to do this for "small" types like int, though I'm assuming you just used an int here for the sake of an example.
Some people will also prefer to take a pointer to an already allocated object as a parameter, rather than allocating the object internally. This makes it clearer that the caller is responsible for deallocating the object (since they allocated it in the first place), but makes the call site a bit more verbose, so it's a trade-off.
For C++, in many cases, just return by value. Even in cases of larger objects, RVO will frequently avoid unnecessary copying.
One possibility is passing the function a pointer:
void computeFoo(int *dest) {
*dest = 4;
}
This is nice because you can use such a function with an automatic variable:
int foo;
computeFoo(&foo);
With this approach you also keep the memory management in the same part of the code, ie. you can’t miss a malloc just because it happens somewhere inside a function:
// Compare this:
int *foo = malloc(…);
computeFoo(foo);
free(foo);
// With the following:
int *foo = computeFoo();
free(foo);
In the second case it’s easier to forget the free as you don’t see the malloc. This is often at least partially solved by convention, eg: “If a function name starts with XY, it means that you own the data it returns.”
An interesting corner case of returning pointer to “function” variable is declaring the variable static:
int* computeFoo() {
static int foo = 4;
return &foo;
}
Of course this is evil for normal programming, but it might come handy some day.
C++ approach to avoid memory leaks. (at least when You ignore function output)
std::auto_ptr<int> myFunction() {
std::auto_ptr<int> result(new int(4));
return result;
}
Then call it:
std::auto_ptr<int> myFunctionResult = myFunction();
EDIT: As pointed out by Joel. std::auto_ptr has it's own drawbacks and generally should be avoided.
Instead std::auto_ptr You could use boost::shared_ptr (std::tr1::shared_ptr).
boost::shared_ptr<int> myFunction() {
boost::shared_ptr<int> result(new int(5));
return result;
}
or when use C++0x conforming compiler You can use std::unique_ptr.
std::tr1::unique_ptr<int> myFunction() {
std::tr1::unique_ptr<int> result(new int(5));
return result;
}
The main difference is that:
shared_ptr allows multiple instances of shared_ptr pointing to the same RAW pointer. It uses reference counting mechanism to ensure that memory will not be freed as long as at least one instance of shared_ptr exist.
unique_ptr allows only one instance of it holding pointer but have true move semantic unlike auto_ptr.
In C++, you should use new:
int *myFunction()
{
int blah = 4;
return new int(blah);
}
And to get rid of it, use delete:
int main(void)
{
int *myInt = myFunction();
// do stuff
delete myInt;
}
Note that I'm invoking the copy constructor for int while using new, so that the value "4" is copied onto the heap memory. The only way to get a pointer to something on the stack reliably is to copy it onto the heap by invoking new properly.
EDIT: As noted in another answer, you will also need to document that the pointer needs to be freed by the caller later on. Otherwise you might have a memory leak.
There is another approach - declare x static. In this case it will be located in data segment, not on stack, therefore it is available (and persistent) during the program runtime.
int *myFunction(void)
{
static int x = 4;
return &x;
}
Please note that assignment x=4 will be performed only on first call of myFunction:
int *foo = myFunction(); // foo is 4
*foo = 10; // foo is 10
*foo = myFunction(); // foo is 10
NB! Using function-scope static variables isn't tread-safe technique.
Your second code snippet is correct.
To help avoid memory leaks, I let the coding conventions help me.
xxxCreate() will allocate memory for xxx and initialize it.
xxxDelete() will destroy/corrupt xxx and free it.
xxxInit() will initialize xxx (never allocate)
xxxDestroy() will destroy/corrupt xxx (never free)
Additionally, I try to add the code to delete/destroy/free as soon as I add the code to create/init/malloc. It's not perfect, but I find that it helps me differentiate between items that need to be freed and those that don't, as well as reducing the likelihood that I will forget to free something at a later time.
Boost or TR1 shared pointers are generally the way to go. It avoids the copy overhead, and gives you semi-automatic deletion. So your function should look like:
boost::shared_ptr<int> myFunction2()
{
boost::shared_ptr<int> x = new int;
*x = 4;
return x;
}
The other option is just to allow a copy. That's not too bad if the object is small (like this one) or you can arrange to create the object in the return statement. The compiler will usually optimize a copy away if the object is created in the return statement.
I would try something like this:
int myFunction2b( int * px )
{
if( px )
{
*px = 4;
return 1;
}
// Choice 1: Assert or Report Error
// Choice 2: Allocate memory for x. Caller has to be written accordingly.
// My choice is 1
assert( 0 && "Argument is NULL pointer" );
return 0;
}
You're asking how to correctly return a pointer. That's the wrong question, because what you should be doing is using smart pointers rather than raw pointers. scoped_ptr and shared_ptr (available in boost and tr1) are good pointers to look at (e.g. here and here)
If you need the raw pointer for something (e.g. passing to a C function), the get() method will supply it.
If you must create raw pointers, e.g. for homework, then you can use malloc() (as you did) or new within a function, and hope you remember to de-allocate the memory (through free() and delete respectively) Or, in a slightly-less-likely-to-leak idiom, you can create the pointer with new, pass it to a function, and de-allocate with delete when you're done with it. Again, though, use smart pointers.