MFC and COM, and Access violation in CoUninitialize
-
Hi everyone, I wasn't sure if this is a COM issue or MFC issue, so I also posted this question in the COM message board... I have an MFC app (with a CWinApp-derived object) that uses COM objects (Crystal Reports). The COM library is imported into my project via the #import directive. I'm using VStudio 6.0 sp5. I've created a global object called _com_init: struct ComInit { ComInit() { ::CoInitialize(NULL); } ~ComInit() { ::CoUninitialize(); } } _com_init_; This way, I guarantee that COM will initialize before the _main() and terminate after the _main(). When my program reaches ~ComInit() and calls ::CoUninitialize(), I see the following message in my Debug Output Window: First-chance exception in MyApp.exe (OLE32.DLL): 0xC0000005: Access Violation. Any ideas? I've looked up other CoUninitialize threads in these forums, but couldn't figure out something that would solve my problem... Thanks. /=/=/=/= Deus /=/=/=/=
-
Hi everyone, I wasn't sure if this is a COM issue or MFC issue, so I also posted this question in the COM message board... I have an MFC app (with a CWinApp-derived object) that uses COM objects (Crystal Reports). The COM library is imported into my project via the #import directive. I'm using VStudio 6.0 sp5. I've created a global object called _com_init: struct ComInit { ComInit() { ::CoInitialize(NULL); } ~ComInit() { ::CoUninitialize(); } } _com_init_; This way, I guarantee that COM will initialize before the _main() and terminate after the _main(). When my program reaches ~ComInit() and calls ::CoUninitialize(), I see the following message in my Debug Output Window: First-chance exception in MyApp.exe (OLE32.DLL): 0xC0000005: Access Violation. Any ideas? I've looked up other CoUninitialize threads in these forums, but couldn't figure out something that would solve my problem... Thanks. /=/=/=/= Deus /=/=/=/=
The most important aspect of using COM with MFC is to initialize and uninitialize COM correctly. Instead of using a global object, I suggest that you put ::CoInitialize(NULL) in the start of your CWinApp-derived application's 'InitInstance' and ::CoUninitialize() o the end of the same application's 'ExitInstance' (Before return call). This way, the initialization and uninitialization are handled smoothly. As for the actual problem, I have just about no idea of where it lies. I suggest using the above methodology to utilize COM server. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.
-
The most important aspect of using COM with MFC is to initialize and uninitialize COM correctly. Instead of using a global object, I suggest that you put ::CoInitialize(NULL) in the start of your CWinApp-derived application's 'InitInstance' and ::CoUninitialize() o the end of the same application's 'ExitInstance' (Before return call). This way, the initialization and uninitialization are handled smoothly. As for the actual problem, I have just about no idea of where it lies. I suggest using the above methodology to utilize COM server. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.
I already tried that. It was a lot worse when I did that. The result was about a zillion "Access Violation" messages instead of just one. I think there was also a "Stack overflow" message somewhere among them... Also, it may have been unclear earlier, but the program doesn't abort with an Access Violation MESSAGE BOX. I only see the message in the debug output window. If I try what you suggested, I also get an Access Violation message box... /=/=/=/= Deus /=/=/=/=
-
I already tried that. It was a lot worse when I did that. The result was about a zillion "Access Violation" messages instead of just one. I think there was also a "Stack overflow" message somewhere among them... Also, it may have been unclear earlier, but the program doesn't abort with an Access Violation MESSAGE BOX. I only see the message in the debug output window. If I try what you suggested, I also get an Access Violation message box... /=/=/=/= Deus /=/=/=/=
Have you determined that the COM server is indeed initialized? if(::CoInitialize()!=S_OK) MessageBox(_T("COM INIT FAILED"),NULL,MB_ICONERROR); ??
-
Have you determined that the COM server is indeed initialized? if(::CoInitialize()!=S_OK) MessageBox(_T("COM INIT FAILED"),NULL,MB_ICONERROR); ??