compiler errors with the new operator
compiler errors with the new operator
hi,
i'm using visual c++ 2003.
i've got the following compiler error:
error c2660: 'odrxobject

erator new' : function does not take 3 arguments.
i tried to use the using operator new, but have this:
error c2143: syntax error : missing ';' before '('
error c2238: unexpected token(s) preceding ';'
what should be the exact stuff to write?
for example this is the code producing the first error:
code:
class caoimodeldrawable : public odgsbasemodeldrawable
{
public:
caoimodeldrawable(odgsbasemodel* pmodel, odgidrawable* punderlayingdrawable)
: odgsbasemodeldrawable(pmodel, punderlayingdrawable) {}
private:
virtual bool valid() const
{ return true; }
virtual void invalidate(odgsmodel::invalidationhint = odgsmodel::kinvalidateall)
{}
};
//and the line producing the code:
odgsbasemodeldrawable* mymodel::createnewwrapperobject(odgidrawable* punderlayingdrawable)
{
return new caoimodeldrawable(this, punderlayingdrawable);
}
thanks in advance for any help.
regards
chudomir
this error seems to be caused by the macro:
#ifdef _debug
#define new debug_new
#endif
which makes the new operator being a diagnostic one.
when i turn off this macro, then the code is compiled.
but i think thus i lose the memory leak detection.
is there a way to combine them both?
thanks in advance for any help
regards
chudomir
workaround
with regards to this problem (which i had as well), i used the following workaround (prompted by a suggestion somewhere else in the forums):
code:
#ifdef _debug
#undef this_file
static char this_file[]=__file__;
#define new ::debug_new
#endif
instead of:
code:
#ifdef _debug
#undef this_file
static char this_file[]=__file__;
#define new debug_new
#endif
this forces all instances of "new" in the target module to explicitly use the global new operator which (if you are getting this error) has been overloaded to include the line number and filename of the allocation context.
note: this basically will override any implicit "new" behavior, which may be undesirable in some custom allocation circumstances. for my part, i was just turning this on to track some memory leaks.
quote:
originally posted by chudo
this error seems to be caused by the macro:
#ifdef _debug
#define new debug_new
#endif
ok...so it compiles okay...but you will likely need to add a
code:
#define delete ::delete
as well... otherwise your deallocate will be "exceptional" in a bad way.
after having gone down this path a bit further, there are further complications, not the least of which are that mfc classes (cobject-derived) use their own new/delete operators, so if you override a creation/deletion that you control, and a creation/deletion occurs that you do not control, then you are hosed. the short story is, this may not get you far...
a better solution...
with a bit more delving, i have come across a better solution still. in odheap.h, there are macros for adding class-specific implementations of the heap operators (odrx_heap_operators() macro). although it is too late for any well-defined classes from the od libraries, derived classes can use this macro to define how they will allocate/deallocate. i have extended this macro to include mfc-style line-and-number memory operators in debug mode. it goes something like this:
code:
#ifdef _debug
#define my_heap_operators() \
odrx_heap_operators() \
void* operator new(size_t s, lpcstr f, int l) { return :

perator new(s, f, l); }\
void operator delete(void* p, lpcstr f, int l) { :

perator delete(p, f, l); }\
void* operator new[](size_t s, lpcstr f, int l) { return :

perator new(s, f, l); }\
void operator delete[](void* p, lpcstr f, int l) { :

perator delete(p, f, l); }
#else
#define my_heap_operators() odrx_heap_operators()
#endif
just use my_heap_operators() in the class definition (in lieu of odrx_heap_operators) and you should be good for mfc-style "new" overloads (through debug_new macro)