0

I am new with C++. I use Visual Studio. My debug build works as intended, but the release build crashes with Access violation. The struct:

struct GeneticData
{
    int NumberOfIdDataPairs;
    char** IdBuffer;
    char** DataBuffer;
};

IdBuffer is an array of char arrays of 20 chars, Databuffer is an array of chars array of 102 chars. The function which returns the pointer:

GeneticData* LoadData(FILE* dataFilePtr) //dataFilePtr is valid in the function
{
    char* result = NULL;
    int fileLine = 0;
    GeneticData geneticData = *new GeneticData();

    geneticData.NumberOfIdDataPairs = *new int;
    geneticData.IdBuffer = new char*[gSizeOfBuffer];
    geneticData.DataBuffer = new char*[gSizeOfBuffer];

    for (int i = 0; i < gSizeOfBuffer; i++)
    {
        geneticData.IdBuffer[i] = new char[gSizeOfId];
        geneticData.DataBuffer[i] = new char[gSizeOfData];
    }

    do
    {
        result = fgets(geneticData.IdBuffer[fileLine], gSizeOfId, dataFilePtr);
        fgets(geneticData.DataBuffer[fileLine], gSizeOfData, dataFilePtr);
        fileLine++;
    } while (result != NULL && fileLine < gSizeOfBuffer);

    if (result != NULL)
        geneticData.NumberOfIdDataPairs = fileLine;
    else
        geneticData.NumberOfIdDataPairs = fileLine - 1;

    return &geneticData;
}

The gSizeofBuffer is the number of id/data pairs, it's value is 1.000.000 at the moment. Inside the function everything is all right, I can acces the values of the id/dataBuffer. The issue arises in the function which calls my LoadData function.

GeneticData geneticData;
geneticData = *LoadData(dataFilePtr);
//acces violation, geneticData contained invalid values

With cout << geneticData.DataBuffer[1] the debugger stopped here (post mortem debugging): enter image description here

With cout << geneticData.DataBuffer[1][0] the debugger stopped at this line. In the actual algorithm which analyses the content of DataBuffer the debugger stops like in the case of cout << geneticData.DataBuffer[1][0]. I allocated the memory of the GeneticData struct as dynamic memory, I do not understand why is the pointer invalid after the LoadData function returns it.

Arszi
  • 27
  • 10

3 Answers3

6
GeneticData geneticData = *new GeneticData();

This monstrosity dynamically allocates an object, uses that to copy-initialise a local variable, and then throws away the only pointer to it, causing a memory leak.

return &geneticData;

This returns a pointer to a local variable, which is destroyed before the caller can do anything with it; leading to your error.

Perhaps you want to return the dynamic object (remembering to delete it when you've finished using it):

GeneticData * geneticData = new GeneticData();
//...
return geneticData;

or perhaps you want to return an object by value:

GeneticData LoadData(FILE* dataFilePtr)
{
    GeneticData geneticData;
    //...
    return geneticData;
}

but you should do yourself a favour and stop messing around with pointers and new. Use std::vector for the buffers.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
5

Your're returning a pointer to a temporary structure:

GeneticData geneticData = *new GeneticData(); // memoryleak: copies from dynamic memory into local
...
return &geneticData; // returns address of local memory

If you want to initialise a local structure write:

GeneticData geneticData; // calls the default constructor GeneticData() to initialise geneticData

Note that geneticData is only valid until the end of scope and so is an address to that variable!

If you need dynamic memory write:

GeneticData* pGeneticData = new GeneticData(); // allocates dynamic memory (new), calls the default constructor and assigns the memory address to the pointer pGeneticData
BeyelerStudios
  • 4,243
  • 19
  • 38
2

You have to replace

GeneticData geneticData = *new GeneticData();

by

GeneticData* geneticData = new GeneticData(); // we define a pointer now

and

return &geneticData;

by

return geneticData; // return the pointer

But try to avoid using raw pointers unless you really know what you're doing.

vsoftco
  • 55,410
  • 12
  • 139
  • 252