-1

Short story . . . I am trying to override new and delete operators. Seem to be okay on new but on delete I have a problem with this bit of code

cCellList::~cCellList()
{
  STPINT loop;
  for (loop = 0; loop < count; loop++)
  {
    delete cells[loop];
  }
  free(cells);
}

The delete here is not going to my overridden delete operator, so things are not working out. The stack trace says

ExeName.exe!Cell::'scalar deleting destructor'()
ExeName.exe!cCellList::~cCellList()
ExeName.exe!Cell::'scalar deleting destructor'()

The line of code being executed is

delete cells

where cells is of type cCellList *.

Long story. I have been working on this executable for nearly 20 years, part time, and it has about 14 MB of source code. All unmanaged C++, currently using VS2010. I started out with a compiler named "Think C with Object-oriented extensions". Probably many of you are too young to remember those days.

Someplace in there is a memory management problem which causes strange things to happen sometimes. I have long since passed the point where third-party solutions like Purify can be used on this program. They just bomb when I try to instrument the code. So I have written my own malloc/free and I am hooking these up to keep better track of what is happening to the memory. So far, I am allocating all the memory with my own system, but in this case it is going to the normal "free" instead of mine, with predictable results.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 2
    If you've been coding for >20 years, you should know how to create a minimal test-case. Please create one, then post here, and then we can help you! – Oliver Charlesworth Mar 15 '12 at 00:29
  • See also this question: http://stackoverflow.com/questions/4948339/why-is-a-scalar-deleting-destructor-being-called-as-a-result-of-vector-delete-on. – Oliver Charlesworth Mar 15 '12 at 00:30
  • You've been coding for nearly 20 years, and you're "writing your own malloc/free", and don't know how to work with `operator new` overloads in C++?! – Karl Knechtel Mar 15 '12 at 00:34

2 Answers2

1
free(cells);

Did you allocate the memory with malloc?

If yes, don't do that. Classes do not play nice with malloc. new exists for a reason. There are all kinds of things that can go wrong here.

If no, then don't deallocate it with free. That is undefined behaviour. If you wrote cells = new Cell[n] to allocate, you must use delete[] cells to deallocate. free may NOT be substituted for delete or delete[], and delete may NOT be substituted for delete[], or vice versa in any combination, no matter how much it "seems to work" or who told you it's ok. It is not ok.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
0

What is the declaration of the delete operator?

As a general remark, your overridden malloc()/free() should take the form of overridden global new()/delete() operators, not forgetting to provide the vector versions. Similarly this destructor should call delete[] cells; instead of free(cells), assuming 'cells' was initialized via new cells[X], which it must have been, otherwise nothing would work, and specifically virtual methods.

user207421
  • 305,947
  • 44
  • 307
  • 483