Solved

Do i must an AddRef() ?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using YubiKey with REST API application 2 108
firstswap challenge 20 67
How does Google get such fast response times? 5 151
iframe detection of parent window scale 20 60
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This is about my first experience with programming Arduino.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now