Thread View: comp.os.ms-windows.programmer.ole
2 messages
2 total messages
Started by Thaddeus L. Olczyk
Thu, 30 Jul 1998 00:00
CComObject and reference counting
Author: Thaddeus L. Olczyk
Date: Thu, 30 Jul 1998 00:00
Date: Thu, 30 Jul 1998 00:00
14 lines
1091 bytes
1091 bytes
Hi. In BCB I am instantiating a class via CComObject eq CComObject<AImpl> *a =new CComObject<AImpl>; followed by returning an interface pointer to a. This created a lot of difficulties for me which I discovered from: CComObject<AImpl> *a =new CComObject<AImpl>; a->AddRef(); a->Release(); which causes a to be deleted. What's going on, it looks like a is created with refcount 0. Shouldn't it be 1?
Re: CComObject and reference counting
Author: Mike Tyukanov
Date: Thu, 30 Jul 1998 00:00
Date: Thu, 30 Jul 1998 00:00
35 lines
1816 bytes
1816 bytes
olczyk@interaccess.com (Thaddeus L. Olczyk) wrote: >Hi. In BCB I am instantiating a class via CComObject eq > >CComObject<AImpl> *a =new CComObject<AImpl>; That works, but it would be safer to use CComObject<AImpl> *a; HRESULT hr=CComObject<AImpl>::CreateInstance(&a); //test hr as you wish However, this code will also left a->refcount to zero. Frankly, there is no much a difference between your and my code, but CComObjectBase<T>::CreateInstance calls FinalConstruct() that can have an essential portion of ATL class initialisation. > >followed by returning an interface pointer to a. >This created a lot of difficulties for me which I discovered from: > >CComObject<AImpl> *a =new CComObject<AImpl>; >a->AddRef(); >a->Release(); > >which causes a to be deleted. >What's going on, it looks like a is created with refcount 0. Shouldn't >it be 1? It shouldn't. You don't get any _interface_ pointer. What you've done is just a C++ class instantiation. Now you should obtain an interface pointer using QueryInterface like CoCreateInstance does internally: IAImpl *pAImpl=NULL; hr=a->QueryInterface(IID_IAImpl,&pAImpl); not refcount is 1 and your pointer pAImpl is a valid COM Interface pointer, not a C++ pointer to class.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads