Hi All ,
I am facing some error in destructor in my class. Below is the constructor and destructor. Constructor seems to work fine . While the class get destructed it give the following error


*** glibc detected *** ./main: double free or corruption (fasttop): 0x08743cd0 ***
======= Backtrace: =========
/lib/libc.so.6[0xa44b16]
/lib/libc.so.6(cfree+0x90)[0xa48070]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x3fb731]
./main(__gxx_personality_v0+0x2e4)[0x8048984]

AND LOT OF MEMORY LOCATION

ChainMesh::ChainMesh(){

        _3DarrayOfSet= new ParticleSet **[_number_of_dev];
        for(int i=0;i<(int)_number_of_dev;i++)
                _3DarrayOfSet[i]= new ParticleSet *[_number_of_dev];

        for(int i=0;i<(int)_number_of_dev;i++)
                for(int j=0;j<(int)_number_of_dev;j++)
                        _3DarrayOfSet[i][j] = new ParticleSet[_number_of_dev];
}

ChainMesh::~ChainMesh(){
        for(int i=0;i<(int)_number_of_dev;i++){
                for(int j=0;j<(int)_number_of_dev;j++){
                        delete [] _3DarrayOfSet[i][j];
                }
        }

        for(int i=0;i<(int)_number_of_dev;i++)
                delete [] _3DarrayOfSet[i];

        delete [] _3DarrayOfSet;
}

Can any one point out any thing i am doing wrong . I am just trying to make a three dimentional array of ParticleSet object and cleaning it up after i am done.

Thanks in advance,
Shailendra

Recommended Answers

All 5 Replies

Hmmm i think your first (nested) delete loop should not have the [] operator after the delete line. The way I understand it, there at [j] the thing being deleted is no longer an array. So:

delete _3DarrayOfSet[j];

should suffice.

If I'm wrong, and _3DarraOfSet[j] is in fact a pointer to an array, then this will not work. If so, please post the .h implementation for this class so I can further understand the datatypes.

There doesn't seem to be anything wrong, so it's most likely somewhere else in your code.

You could try breakpoint in the dtor, and check that _number_of_dev is what you expect. Oh, and by the way, names beginning with _ are reserved (with few exceptions).

Memory corruption problems have a "cause" and an "effect", which can be separated in the code by a long way (in time, and in location in the code). Looking at where it crashes is seldom that informative. For example, if you try this code (and ONLY this code) in a standalone program, it'll probably work. If it does work, it's a pretty solid indication that the problem is elsewhere.

Since you seem to be on Linux, then I suggest
g++ prog.cpp -lefence
gdb a.out
run

Electric fence is a memory diagnostic library which will trap on the instruction which corrupts memory (and drop you in the debugger). As opposed to your present situation of where the code notices there is a problem and drops you back to the shell.

I don't see anything wrong with the way you are allocating and de-allocating memory for the pointers.
It works fine in a standalone program.

Your error says that the free is being called twice on a specific pointer. You might want to put some print statements in your destructor to see if its being called more than one for an object. And to narrow down the point to where it throws the error.

You must define overloaded assignment operator and copy constructor for the class ChainMesh. Default assignment and copy constructor are not valid for this class. If you assign this class variables or pass them as arguments then you will deallocate memory twice with default operator= and ctor...
Also check up the ParticleSet destructor (it's called by ChainMesh destructor).

thanks for all the suggestion... Indeed the problem was there in some other place . I was creading a local object and assigning it to a collection and was calling the destructor twice.

thanks,
Shailendra

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.