Solved

Do i must an AddRef() ?

Posted on 2002-07-30
1
910 Views
Last Modified: 2008-02-01
Hello

I would like to know if i must do an implicit AddRef() when ADO returns me a Fields collection ....

For instance:

      if( oRS->Open( Source, Conn, adOpenStatic,  adLockPessimistic, adCmdTable ) == 0 )
      {

         // DO I CONSIDER THAT THE AddRef() HAS
         // BEEN MADE BY ADO ? So the only thing i
         // must do is to impliclty call the Release()
         // function.


         CComPtr<Fields15> allfield;
         allfield.Attach( oRS->get_Fields() );


         // OR I DO IT BY ATL CLASS, and the Release()
         // function will be called too.


         CComPtr<Fields15> allfield;
         allfield = oRS->get_Fields();


Is it the same thing for all ADO objects, ie. retrieving an unique field "Field15" ??

Thank you to answer me
0
Comment
Question by:mars
1 Comment
 
LVL 7

Accepted Solution

by:
peterchen092700 earned 100 total points
ID: 7221171
no, you need not.

Let's first discuss the AddRef/Release rules without  smart pointers (like CComPtr)

* if a function returns an itf ptr, the function addref's before returning, and the caller releases when he is done using it.

* if an itf ptr is passed to a function as parameter, neither the caller nor the function need to addref or release.

* If a function receives a itf ptr as parameter, *and* stores this pointer to use it after the function has returned, it must AddRef the itf ponter, and it must be released when it is no longer used.

------

Smart pointers will do all this handling for you (although they end up with a few more AddRef's and Releases than technically necessary). There are only a few places where you have to be careful:

a) raw interface pointer returned from function
you should use Attach instead of assignment or the Constructor:

IMyInterface * MyFace();
...
CComPtr<IMyInterface> cp;
cp.Attach(MyFace());
// MyFace has called AddRef,
// the ComPtr DTor will call Release,

If you would have used

CComPtr<IMyInterface> cp = MyFace();

there would be "one AddRef to much"

b) passing the address of an itf ptr to a function:
if it's declared
HRESULT Foo(/* [out]*/ IMyInterface ** p)
you can use:

CComPtr<IMyInterface> cp;
hr = Foo(&cp);

However, if it's declared
HRESULT Bar(/* [in,out]*/ IMyInterface ** p)

you need to:

CComPtr<IMyInterface> cp;
IMyInterface * piraw = cp.Detach();
hr = Foo(&cp);
cp.Attach(piraw);

Helped?
Peter
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

785 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