Question

I'm writing a memory leak detector that creates a text file with some warnings. For example, it generates a warning when delete is used instead of delete[] to free the memory allocated using new[].

What I find difficult is the same thing vice versa, when using delete[] on an object created with new. In that case, the destructor is called for a number of objects that have never been constructed.

Here's something from the c++ reference:

... delete[] is an operator with a very specific behavior: An expression with the delete[] operator, first calls the appropriate destructors for each element in the array (if these are of a class type) ...

My question is, can I do something about it? Calling the destructor for objects that haven't actually been created often causes a segfault. I'd like to somehow "catch" it, cancel the deletion of an array and create a warning.

EDIT: Seems there's not much I can do. Checked another board and got the advice to use a memory allocator, which is overkill for my memory leak detector, and/or look into how valgrind operates. I might do the latter.

Was it helpful?

Solution

You probably can't do anything, because if the client code calls delete, when the operator new was new [], you probably won't receive the same address in the delete operator.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top