Solved

Do i must an AddRef() ?

Posted on 2002-07-30
1
917 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …

726 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