Sign in to follow this  

using free()

This topic is 4732 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

Hello, I'm not sure about something when using free(). I'm programming a little MD2 model loader. I have a structure md2_frame_t like this :
typedef struct
{
   /* ... */
   md2_vertex_t *verts;

} md2_frame_t;
*verts is an array of vertices allocated with malloc(). I have also an array of md2_frame_t objects, allocated with malloc() too. Now to free memory, I do this :
FreeModel( md2_model_t *mdl )
{
    /* free other data */

    for( i = 0; i < mdl->header.num_frames; i++ )
    {
        free( mdl->frames[i].verts );
        mdl->frames[i].verts = NULL;
    }

    free( mdl->frames );
    mdl->frames = NULL;
}
But the last line, mdl->frames = NULL;, makes a segmentation fault. So I would like to known what's wrong in my FreeModel() function?

Share this post


Link to post
Share on other sites
Use free(pointer) where pointer is the same ptr returned by malloc. Can be simpler?
If you allocate a structure and the structure allocate a second array...delete the array first then the structure.

If you use new/delete do not confuse new/delete with new[]/delete[]

Share this post


Link to post
Share on other sites
This is what I'm doing. And I didn't used new[], I'm coding in C.

mdl->frames = (md2_frame_t *)malloc( sizeof( md2_frame_t ) * mdl->header.num_frames );

/* ... */

for( i = 0; i < mdl->header.num_frames; i++ )
{
mdl->frames[i].verts = (md2_vertex_t *)malloc( sizeof( md2_vertex_t ) * mdl->header.num_vertices );

/* ... */
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hmmm, this code seems absolutely correct to me - it's the way i do it in my projects too. Really don't know where the problem is :(

Share this post


Link to post
Share on other sites
Perhaps it comes from elsewhere (but when commenting this last line it works fine...). The full program source code (not very long) is here : http://tfc.duke.free.fr/coding/src/md2.c (and http://tfc.duke.free.fr/coding/src/anorms.h)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Well, I tried to compile your code and test it on some md2's and it worked without any error ... isn't your md2 model somehow corrupted?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just a comment to the reply above: I've tested it on WinXP (I can see you are probably working on Linux) and compiled and debugged it in MS Visual C++ .Net 2003.

Share this post


Link to post
Share on other sites
I've just tried it on an other computeur and it worked without any error too :/ Both Windows and Linux... And when I written this code ten days ago, there was any problem on my own computeur... strange.

Share this post


Link to post
Share on other sites
It's incredible. My shutdown() function is called two times ! I removed the atexit( shutdown ); instruction, and the shutdown() function has been called nevertheless o_O. I renamed the shutdown in “Myshutdown” and all worked fine... But I don't understand why shutdown() is called in my computer but not in another computer (with same libraries).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Look at the glut.h file - it implements some strange behaviour with the exit and atexit functions. Maybe this is the cause of your problem. If your free function has been called twice then it is obvius it will cause a segmentation fault. You should do some testing in FreeModel like this:


if (mdl->frames != NULL)
{
if (mdl->frames[i].verts != NULL)
{
/* ... free the memory ... */
}
}

Share this post


Link to post
Share on other sites

This topic is 4732 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.

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