Using wodKeys in multiple threads - WeOnlyDo Discussion board

Using wodKeys in multiple threads (General questions)

by mulloju, Friday, November 04, 2011, 09:49 (4579 days ago)

Is wodKeys thread-safe? I'm experiencing occasional exceptions and automatic breaks complaining about possible heap corruption when calling this method in multiple parallel threads:

hr = pKeys.CreateInstance(CLSID_Keys, NULL);

I do this in every thread:

IKeysPtr pKeys;
HRESULT hr = NULL;
CoInitialize(NULL);
hr = pKeys.CreateInstance(CLSID_Keys, NULL);
...
pKeys.Release();
CoUninitialize();

Am I doing something wrong?

Re: Using wodKeys in multiple threads

by wodDamir, Friday, November 04, 2011, 10:08 (4579 days ago) @ mulloju

Hi,

Yes it is. However, you need to make sure you keep it apartment threaded. In other words, you can only use it from the same thread you created the component in.

Can you please verify that?

Regards,
Damba

Re: Using wodKeys in multiple threads

by mulloju, Friday, November 04, 2011, 11:02 (4579 days ago) @ wodDamir

Yes. Each Keys instance is only used inside its own thread.

Re: Using wodKeys in multiple threads

by wodDamir, Friday, November 04, 2011, 11:49 (4579 days ago) @ mulloju

Hi,

In that case, I don't see how that's possible. Is there any chance we could reproduce this on our side?

Can you perhaps provide us a sample we could use on our side in order to do so? You can send it over to techsupport@weonlydo.com

Regards,
Damba

Re: Using wodKeys in multiple threads

by mulloju, Friday, November 04, 2011, 12:14 (4579 days ago) @ wodDamir

Hi,

I don't think I can easily share the code. Basically, the main process spawns N threads, each of the N threads spawn their own M threads, and inside each M thread I create a Keys instance like I showed earlier.

These kind of errors I get while debugging. Perhaps occurs more often if I have enabled some breakpoints:

HEAP[xxx.exe]: Invalid address specified to RtlReAllocateHeap( 03B30000, 03B36698 )
Windows has triggered a breakpoint in xxx.exe.

callstack:

HRESULT CreateInstance(const CLSID& rclsid, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) throw() ->
...
hr = CoCreateInstance(rclsid, pOuter, dwClsContext, __uuidof(IUnknown), reinterpret_cast<void**>(&pIUnknown)); ->
...
...
asctime.c ->
errno_t __cdecl _tasctime_s (
_TSCHAR *buffer,
size_t sizeInChars,
REG1 const struct tm *tb
)

Re: Using wodKeys in multiple threads

by wodDamir, Friday, November 04, 2011, 13:24 (4579 days ago) @ mulloju

Hi,

It's virtually impossible to tell why or where you would receive this error. However, there's no need to share your complete code.

Is there any chance you can isolate only the code using wodKeys? I.e. create a sample application that only uses wodKeys in the way you are calling it in your application?

Regards,
Damba

Re: Using wodKeys in multiple threads

by mulloju, Friday, November 04, 2011, 14:23 (4579 days ago) @ wodDamir

Not quite sure yet, but using:

CoInitializeEx(NULL, COINIT_MULTITHREADED);

..instead of:

CoInitialize(NULL);

..seems to fix my problem.