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: =========



        _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];

        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,

8 Years
Discussion Span
Last Post by skvikas

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

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.


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.