29

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
  • 10
    "doesn't seem to do anything": what do you mean? You overloaded the operator, but you haven't overridden the default operator new, so "new x" calls the default new while "new (filename, line) x" calls your overloaded version. Feb 24, 2009 at 21:22

4 Answers 4

61

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
  • 4
    should it be size_t instead of unsigned int? I believe on some platforms these types are not equivalent.
    – iggy
    Mar 30, 2015 at 17:16
  • Why void* operator new (size_t size, const char* filename, int line)'s 1st argument is size_t? If I want to override delete operator, what should I write?
    – naive231
    Sep 13, 2017 at 11:57
  • 1
    @naive231: new always takes a size as its 1st parameter. It receives the byte size of the data that is being allocated (in this example, sizeof(int)). Anything extra passed to new is passed in secondary parameters Mar 22, 2018 at 19:02
  • 1
    Doesn't this torch placement new? Nov 18, 2018 at 14:48
13
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);
}
7
  • 8
    Actually, I had a good reason to do that once. We had a compiler "issue" where it was using heaps in an unsafe way across DLL boundries. The fix was to create a custom version of "new" that used a specific named heap.
    – T.E.D.
    Feb 24, 2009 at 18:50
  • 2
    Why did you not file a bug and instead chose to play with fire?
    – dirkgently
    Feb 24, 2009 at 18:52
  • 2
    We did. The response we got back was something along the lines of "DLLs don't work very well cross-process in our system". We eventually quit using them, but in the short term this fixed the problem.
    – T.E.D.
    Feb 24, 2009 at 20:33
  • 7
    There will be no problems with libraries and everything else if you will not try to deallocate memory that wasn't allocated by you (and vice versa). And STL can use custom memory allocators. We use custom new/delete operators in debug builds for long time and have no problems.
    – n0rd
    Feb 25, 2009 at 8:23
  • 8
    "Why did you not file a bug and instead chose to play with fire?" If you ever had to work with a vendor you would know the answer to that question. Some are better than others, some would just say "thanks for the bug report - it will be fixed in the next version - oh by the way your license doesn't cover the next version..." Some may even go so far as to say "it's not a bug, it's a feature" as was stated by @T.E.D. Sep 14, 2013 at 1:59
13

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);
4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.