Memory leaked due to reassignment help

Started by
0 comments, last by Zeke 22 years, 7 months ago
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
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
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.

This topic is closed to new replies.

Advertisement