Mysterious C++ behaviour
-
I have 4 DLLs as following: CFiles.dll CExceptions.dll CXML.Dll CDataFormat.dll CExceptions contains an Object factory that follows the Loki Object Factory model as following: It has a map containing function pointers with enteries such as following: void CExceptionFactory::InitializeMap() { AddToMethodMap(CExceptionFactory::DATABASE_EXCEPTIONS, CreateDatabaseExceptions); AddToMethodMap(CExceptionFactory::NULL_POINTER_EXCEPTIONS, CreateNullPointerExceptions); AddToMethodMap(CExceptionFactory::READER_EXCEPTIONS, CreateReadExceptions); } Now, within the DataFormat DLL, depending on the type of formatting, I load different DLLs. For instance, if the type is XML, I load the XML dll as following: case ID_XML: { //m_pDataFormat = new XML(); HMODULE hLibrary = 0; DataFormatProc pfnDataFormat = 0; GEA pfnGEA = 0; hLibrary = ::LoadLibrary("CXML.dll"); if(hLibrary != 0) { DWORD dw = 0; pfnDataFormat = (DataFormatProc) GetProcAddress (hLibrary, "CreateXMLSingletonInstance"); dw = GetLastError(); if(pfnDataFormat != 0) m_pDataFormat = pfnDataFormat(); m_pDataFormat->LoadFile(L"Stocks.xml"); ::FreeLibrary(hLibrary); } Now if the method LoadFile above fails, it throws an exception by calling CExcpetionFactory class above and CExceptionFactory will try to look inside its Loki map and retrieve the required method as shown below: CExceptionFactory::CExceptionFactory(int nTypeID, const string & strMessage) { InitializeMap(); CreateObject(nTypeID); if(m_pException != 0) { m_pException->Log(strMessage); delete m_pException; } else { throw CExceptionFactory(CExceptionFactory::NULL_POINTER_EXCEPTIONS,"Unknown Exception."); } } void CExceptionFactory::CreateObject(int nTypeID) { CallbackMap::const_iterator i = m_Callbacks.find(nTypeID); if( i != m_Callbacks.end()) { m_pException = (i->second)(); } else m_pException = 0; } Now, as soon as the code hits (i->second)(); , it leaves the exception dll all together and goes back to the beginning of that case statement above. I am quite puzzled with this behaviour. I am guessing I have a function pointer to begin with the loaded DLL and then I am getting a second function pointer and the compiler gets confused, but why should it? Your help would be highly appreciated. thanks:confused:
-
I have 4 DLLs as following: CFiles.dll CExceptions.dll CXML.Dll CDataFormat.dll CExceptions contains an Object factory that follows the Loki Object Factory model as following: It has a map containing function pointers with enteries such as following: void CExceptionFactory::InitializeMap() { AddToMethodMap(CExceptionFactory::DATABASE_EXCEPTIONS, CreateDatabaseExceptions); AddToMethodMap(CExceptionFactory::NULL_POINTER_EXCEPTIONS, CreateNullPointerExceptions); AddToMethodMap(CExceptionFactory::READER_EXCEPTIONS, CreateReadExceptions); } Now, within the DataFormat DLL, depending on the type of formatting, I load different DLLs. For instance, if the type is XML, I load the XML dll as following: case ID_XML: { //m_pDataFormat = new XML(); HMODULE hLibrary = 0; DataFormatProc pfnDataFormat = 0; GEA pfnGEA = 0; hLibrary = ::LoadLibrary("CXML.dll"); if(hLibrary != 0) { DWORD dw = 0; pfnDataFormat = (DataFormatProc) GetProcAddress (hLibrary, "CreateXMLSingletonInstance"); dw = GetLastError(); if(pfnDataFormat != 0) m_pDataFormat = pfnDataFormat(); m_pDataFormat->LoadFile(L"Stocks.xml"); ::FreeLibrary(hLibrary); } Now if the method LoadFile above fails, it throws an exception by calling CExcpetionFactory class above and CExceptionFactory will try to look inside its Loki map and retrieve the required method as shown below: CExceptionFactory::CExceptionFactory(int nTypeID, const string & strMessage) { InitializeMap(); CreateObject(nTypeID); if(m_pException != 0) { m_pException->Log(strMessage); delete m_pException; } else { throw CExceptionFactory(CExceptionFactory::NULL_POINTER_EXCEPTIONS,"Unknown Exception."); } } void CExceptionFactory::CreateObject(int nTypeID) { CallbackMap::const_iterator i = m_Callbacks.find(nTypeID); if( i != m_Callbacks.end()) { m_pException = (i->second)(); } else m_pException = 0; } Now, as soon as the code hits (i->second)(); , it leaves the exception dll all together and goes back to the beginning of that case statement above. I am quite puzzled with this behaviour. I am guessing I have a function pointer to begin with the loaded DLL and then I am getting a second function pointer and the compiler gets confused, but why should it? Your help would be highly appreciated. thanks:confused:
Well it sounds like you have corrupted your stack. Then you can end up any where in your code when calling function. Magnus