# c++: memory deallocation (and freeing) not working?

This topic is 2023 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I use std::vector(s) as containers for structs, where I store my data.
Just to make a quick example, I've something like this:

 struct myFolliclesLib { QString follicleGroupName; std::vector<myData::myDataLib> myDataIDs; } ; 

Later I store data into the container:

 std::vector<myFollicles::myFolliclesLib> myFollicle; myFollicle.push_back(myFollicles::myFolliclesLib()); ... myFollicle.at(n).follicleGroupName="something"; myFollicle.at(n).myDataIDs.push_back(someData); 

Everything works ok, data is stored, I perform queries etc. I've a problem only with deleting and freeing memory.

I thought (after googling around) that deleting the container would just delete also all the data stored inside the structs:

 myFollicle.clear(); myFollicle.erase(myFollicle.begin(), myFollicle.end()); 

Instead, from the Task Manager (I've also another application called Process Explorer that shows memory usage more in detail) the memory is not freed.

So I was wondering wether I need to delete also all the data contained into the structs (and so each vector, array etc.) ? Edited by Alessandro

##### Share on other sites

Instead, from the Task Manager (I've also another application called Process Explorer that shows memory usage more in detail) the memory is not freed.

How much memory are you actually allocating? Memory is allocated in rather large pages, so freeing juste a few kB of memory may not actually cause any page to get deallocated.

It is my understanding that vectors will only free memory allocated to storing the structs themselves. If the structs contain any reference to other data (such as pointers to objects), the data will still be there - only the memory allocated to storing the reference will have been freed. So if you have anything other than primitive types (like ints, etc...) in your struct, you need to explicitly free the objects they point to since they are actually references. Edited by Bacterius

##### Share on other sites
Vectors keep their claim to the memory they've allocated, even after you've cleared it's content. It destructs all the elements, but it does not deallocate them. This is a precaution for when you re-use the vector. If it's grown to a certain size once, it's a good chance it will again. That's how they reason anyway..

If you really want to get rid of the allocated memory, replace the vector with a new empty one to force it. C++11 has included the vector.shrink_to_fit() method, but it's a request that may not be fulfilled by the implementation.

 myFollicle = std::vector<myFollicles::myFolliclesLib>(); 

You only need to explicitly free raw pointers! Never call the destructor or a delete on an object in a container. Vector, when clearing, removing or erasing, will call the appropriate destructor (not delete) on the objects it contains. If this is a raw pointer, the actual object it points to will not be deleted unless you call delete explicitly. It's however recommended to use a unique_ptr in this case rather than a raw pointer. Edited by Zoomulator

##### Share on other sites
Just to be clear: the containers in the C++ standard library by default use a "pooled allocation strategy" for their memory, which means they do not relinquish memory they have allocated but instead return it to a pool for future use. The std::vector, in particular, does not shrink when elements are removed.

The standard library containers use copy semantics. That means that when you insert an object into the container, a new copy of the object is created, and when you remove it from the container, that copy is destroyed. If the container holds a pointer object, the pointer is destroyed, not the object it was pointing to (unless the pointer object is a smart pointer object and the conditions are appropriate).

Memory allocation in the C runtime is not as simple as most beginners seem to believe. Aside from the pooled allocation strategy used by the C++ standard library containers, the runtime itself uses a pooled allocation strategy in which the C library calls (malloc, etc) request large chunks of memory from the underlying OS an doles it out from its arean in order to satisfy requests. It is very rare that such memory is ever returned to the underlying OS. Instead, when freed, it just gest reused again, only growing as needed. The default C++ [font=courier new,courier,monospace]::operator new[/font] is written 'as if' it uses malloc(), so it, too uses a memory allocation pool.

Using a crude tool like the Task Manager to watch how your application uses memory is more likely to mislead you than to inform you except in very blatantly obvious cases.

I suspect that in the OP's case, everything is in fact working correctly.

##### Share on other sites
The memory allocated is about 400MB, so that means that each time that I run the routine that allocates data, "costs" about 400MB of memory.
I know that Task Manager is misleading but then I can one be sure that memory is freed?
From one of your suggestions (Zoomulator) it seems that rather using .clear() and .erase(), I should just a new empty vector to do the job. Just did that but again the Task Manager still adds to memory usage.
So I'm a bit confused. Is calling a new empty vector really freeing memory? Can't use shrink_to_fit as it is unsupported in XCode.

##### Share on other sites
Task Manager is not a reliable way to see how much memory your process is using. Have you verified that you are in fact leaking memory using a proper leak detection tool, or just Task Manager?

##### Share on other sites

Vectors keep their claim to the memory they've allocated, even after you've cleared it's content. It destructs all the elements, but it does not deallocate them. This is a precaution for when you re-use the vector. If it's grown to a certain size once, it's a good chance it will again. That's how they reason anyway..

If you really want to get rid of the allocated memory, replace the vector with a new empty one to force it. C++11 has included the vector.shrink_to_fit() method, but it's a request that may not be fulfilled by the implementation.

 myFollicle = std::vector<myFollicles::myFolliclesLib>(); 

You only need to explicitly free raw pointers! Never call the destructor or a delete on an object in a container. Vector, when clearing, removing or erasing, will call the appropriate destructor (not delete) on the objects it contains. If this is a raw pointer, the actual object it points to will not be deleted unless you call delete explicitly. It's however recommended to use a unique_ptr in this case rather than a raw pointer.

Hi, thanks for this suggestion, searching around it seems it's the common way to do it.
But how'd you use it in a situation where you want to deallocate a vector inside another container vector?

See this example below:

 // this is the struct that contains a vector with some data struct myShaveGroupsLib { QString shaveGroupName; std::vector<myFollicles::myFolliclesLib> myFollicle; } std::vector<myShaveGroups::myShaveGroupsLib> myShaveGroup; // a vector that points to the above struct for (int g=0; g<MAX_G; g++) { myShaveGroup.at(currentShaveGroup).myFollicle.push_back(myFollicles::myFolliclesLib()); // fill vectors with data } 

Now, from what I understood I might simply call the following line to deallocate the memory held by the container (myShaveGroup) and all the data inside it (and so also the struct data):

 std::vector<myShaveGroups::myShaveGroupsLib> myShaveGroup; 

But what if I'd like just to deallocate the memory held by the 'myFollicle' vector? Is that possible somehow?

I've learned a little bit of C++ by myself and I find memory allocation/deallocation so hard to deal with.
Thanks for any help!

##### Share on other sites

But what if I'd like just to deallocate the memory held by the 'myFollicle' vector? Is that possible somehow?

I've learned a little bit of C++ by myself and I find memory allocation/deallocation so hard to deal with.
Thanks for any help!

 std::vector<myFollicles::myFolliclesLib>().swap(myShaveGroupsLib.myFollicle); 

Ideomatic way to deallocate a vector, swaps the internal contents with a temporary which then immediately goes out of scope at end of expression, freeing the contents.

Convince yourself with proper profiling that you need this sort of stuff before you waste time on it though. Standard library is good at general use of memory stuff. Normally only specific domains require special handling.