Failed CoCreateInstance
Asked Answered
B

2

5

I have a function where I am calling CoCreateInstance. This function is called multiple times and it works, however one time the CoCreateInstance fails with the "Bad variable type" error. The thing is the parameters are always the same both when it succedes and when it fails. What could cause this to happen?

Benavides answered 13/7, 2011 at 12:55 Comment(10)
That could be coming from the component code - it can be anything. Do you have that component source code?Paduasoy
You need to be a lot more specific about the error. Is that a COM result code? What kind of object are you trying to create.Privacy
I do have the source code but it's very large. It is a COM result code, yes. I don't know a lot of details about the object it creates. What I don't get is that the parameters are the same and yet it fails... something must change the behavior, right? What could that be?Benavides
@Luchian Grigore It could be all sorts of memory corruption ... What is supposed to do this function ?Mcclanahan
That can be some defect that only surfaces once you've created several instances. Your best bet is to attach the debugger and see what's going wrong inside the component.Paduasoy
The function might as well just call the CoCreateInstance and return the result. I narrowed it down to memory corruption myself, but I still can't figure it out. The parameters are the same, so what could be corrupted? The code for CoCreateInstance? I find that unlikely.Benavides
@Paduasoy I've been debugging for hours and can't find anything. is there a limit to the number of instances that can be created?Benavides
There's no system wide limit. You don't debug CoCreateInstance(), you should debug the component instead - get into its code and see where it returns the error code.Paduasoy
The component is what i've been debugging... And the CoCreateInstance call is the one that returns the error code... Does CoCreateInstance call something else from my component?Benavides
It should call DllGetClassObject(), then CreateInstance() in the retrieved class factory.Paduasoy
B
5

I fixed this. The call was made from different threads. When the call succeded, it was called from the main thread. On fail, a different thread was the source and CoInitialize() wasn't called beforehand. Calling CoInitialize solved the problem.

Benavides answered 20/7, 2011 at 8:48 Comment(2)
That's strange. Usually when you call CoCreateInstance() before CoInitialize() you get a recognizable "CoInitialize has not been called error".Paduasoy
Not really, in many cases without CoInitialize() I recived a crazy error that had nothing to do with anything.Vincevincelette
C
9

I also had a function in my code calling CoCreateInstance(...). When one time it worked and another time it did not, without changing the code.
But when looking at the HRESULT which was returned, the result was an Out of Memeory Error, "E_OUTOFMEMORY Ran out of memory." I did do some research regarding the subjects and found the following reasons that this can occur.

  1. You must first call the CoInitialize()

The CoInitialize function must be called prior to calling the CoCreateInstance function

  1. Incorrect COM object registration

This can often occurred when the COM object (or the proxy-stub) wasn't registered properly. You can check the registration of the COM object using the Oleview tool by trying to instantiate the object.

The Oleview.exe is included in Windows SDK, the location is typically at C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin.

If it is not there you can try installing Microsoft Windows SDK for Windows Server 2008: http://www.microsoft.com/downloads/details.aspx?FamilyId=F26B1AA4-741A-433A-9BE5-FA919850BDBF&displaylang=en.

Note: it is not only for Windows 2008, it is just named after the recent Windows version and it supports Windows Server 2003; Windows Server 2008; Windows Vista; Windows XP.

  1. Missing, corrupt of incorrect versions of DLLs

For me, this was happening in different projects configuration, in Debug mode I caught the exception but in Release it did not appear.

Croy answered 20/7, 2015 at 15:14 Comment(0)
B
5

I fixed this. The call was made from different threads. When the call succeded, it was called from the main thread. On fail, a different thread was the source and CoInitialize() wasn't called beforehand. Calling CoInitialize solved the problem.

Benavides answered 20/7, 2011 at 8:48 Comment(2)
That's strange. Usually when you call CoCreateInstance() before CoInitialize() you get a recognizable "CoInitialize has not been called error".Paduasoy
Not really, in many cases without CoInitialize() I recived a crazy error that had nothing to do with anything.Vincevincelette

© 2022 - 2024 — McMap. All rights reserved.