views:

128

answers:

2

My developement environment is [Windows 7; visual studio 2010; x86].

I have a dll that was built for server 2003 long time back. When I use it in my project and follow new/delete sequence to use a class, application crashes during delete call. I verified the same even without any other call between new and delete. When I replace new/delete with malloc/free, there is no crash. If I simply declare an instance of the class without new, no crash happens when scope is exited.

Any idea what may be going wrong? This is internal library of our company, so I will not be able to name it and other such stuff.

Additional Information: To use this library in first place, I had to turn off VS feature "Treat wchar_t as built-in type".

Code is simple

{
   CLogger * myLog = new CLogger(); 
   delete myLog; // Crash happens here 
} 

{ // No crash here 
  CLogger MyLog; 
} 

{ 
  CLogger * myLog = (CLogger *) malloc (sizeof(CLogger)); 
  free (myLog); // This does not crash. 
} 

This being proprietary library, I cannot post constructor and destructor.

+6  A: 

delete does more than just freeing memory: it also calls the destructor before. That means that there must be something bad in the destructor of that class.

If an uncaught exception occurs in a destructor the whole process exits (*). As commented below (thanks for the good feedback) this is over-simplified here is a good link for more details: http://stackoverflow.com/questions/130117/throwing-exceptions-out-of-a-destructor

I would recommend you to put a

try {} catch (std::exception& e){} catch(...) {}

inside the destructor and log out what is going on, or better let it go through the debugger with the option to stop at the place where the exception is thrown.

Then it should be easy to identify what is different. Just a guess from me, it may be some registry access or file access rights, where some changes were introduced from server 2003 to windows 7.

jdehaan
That's an over simplification of what happens when an exception is thrown from a destructor. It is definitely worth mentioning the exact conditions that are need for this to cause a call to <b>terminate()</b>
Martin York
The uncaught exception will only terminate the process if it occurs when the stack is unwinding due to another exception.
David Rodríguez - dribeas
+2  A: 

I apply my psychic debugging skills to suggest that you are using delete where you should be using delete[].

Reasoning: if you were able to trivially replace new with malloc, you're probably allocating an array of primitive types rather than an object, and naively using delete in place of free on the assumption that object allocation and array allocation are the same in C++. (They're not.)

JSBangs
From my experience that should maybe create a leak because only the first array object's constructor gets called, but no crash. I don't think it is the problem in this case however a good advice.
jdehaan
The fact that Zhinkaas has to modify a setting involving `wchar_t` also adds potential evidence for your point.
JAB
I think it is more due to the fact that wchar_t are decorated in another way if you use it as built-in and is maybe using c++ linking to another dll and would va to turn the signatures into shorts to link not to compile properly otherwise a lot or unresolved symbols. @Zhinkaas correct me it I am wrong.
jdehaan
the wchar_t related hack was needed ue to http://msdn.microsoft.com/en-us/library/799kze2z(VS.80).aspx (LNK2019 error).This is not the case as diagnosed about lack of [] as you can see form code posted above.
Zhinkaas