• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Alessandro

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

7 posts in this topic

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:

[CODE]
struct myFolliclesLib {
QString follicleGroupName;
std::vector<myData::myDataLib> myDataIDs;
} ;
[/CODE]

Later I store data into the container:

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

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:

[CODE]
myFollicle.clear();
myFollicle.erase(myFollicle.begin(), myFollicle.end());
[/CODE]

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
0

Share this post


Link to post
Share on other sites
[quote name='Alessandro' timestamp='1341314254' post='4955241']
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.
[/quote]
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 [i]any[/i] 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
0

Share this post


Link to post
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.

[CODE]
myFollicle = std::vector<myFollicles::myFolliclesLib>();
[/CODE]

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
1

Share this post


Link to post
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.
0

Share this post


Link to post
Share on other sites
Thanks for your answers.
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.
0

Share this post


Link to post
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?
0

Share this post


Link to post
Share on other sites
[quote name='Zoomulator' timestamp='1341315813' post='4955246']
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.

[CODE]
myFollicle = std::vector<myFollicles::myFolliclesLib>();
[/CODE]

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.
[/quote]

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:

[CODE]
// 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
}
[/CODE]

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):

[CODE]
std::vector<myShaveGroups::myShaveGroupsLib> myShaveGroup;
[/CODE]

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!
0

Share this post


Link to post
Share on other sites
[quote name='Alessandro' timestamp='1341845951' post='4957299']
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!
[/quote]

[code]
std::vector<myFollicles::myFolliclesLib>().swap(myShaveGroupsLib.myFollicle);
[/code]

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.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0