Optimisation causing crash

Optimisation causing crash

am 09.04.2008 13:12:44 von ropo

I have the following C++

// unmanaged code
func()
{
// this class is principly data
CMyClass Obj();

do
{
CallManagedWrapper(&Obj);
} while(...) // crashes second time around
}

// manage code
CallManagedWrapper( CMyClass* pObj)
{
CMyManagedClass^ ManObj = gcnew CMyManagedClass(pObj);
}

CMyManagedClass(CMyClass* pObj)
{
// copy data from unmanaged code
m_dbA = pObj->m_dbA;
}

With optimisation off around func() there is no crash with it on, the
second time the CMyManagedClass construtor accesses pObj it crashed.

so.

#pragma optimize("", off)
// unmanaged code
func()
{
// this class is principly data
CMyClass Obj();

do
{
CallManagedWrapper(&Obj);
} while(...) // crashes second time around
}
#pragma optimize("", on)

there is no problem

also there is no problem if I do this:

// unmanaged code
func()
{
// this class is principly data
CMyClass* pObj = new CMyClass();
CMyClass& Obj = *pObj;

do
{
CallManagedWrapper(&Obj);
} while(...) // crashes second time around
delete pObj;
}

I am worried that this problem may occur elsewhere so am seeking an
understanding of the problem.