Pointer to a struct (with pointers) *** glibc detected *** double free

I am using a structure defined as follows

struct gene_square
{
    double *x;
    double *y;
};

I have class, with a member function which is a pointer of this type:

gene_square* m_Genes;

I am allocating memory in the constructors like this:

m_Genes = new gene_square[square_genome_size];
    for (ii=0; ii<square_genome_size; ii++)
    {
        (m_Genes+ii)->x = new double[POINTS];
        (m_Genes+ii)->y = new double[POINTS];
    }

And my destructor does this:

if(m_Genes != NULL)
    {
        for(ii=0; ii<square_genome_size; ii++)
        {
            if (((m_Genes+ii)->x)!=NULL)
            {
                delete [] (m_Genes+ii)->x;
            }
            if (((m_Genes+ii)->y)!= NULL)
            {    
                delete [] (m_Genes+ii)->y;
            }
        }
        delete [] m_Genes;
    }

But when the destructor is called I am getting a glibc double free error.

*** glibc detected *** ./Evolution: double free or corruption (fasttop): 0x08055008 ***

And I can't work it out.

Anyone able to help?

I think the code looks okay (but I'm not a C++ expert). It could be that x and y no longer hold the original reference to your allocated arrays. In that case, the pointers might point somewhere they did not originally. Try this: after instantiating the m_Genes object, create a "shallow copy" of it, then before you distruct, compare the shallow copy with the m_Genes copy. If there's a difference in pointer values, that might be your problem.

Another possibility just occurred to me: It could be that you made a shallow copy of m_Genes and deleted the shallow copy as if it were a deep copy. That is, you already freed the pointers in m_Genes->x and ->y.