?
Solved

Do i must an AddRef() ?

Posted on 2002-07-30
1
Medium Priority
?
924 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
[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
1 Comment
 
LVL 7

Accepted Solution

by:
peterchen092700 earned 400 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

Technology Partners: 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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn different types of Android Layout and some basics of an Android App.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Introduction to Processes

752 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