Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Zeke

Memory leaked due to reassignment help

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

I have a problem in my mfc app in that a particular function is reported by BoundsChecker to be leaking memory. Here is the function:
        
//FF_FILE_INFO is a struct that contains cstrings and a char*


bool FastFileCreate(FF_FILE_INFO *Data, 
                    int NumItems, 
                    int State, 
                    CString Path)
{
    //comparestruct just compares a certain CString member

    //of FF_FILE_INFO

    qsort(Data,NumItems,sizeof(FF_FILE_INFO),comparestruct);

    char* DataPtr=NULL;

    FILE * fpDst = fopen( Path, "wb" );

    for (int intFileName = 0; intFileName < NumItems;     
         intFileName++ ) 
    {
        //Data[intFileName].Data;//is the char* of FF_FILE_INFO

        //it has been created(allocated) with new


        DataPtr=Data[intFileName].Data;//PROBLEM


        //here I fwrite the data pointed to by DataPtr to disk


    }

    fclose( fpDst );

    return true;
}

    
Now the problem boundschecker has is with the line that ends //PROBLEM. I can see why it is saying there is a memory leak here because I have a pointer to memory allocated with new and then without deleting that memory i reassign it to point to a different block of memory. The problem I have with what BoundsChecker is saying is that I still have a pointer to each block of memory in my array of FF_FILE_INFO that is passed into the function and after the function returns I delete[] it like so:
  

FastFileCreate(Data, 20, 3, Path);//above function


for (int x=0;x<20;x++)
{
    delete[]Data[x].Data;
}
delete[]Data;
      
So i am actually deleting the data that boudschecker thinks is causing a memory leak. when in the function i do DataPtr=Data[intFileName].Data, I am not copying the stuff that is in Data[intFileName].Data, but just getting another pointer to it, write it out and then assign the pointer to the next Data[intFileName].Data. So I just dont see how this can be causing a memory leak and yet when i run it if there are a lot of FF_FILE_INFO's in the array Windows will eventually tell me I am out of memory. I probably havent explained this very well but if anyone could help me to figure out where I have gone wrong I would be most appreciative. Thanks Edited by - Zeke on September 20, 2001 7:19:43 AM Edited by - Zeke on September 20, 2001 7:20:45 AM Edited by - Zeke on September 20, 2001 7:21:09 AM

Share this post


Link to post
Share on other sites
Advertisement
That should not be causing a false leak report. I suspect that there is a real leak somewhere.

Anyway, try

1. Setting the DataPtr to 0 when it exits and also when you delete the ptr outside the function.

Also, Why declare the Data_Ptr outside the loop? Do this
  
for (int iFileName = 0;...)
{
char *DataPtr = Data[iFileName].Data

DataPtr = 0;
}


Better still, why use pointers? Use references when getting an instance to the Data.

But I suspect that will not solve the problem but it''s worth a try.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!