Jump to content
  • Advertisement
Sign in to follow this  
mark ds

Freeing memory, c/c++

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi

I need to delete the contents of 'stuff' (see below), which are a list of opengl extensions, what the right way to go about it?

Here's a simplified example:


class foo
{
public:
char** stuff;
int arraySize;
public:
foo();
~foo();
void deleteArrayContents( char **pStuff, int count )
};

foo::foo()
{
arraySize = 10;

stuff = (char**) malloc( sizeof( char* ) * arraySize ); // allocate an array of char pointers;

...

for( int i = 0; i < arraySize; i++ )
{
stuff = (char*) glGetStringi( GL_EXTENSIONS, i ); // glGetStringi returns a char*
}

deleteArrayContents( stuff, arraySize )
}

foo::deleteArrayContents( char **pStuff, int count )
{
for( int i = 0; i < arraySize; i++ )
{
free( pStuff ); // fails with heap corruption
}
}


But when I try to free each array element individually (in deleteArrayContents), I'm getting heap corruption. Any ideas where I'm going wrong?

Many thanks. Edited by mark ds

Share this post


Link to post
Share on other sites
Advertisement
It would be the correct way if you owned the memory pointed to by the pointers in stuff. However, you don't own the memory returned by glGetString, and thus you must not attempt to release it. Memory management is about ownership; you must realease only whay you own, and if you don't own a piece of memory then someone else is responsible for releasing it. In this case, OpenGL owns the memory and it is responsible for it.

However, you need to release stuff itself, because you allocated it yourself. Edited by Brother Bob

Share this post


Link to post
Share on other sites
That was quick! Thanks

I figured that would be the case, but i was afraid that by reusing the 'stuff' array with new content would result in a memory leak. Would that not be the case?

Share this post


Link to post
Share on other sites
There is only a memory leak if the owner of the memory doesn't release it. You don't own the memory, so you cannot leak it.

edit: And just to be on the safe side again: I'm talking about the pointers in stuff[n] now, not stuff itself. You own stuff, on the other hand, so free(stuff) is necessary at some point when you are done with the array of pointers, but free(stuff[n]) is not something you shall attempt to do. Edited by Brother Bob

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!