Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everybody,

I have a situation where the Delete() statement in the following code segment causes an access violation:

C#
try {
    MyOleDispatchDriver->InvalidFunction();
} catch (COleException* err) {
    TCHAR   szCause[255];
    err->GetErrorMessage(szCause, 255);
    LogString("Call failed because \"" + CString(szCause));
    err->Delete();
}


The MyOleDispatchDriver is an object of a class-wizard generated wrapper. The InvalidFunction is generated by this, but at the time the code is executed an older version of the automation server is used - which does not provide InvalidFunction. As a result, the exception handler is entered.

The err->Delete() statement does work - the rest of the err data seems valid. As for any CException-derived types I expected this to be the correct way of destructing the exception.

Has anybody run across a similar thing?

Many thanks!
Posted

The LogString() call looks suspicious:
You are adding a CString object to a LPCTSTR. If LogString() expects a LPCTSTR, you are passing an invalid pointer: The address of the CString (by using the implicit LPCTSTR operator) is added to the address of your constant string.
 
Share this answer
 
Comments
Doc Lobster 19-Nov-12 6:21am    
Hi, thanks for your answer. LogString takes const CString& as parameter and the string is concatenated properly. Anyway, I found the solution, I'm calling Delete twice ...
Jochen Arndt 19-Nov-12 6:38am    
It was a try and would explain the problem.

I had never such problems with COleExceptions but can't right now remember when one was thrown from code written by me (code is similar to yours).

Did you also catch COleDispatchExceptions? If not, you should do so. However, I don't think that this may help here.

You may also try catching all exceptions to see if this makes a difference. E.g.:
catch (COleDispatchException *de)
{
// show error message
de->Delete();
}
catch (CException *e)
{
// show error message
e->Delete();
}
Doc Lobster 19-Nov-12 13:01pm    
Yes, I'm aware of COleDispatchException*. It is derived from CException, so catching CException will do the job.
Sorry for asking the question, I'm calling err->Delete() twice (not visible in the question itself) . Understandably, I rather would like to draw back the question.
Anyway, when looking at the Delete() code:

C#
void CException::Delete()
{
    // delete exception if it is auto-deleting
    if (m_bAutoDelete > 0)
    {
#ifdef _DEBUG
        m_bReadyForDelete = TRUE;
#endif
        delete this;
    }
}


m_bAutoDelete is just a random number after the first call to err->Delete(), so chances are 1:1 that the deletion is tried a second time if Delete() is to be called again.

On my development system, it was always a negative not causing the exception. On another system, m_bAutoDelete resolved to true and bang.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900