0

I am getting the runtime error

malloc: *** error for object 0x10070c5c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

with the following stack trace:

enter image description here

This tells me that something goes wrong in the destructors ~Clustering and ~NodeMap (the first one is an empty stub, the second one calls delete[]), but not which the "pointer being freed" is. How can I find this out?

Also, where is malloc_error_break?

clstaudt
  • 21,436
  • 45
  • 156
  • 239
  • 3
    Run it under valgrind or any such memory leak/corruption detection tool. – Alok Save Jan 08 '13 at 16:19
  • 2
    Read about the "[Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three)." – Robᵩ Jan 08 '13 at 16:20
  • 1
    "*This tells me that something goes wrong in the destructors `~Clustering` and `~NodeMap` (which are empty stubs)*". I don't think they are both empty. I'm pretty sure that that `~NodeMap()` calls `delete[]`. – Robᵩ Jan 08 '13 at 16:22
  • @Robᵩ You're right, I corrected this in the question. – clstaudt Jan 08 '13 at 16:24
  • @cls what is it calling `delete[]` on? It seems that that pointer isn't valid? Was it initialized to `NULL` and/or set to `new[]`. – JaredC Jan 08 '13 at 16:29
  • @JaredC on an array of `int64_t`. it was not set to `NULL` but allocated with `new T[n]`. – clstaudt Jan 08 '13 at 16:33

2 Answers2

3

Do you have a copy constructor that copies allocated memory? If there isn't a copy constructor or if it is but doesn't copy allocated memory then destructors of two objects try to free the same memory locations.

tcb
  • 2,745
  • 21
  • 20
2

This tells me that something goes wrong in the destructors ~Clustering and ~NodeMap (the first one is an empty stub, the second one calls delete[]), but not which the "pointer being freed" is.

Just because ~Clustering is "an empty stub" doesn't mean its doesn't do anything. After executing the body of the destructor, the destructor calls the destructors for each direct non-variant non-static data member. Apparently your class Clustering contains a data member of type NodeMap<long,long>.

How can I find this out?

How many allocated arrays does your class NodeMap<long,long> contain? If it's only one, that's the one. If there are more than one, set a breakpoint in ~NodeMap so you can step through the execution.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • `Clustering` derives from `NodeMap`. The array is indeed the source of the error, and I believe I have to implement a correct copy constructor to fix this. – clstaudt Jan 08 '13 at 16:37