Failed new operator
-
You are right. It compiles fine in release mode. This is new and weird to me... How am I supposed to debug my app then?
Joe Smith IX wrote:
You are right. It compiles fine in release mode. This is new and weird to me... How am I supposed to debug my app then?
In the implementation file you are using, you could look for the
#define new
statement and remove it. If you want to keep the ability to detect memory leaks from that code, you can change it to#define TNEW
, add a#else
clause with#define TNEW new
, and then replace all standard calls tonew
withTNEW
. Nathan -
I recall that Visual C++ generates code with a debug feature in which
new
is#Define
d as something else in debug builds. Does it compile in release mode? Nathan:doh: I didn't think of that. Good eye Nathan!! :) Cheers!
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Joe Smith IX wrote:
You are right. It compiles fine in release mode. This is new and weird to me... How am I supposed to debug my app then?
In the implementation file you are using, you could look for the
#define new
statement and remove it. If you want to keep the ability to detect memory leaks from that code, you can change it to#define TNEW
, add a#else
clause with#define TNEW new
, and then replace all standard calls tonew
withTNEW
. NathanBut when I search for it, the only two instances of 'define new' are the standard one below in my dialog and app .cpp files. Could it be defined some other way? Or any other workaround to make it compilable in debug mode? Thanks.
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif -
But when I search for it, the only two instances of 'define new' are the standard one below in my dialog and app .cpp files. Could it be defined some other way? Or any other workaround to make it compilable in debug mode? Thanks.
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifDo you really need to call the operator directly? Can you just use the vector form of new and allocate an array of bytes? return new BYTE[size]; Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Do you really need to call the operator directly? Can you just use the vector form of new and allocate an array of bytes? return new BYTE[size]; Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Well, the thing is that the call is made from the class I downloaded from CP (see my first post above). If I change the object, then I have to modify the class as well, right? What makes me wonder is that how come I can run the demo code in debug mode and step thru line-by-line, while in my test project I can't even compile the debug mode? They both using the same classes! Any ideas? thanks for any suggestion.
-
Well, the thing is that the call is made from the class I downloaded from CP (see my first post above). If I change the object, then I have to modify the class as well, right? What makes me wonder is that how come I can run the demo code in debug mode and step thru line-by-line, while in my test project I can't even compile the debug mode? They both using the same classes! Any ideas? thanks for any suggestion.
If the problem is with using the DEBUG_NEW operator in debug builds, then you can wrap your calls to operator new with something like #pragma push_macro("new") #undef new ... (use the new operator here) ... #pragma pop_macro("new") The problem is, where do you put this in a template class? Do you put it in the class declaration or do you have to remember to do it every place you instantiate an object of the class? I haven't seen the rest of the class in question, but I assume if it calls operator new, then it must call operator delete somewhere. Those should be the only calls you'd need to change. I have no idea why the author did that - is the operator new overloaded in the class? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
If the problem is with using the DEBUG_NEW operator in debug builds, then you can wrap your calls to operator new with something like #pragma push_macro("new") #undef new ... (use the new operator here) ... #pragma pop_macro("new") The problem is, where do you put this in a template class? Do you put it in the class declaration or do you have to remember to do it every place you instantiate an object of the class? I haven't seen the rest of the class in question, but I assume if it calls operator new, then it must call operator delete somewhere. Those should be the only calls you'd need to change. I have no idea why the author did that - is the operator new overloaded in the class? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Mark Salsbery wrote:
#pragma push_macro("new") #undef new ... (use the new operator here) ... #pragma pop_macro("new")
That's cool. I didn't know about the
#pragma push_macro
. Thanks for mentioning it. Nathan -
If the problem is with using the DEBUG_NEW operator in debug builds, then you can wrap your calls to operator new with something like #pragma push_macro("new") #undef new ... (use the new operator here) ... #pragma pop_macro("new") The problem is, where do you put this in a template class? Do you put it in the class declaration or do you have to remember to do it every place you instantiate an object of the class? I haven't seen the rest of the class in question, but I assume if it calls operator new, then it must call operator delete somewhere. Those should be the only calls you'd need to change. I have no idea why the author did that - is the operator new overloaded in the class? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
You are right about the conflict is with the definition of DEBUG_NEW operator in debug builds. As soon as I commented all instances, it compiled fine.
/*
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
*/I can't find any code that overload new operator in the class, though... Using the push_macro and pop_macro resulted this error:
#pragma push_macro : 'new' is not currently defined as a macro
Anyway, what's the effect I will get by commenting out the definition of DEBUG_NEW? Thanks.
-
You are right about the conflict is with the definition of DEBUG_NEW operator in debug builds. As soon as I commented all instances, it compiled fine.
/*
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
*/I can't find any code that overload new operator in the class, though... Using the push_macro and pop_macro resulted this error:
#pragma push_macro : 'new' is not currently defined as a macro
Anyway, what's the effect I will get by commenting out the definition of DEBUG_NEW? Thanks.
Joe Smith IX wrote:
what's the effect I will get by commenting out the definition of DEBUG_NEW?
You just lose the file/linenumber info in debug builds that aid in finding memory leaks. I'm still not sure why the author used new like that... Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Joe Smith IX wrote:
what's the effect I will get by commenting out the definition of DEBUG_NEW?
You just lose the file/linenumber info in debug builds that aid in finding memory leaks. I'm still not sure why the author used new like that... Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
In that case, is there any way to undef the DEBUG_NEW just before the conflicting code, and then re-define it right afterward? Something like below: (except this one generated one warning)
static void* Alloc(UINT size)
{
#ifdef _DEBUG
#undef new DEBUG_NEW // <-- compile warning C4067: unexpected tokens following preprocessor directive - expected a newline
#endifreturn operator new(size);
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
}Thanks.
-
In that case, is there any way to undef the DEBUG_NEW just before the conflicting code, and then re-define it right afterward? Something like below: (except this one generated one warning)
static void* Alloc(UINT size)
{
#ifdef _DEBUG
#undef new DEBUG_NEW // <-- compile warning C4067: unexpected tokens following preprocessor directive - expected a newline
#endifreturn operator new(size);
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
}Thanks.
Sorry for the late reply - did you get this working? The line with the warning should be just
#undef new
As I mentioned before, I'm just not sure where this should be in a template class. Since the actual class is built where an object is instantiated, I'm not sure if the #undef stuff needs to be there or if it worke by putting it in the template declaration. If it works in the template declaration (as you've shown) then that's cool :) It's easy enough to test. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: