Double free or corruption when trying to clear memory in c++

I'm writing a function that frees an array of pointers, but when I invoke the function, am getting the runtime error 'double free or corruption (fasttop)'. The purpose of the function is to clear out the array, which gets its data from an input file, if the file is already open and the fillArray function is called again.

Here is the function that is causing issues:

void freeArray(Country ** g_countryArray)
    for (int i = 0; i < g_arrsz; ++i)
        delete [] g_countryArray;

If it's called anywhere in the program, as far as I can tell, it causes the program to crash. I've run the debugger but am not experienced enough with it to understand the output very much. I can post information from that if it would help.

You left out the [i] in your loop body. You need to drop the [] if the individual pointers are to single Country objects.

You might also want to delete[] the overall array; that depends on the rest of your code.

Change delete [] g_countryArray; to delete g_countryArray[i];.

delete [] arr deletes the whole array at once.

0 Comment



Captcha image