?
Solved

ATL, COM and coinitialize

Posted on 2001-06-02
3
Medium Priority
?
1,453 Views
Last Modified: 2013-12-14
I'm pretty new to ATL COM programming so bare with my ignorance, please...

I've written an ATL COM .Dll that populates a recordset and returns it to a VB caller.  Everything works great.  However, I'm not sure where I should (properly) call CoInitialize and ::CoUninitialize().

At first I called them in the beginning (and end) of each method but that seemed redundant.  

So I started to call CoInitialize in InitInstance() and subsequently, ::CoUninitialize() in ExitInstance().

Will this be ok?  Any memory leak problems with this style?  Any problem with multiple instances? It seems to work but I need to be sure that this is solid programming!

Please advise and THANKS IN ADVANCE!
0
Comment
Question by:khampton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Expert Comment

by:djbusychild
ID: 6150074
you only need to call CoInitialize once per thread and you'll need a matching pair of CoUninitialize in that thread.. If you call CoInitialize multiple times it'll simply succeed ( unless you try to change the threading ).. you'll get S_FALSE returned for those calls, though.. you DO need to match those CoInitialize calls with CoUninitialize if you do call it.

You're not caling this in DllMain, though, right?
0
 
LVL 1

Author Comment

by:khampton
ID: 6150433
Appearently, we read the same MS-KnowledgeBase material.  I do not have an explicit DllMain routine (I guess that's because this is an ATL ActiveX component).  

Because I'm new to this stuff, and because I've not seen any examples of programs that do the CoInitialize in the InitExistance routine, I wanted verification that this was ok.  In fact, I want to know how (where) the experts do it.

Thanks.
0
 

Accepted Solution

by:
robertmitchell_99 earned 200 total points
ID: 6152487
Hi,

From your question it appears that youre asking where in your ATL code you need to call CoInitialize()and CoUninitialize()....?

If that is your question theres no need.  Check out the implementation, in atlbase.h and youll see that the COM initialization methods are called in CComApartment which is used by CComModule to handle the COM threading.

ATL does it for you.

If your question is where in a non-com client module you need to call them then the following code is what i use:

In InitInstance() or as early in a thread as possible
     if(FAILED(CoInitialize(NULL)) )
     // do some error handling

In ExitInstance() or as late in the thread as possible
     CoUninitialize();


best of luck,

Robert.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question