Article View: comp.os.ms-windows.programmer.ole
Article #22Re: CComObject and reference counting
From: 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.
Technical Details
Message-ID:
<35c0eebf.141473840@news-win.rinet.ru>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<35bfeef3.1377340@nntp.interaccess.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f9701,1d14f9d1d44ed69e X-Google-Attributes: gidf9701,public X-Google-Thread: 108cda,1d14f9d1d44ed69e X-Google-Attributes: gid108cda,public From: stroke@rinet.ru (Mike Tyukanov) Subject: Re: CComObject and reference counting Date: 1998/07/30 Message-ID: <35c0eebf.141473840@news-win.rinet.ru> X-Deja-AN: 376482198 References: <35bfeef3.1377340@nntp.interaccess.com> Organization: Rinet News Server Newsgroups: comp.os.ms-windows.programmer.ole,borland.public.cppbuilder.activex