Simplest way to count instances of an object

后端 未结 8 1916
时光取名叫无心
时光取名叫无心 2021-01-01 17:42

I would like to know the exact number of instances of certain objects allocated at certain point of execution. Mostly for hunting possible memory leaks(I mostly use RAII, al

8条回答
  •  Happy的楠姐
    2021-01-01 18:31

    Better off to use memory profiling & leak detection tools like Valgrind or Rational Purify.

    If you can't and want to implement your own mechanism then,

    You should overload the new and delete operators for your class and then implement the memory diagnostic in them.

    Have a look at this C++ FAQ answer to know how to do that and what precautions you should take.

提交回复
热议问题