Click to See Complete Forum and Search --> : CoInitializeEx and MTA


George2
June 30th, 2008, 11:08 PM
Hello everyone,


Suppose there are a couple of threads in an MTA, since a process could have only one MTA, I think,

1. Even if each threads calls CoInitializeEx or CoInitialize, only the first call allocates COM related resources, all subsequent calls does not allocate any resources?

2. Only the last call to CoUninitialize frees the COM resources in the MTA, and at that time, the MTA terminates?

Both are correct?


thanks in advance,
George

Arjay
July 1st, 2008, 01:19 AM
1. Even if each threads calls CoInitializeEx or CoInitialize, only the first call allocates COM related resources, all subsequent calls does not allocate any resources?Think of this as reference counting where the first call allocates resources and subsequent calls just increment the ref count. Given this, would it make sense to keep allocating on each subsequent call?

2. Only the last call to CoUninitialize frees the COM resources in the MTA, and at that time, the MTA terminates?Would it makes sense if the first call to CoUnitialize freed the resources if initially several calls were made to ConInitialize[Ex]?

Think about how LoadLibrary/FreeLibrary works. From Msdn:

"The system maintains a per-process reference count on all loaded modules. Calling LoadLibrary increments the reference count. Calling the FreeLibrary or FreeLibraryAndExitThread function decrements the reference count. The system unloads a module when its reference count reaches zero or when the process terminates (regardless of the reference count)."

Do you think that CoInitialize[Ex]/CoUnitialize should follow the same pattern? Would it make sense not to?

George2
July 1st, 2008, 01:23 AM
Thanks Arjay,


So, I am thinking about there are two levels of COM related resources? Or just MTA level COM related resources?

1. MTA level (apartment level resource), allocated by the 1st thread of the
MTA which calls CoInitialize[Ex], freed by the last thread which calls
CoUninitialize of the MTA;

2. Thread level resource, allocated and freed each time a thread in the MTA
calls CoInitialize[Ex]/CoUninitialize.


regards,
George

Arjay
July 1st, 2008, 01:38 AM
Maybe yes, maybe no.

George2
July 1st, 2008, 01:46 AM
Sorry Arjay,


I asked this question because I have not found any documents about it. :-)

Maybe yes, maybe no.


regards,
George

Arjay
July 1st, 2008, 02:27 AM
Pick up the books 'Essential COM' (http://books.google.com/books?id=kfRWvKSePmAC&dq='Essential+COM'&pg=PP1&ots=o6bUieaMct&sig=0PilOomaGX1bZ8BoqnKgtitQjTs&hl=en&sa=X&oi=book_result&resnum=1&ct=result) and 'ATL Internals' (http://www.amazon.com/ATL-Internals-Addison-Wesley-Object-Technology/dp/0201695898).