Solved

Do i must an AddRef() ?

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to adjust sheet 1 81
sumHeights  challenge 17 60
Sorting in Excel with Group Headers if the Exist 2 67
python question 5 57
Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
A short article about problems I had with the new location API and permissions in Marshmallow
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…
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…

707 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

16 Experts available now in Live!

Get 1:1 Help Now