tags:

views:

2211

answers:

7

Is it possible to create macros to replace all forms of operator new with overloads that include additional args...say __FILE__ and __LINE__?

The trouble appears to be that operator new can either be coded with or without parentheses, therefore:

  • object-like macros:

    #define new new(__FILE__, __LINE__)
    

    will replace declarations like:

    A* a = new A();
    
  • and function-like macros:

    #define new(A) new (A, __FILE__, __LINE__)
    

    will replace declarations like:

    A* a = new(std::nothrow) A();
    

Unfortunately it's an error to attempt to declare two macros with the same identifier, even if they are of different types, so the following fails:

#define new new(__FILE__, __LINE__)
#define new(A) new (A, __FILE__, __LINE__) // Error: "new" already defined

Since I'm using g++ I was hopeful that employing their syntax of variadic macros would yield success, but unfortunately not. The following:

#define new(...) new(__FILE__, __LINE__, ## __VA_ARGS__)

only matches new(xyx) A(), not new A().

I know that essays have been written about why it is impossible, but I feel like I'm so close that there must be a way. Is there anything obvious that I'm missing?

A: 

No, there's no way.

You could do this in the bad old days of malloc()/free() but not for new.

You can replace the memory allocator by globally overriding the new operator, but you cannot inject the special variables you're talking about.

Jason Cohen
You can do it, I've done it last year. Wasn't tricky or anything.
Robert Gould
@Robert: Could you provide any details?
David Citron
+5  A: 

You should check out this excellent blog entry by my coworker Calvin. We had a situation recently where we wanted to enable this type of fix in order to associate memory leaks with the line that allocated them in diagnostic/debug builds. It's an interesting trick

http://blogs.msdn.com/calvin_hsia/archive/2009/01/19/9341632.aspx

JaredPar
That technique doesn't actually show the file/line whence the new was called. When __FILE__ and __LINE__ are passed as default function parameters, you always get the file/line at which the function is declared. Even his sample output shows this.
David Citron
+1  A: 

You don't say what compiler you are using, but at least with GCC, you can override new and log the caller address, then later translate that to file/line information with addr2line (or use the BFD library to do that immediately).

TrayMan
Oops, sorry. You did say that you are using g++. So there you go.
TrayMan
Unfortunately addr2line requires debugging symbols to work. Our executables are typically compiled with -O2 and without -g, so I don't think this would work. Please correct me if I'm wrong.
David Citron
You can have -g flag with -O2 flag. Also, unless you specify the -s flag, there's going to be some information, like which function and which source file an address belongs to (if I recall right).
TrayMan
Unfortunately -O2 collapses (optimizes away) some function calls so the call stack does not match the source.
David Citron
+2  A: 

3.7.4 Dynamic storage duration

2 The library provides default definitions for the global allocation and deallocation functions. Some global allocation and deallocation functions are replaceable (18.5.1). A C++ program shall provide at most one definition of a replaceable allocation or deallocation function. Any such function definition replaces the default version provided in the library (17.6.4.6) [...]

17.6.4.6 Replacement functions

  1. A C++ program may provide the definition for any of eight dynamic memory allocation function signatures declared in header (3.7.4, Clause 18):

    • operator new(std::size_t)
    • operator new(std::size_t, const std::nothrow_t&)
    • operator new[](std::size_t)
    • operator new[](std::size_t, const std::nothrow_t&)
    • operator delete(void*)
    • operator delete(void*, const std::nothrow_t&)
    • operator delete[](void*)
    • operator delete[](void*, const std::nothrow_t&)

Hope this clarifies what is a legal overload and what isn't.

This may be of interest to a few here:

#define delete cout <<  "delete called at: " << __LINE__ << " of " << __FILE__  << endl, delete 

using namespace std;

void *operator new(size_t size, ostream& o, char *f, unsigned l) {
    o << "new called at: " << l << " of " << f << endl;
    return ::new char[size];
}

int main() {
    int *a = new(cout, __FILE__, __LINE__) int;
    delete a;
}

Caveat Lector: What I do here is a Bad Thing (TM) to do -- overloading new/delete globally.

dirkgently
delete[] ??
Learner
Why is overloading new globally a bad thing? Our entire architecture depends on it; it's the only way to force everything, including crappy third party code, to go through our pooled allocators.
Crashworks
I like your idea about using the comma operator with the #define. It's not exactly what the OP wanted, but it could be a workaround.
strager
Wouldn't your marco expand recursively, or am I missing something important about the comma?
Chris Lutz
+1  A: 

I found the following library "nvwa" very useful for tracking down new/delete memory leaks - have a look at the file "debug_new" for examples, or just use it 'as is'.

James Fisher
Wow--that's a great find. I'm not sure if that's using operator overloading for good or for evil, but it definitely appears to work!
David Citron
Hmmm...the macro replacement is generating bogus output on some 3rd-party code that I can't change that explicitly invokes "::operator new" (the "::operator" is remaining, of course, and is making it bogus). Any ideas?
David Citron
+1  A: 

What you could do is to overload the operator new and get the stack trace there (platform specific) and use the stack information to deduce from where new was called.

lothar
This answer would be better if it included a link to http://www.gnu.org/software/hello/manual/libc/Backtraces.html -- however, run-time performance impact is an issue and compile-time optimizations collapse function calls making the backtrace inaccurate.
David Citron
+2  A: 

Here is what I use:

In new.c++

const char* __file__ = "unknown";
size_t __line__ = 0;

void* operator new(size_t size) {
    void *ptr = malloc(size);
    record_alloc(ptr,__file__,__line__);
    __file__ = "unknown";
    __line__ = 0;
    return ptr;
}

void delete(void *ptr)
{
   unrecord_alloc(ptr);
   free(ptr);
}

For compactness, I'm leaving out the other definitions of new and delete. "record_alloc" and "unrecord_alloc" are functions that maintain a linked list of structure containing ptr, line, and file).

in new.h++

extern const char* __file__;
extern size_t __line__;
#define new (__file__=__FILE__,__line__=__LINE__) && 0 ? NULL : new

For g++, "new" is expanded only once. The key is the "&& 0" which make the it false and causes the real new to be use. For example,

char *str = new char[100];

is expanded by the preprocessor to

char *str = (__file__="somefile.c",__line__=some_number) && 0 ? NULL : new char [100];

Thus file and line number are recorded and you custom new function is called.

This works for any form of new -- as long as there is a corresponding form in new.c++

strager
Johannes Schaub - litb
I like this solution for debug builds. It isn't thread safe, but a bit of thread local storage fixes that. I think that would make it a bit costly for a release build ... but there are never any leaks in release builds anyway <grin>
Mark Wilkins
very very smartIf I think again you could do something like:#define new PrepareNew(__FILE__,__LINE__, other ... parameters); new#define delete PrepareDelete( .... ); deleteYou could implement this to be thread safe without any hassle
Iulian Şerbănoiu