Double Free or Corruption error

I keep getting a glibc detected: double free or corruption error. It runs through the whole thing and then error at the break statement. I think it's cause i'm trying to delete something that I already deleted? Not really sure, what do you think?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
     case 3:{
        double (*wspd10)[lay][ROW][COL]=new double [TSTEP][lay][ROW][COL];
        int timeIndex=0;
        //get variables from netcdf file
        NcVar *wspd10Var;
        if (!(wspd10Var=dataFile.get_var("WSPD10")))
        {cout<<"Could not get pointer to NcVar wind speed at 10m requested."
            <<" Common problem: file parameter does not point to a METCRO2D file."<<endl;}
        if (!wspd10Var->get(&wspd10[0][0][0][0],TSTEP,lay,ROW,COL))
            {cout<<"Could not set netcdf wind speed at 10 m variable to class constructor"<<endl;}
        //create data for wind
        for (int tstep=0;tstep<TSTEP;tstep++){
            if (hour_array[tstep]==12){
                for (int lon=0;lon<ROW;lon++){
                    for (int lat=0;lat<COL;lat++){
                        data[timeIndex][lon][lat]=wspd10[tstep][0][lon][lat]*36;
                    }
                }
                timeIndex++;
            }
        }             
        delete [] wspd10;
        loaded=true;
     }
     break;


I posted this not because I want advice on how you (the generic you) would write this code based on one clip of it- I have been doing this awhile now and I'm doing this the most memory efficient way possible (I will eventually have a terabyte of data streaming through this). What I would like help on is the error. Have you gotten it before? what did it mean for you? Also, if you can see why this code is possibly faulting (i.e. trying to delete something that has already been deleted beyond a scope or something along those lines), please let me know.

I did find somewhere that this error means: "the C++ library thinks you did a double free (that is, you freed the same thing twice, which is of course a bug) or that you corrupted its data structures, such as by writing beyond the end of a buffer you allocated."
Last edited on
Accessing wspd10[tstep][0][lon][lat] where tstep > 0 is accessing an uninitialised memory right?

Why don't you use a class to manage this multidimensional nightmare?
this is part of a function to initialize an object "data" in a class. wspd10 is initialized in the get() statment (all part of a netcdf package)
Last edited on
So it is part of the constructor?
no- it (this clip of code) is part of a modification member function, hence the modification to data which is created and initialised in the constructor.
Last edited on
All I can say is that I am glad I don't have to maintain that. Dynamic multi-dimensional arrays give me the heebie-jeebies.
so you don't see anything? Thanks for taking a look! I appreciate all the help anyone offers.
I suspect that you're having a buffer overflow somewhere.
Run the debug version of your program through valgrind.
You'll for ever have problems with that code until you refactor that array thing into something manageable with a nice clean interface.
Solution: used gdb debugger and found a class object (multidimensional array) was being deleted twice.

@Athar- thanks for suggesting something useful.
@kbw- comments like that are particularly unhelpful. In this case, I don't mind because you really have only seen 20 lines of several thousand & I have enough experience to shrug it off. However, to an inexperienced programmer, remarks like that are condescending and unproductive- may I suggest not saying anything at all if you don't have anything constructive to say?
Topic archived. No new replies allowed.