overloading new/delete

狂风中的少年 提交于 2019-11-26 06:39:34

问题


I\'m making a little memory leak finder in my program, but my way of overloading new and delete (and also new[] and delete[]) doesn\'t seem to do anything.

void* operator new (unsigned int size, const char* filename, int line)
{
    void* ptr = new void[size];
    memleakfinder.AddTrack(ptr,size,filename,line);
    return ptr;
}

The way I overloaded new is shown in the code snippet above. I guess it\'s something with the operator returning void* but I do not know what to do about it.


回答1:


void* ptr = new void[size];

Can't do that. Fix it.

Never ever try to overload new/delete globally. Either have them in a base class and derive all your objects from this class or use a namespace or a template allocator parameter. Why, you may ask. Because in case your program is more than a single file and using STL or other libraries you are going to screw up.

Here's a distilled version of new operator from VS2005 new.cpp:

void * operator new(size_t size) _THROW1(_STD bad_alloc)
{       // try to allocate size bytes
   void *p;
   while ((p = malloc(size)) == 0)
    if (_callnewh(size) == 0)
     {       // report no memory
        static const std::bad_alloc nomem;
        _RAISE(nomem);
     }

     return (p);
}



回答2:


Never ever try to overload new/delete globally

Why is it whenever someone tries to use a less common feature of C++, someone acts like it should never be done?

It's done all the time, it is quite common, and I have not worked for a company that did not do this.

Globally overloaded new and delete are extremely helpful in tracking memory, memory bugs, buffer overruns, etc.

Nobody in their right mind is going to go through a program with several million lines of code, and add a new and delete member to each and every class. That's just stupid.




回答3:


Maybe you can do what you want with a little bit of preprocessor magic:

#include <iostream>

using namespace std;

void* operator new (size_t size, const char* filename, int line) {
    void* ptr = new char[size];
    cout << "size = " << size << " filename = " << filename << " line = " << line << endl;
    return ptr;
}

#define new new(__FILE__, __LINE__)

int main() {
    int* x = new int;
}



回答4:


I think the problem here is that your new's parameter profile doesn't match that of the standard operator new, so that one isn't getting hidden (and is thus still being used).

Your parameter profiles for new and delete need to look like this:

void* operator new(size_t);
void operator delete(void*, size_t);



回答5:


Are you invoking the overloaded operator correctly, i.e., passing it the additional parameters?




回答6:


The problem relies with the two arguments that you have added to the overloaded new operator. Try making filename and line global in some way (or member variables if you're overloading new and delete for a single class). That should work better.



来源:https://stackoverflow.com/questions/583003/overloading-new-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!