Bypass override of operator new in C++ -
is there way achieve bypass of override of operator new?
something this:
void* ::operator new( std::size_t size ) { void *p = ( ::operator new( size ) ); // original, _not_ infinite recursion // stuff p return p; } background: have legacy code, switched compile visual studio 2012. random crashes when malloc fails _heap_alloc sufficient memory blocks. (yes, code sprinkled small memory leaks , other bad behaviour over. unfortunately thorough cleanup not realistic, somewhere around 500 000 sloc.)
my current theory cause source files include header following overrides of operator new:
void* ::operator new( std::size_t size ) { void* p = malloc( size ); if( p == null ) throw; // set memory 0 memset( p, 0, size ); return p; }; void* ::operator new[]( size_t count ) throw(std::bad_alloc) { // try allocate count bytes array return (operator new(count)); } there no overrides of delete.
in essence, means application mixes allocation using malloc deallocation using delete instead of free.
first try q&d fix: introduce overrides of delete uses free. partly helps, because include order , linked libraries still messed up.
second try q&d fix: remove override. initialization of memory 0 unfortunately necessary. heritage old compiler used, did that, , coders assumed c++ that.
i aware new() take care of that, unfortunately not aware of method make use of without manually going through source code , updating it. won't poorly implemented classes assumes members nullified without doing in constructor.
therefore, third idea of q&d fix: using normal new in override, question asks about.
is there way achieve bypass of override of operator new?
none i'm aware of, why don't re-implement in custom function in same manner ms did in _heap_alloc-based implementation , add customization it?
one do-or-die allocation attempt anyway not ms does:
http://msdn.microsoft.com/en-us/library/vstudio/we2zys4d.aspx
the default behavior execute loop. within loop, function first attempts allocate requested storage
anyway- second n.m. put in comment
you need fix real memory bugs
that self-explaning; debugging purposes - pls have @ std::set_new_handler.
btw - there things find irritating code
- the explicit qualifications of operator
- not throwing std::bad_alloc re-throwing outside of catch-block
happy hacking!
regards, s.
p.s.:@all: cannot "vote up" question due limited exp here, recommend doing that. it's interesting topic , formulated.
Comments
Post a Comment