简体   繁体   中英

cancel destructor calls in operator delete[]

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.

您可能什么也做不了,因为如果客户端代码调用delete ,则当运算符new为new [] ,您可能不会在delete运算符中收到相同的地址。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM