Questions about reference counting 
Author Message
 Questions about reference counting

I have a few questions about reference counting:

1. What is the best method for debugging reference problems?

2. Do I need to release after every QueryInterface?

3. When implementing a Get property on a com object, do I do an AddRef?

Thanks
Dell



Tue, 28 Oct 2003 00:21:13 GMT  
 Questions about reference counting
I have found that you can use the definition _ATL_DEBUG_REFCOUNT in your
applications to help you debug reference counting problems.

Richard


Quote:
> I have a few questions about reference counting:

> 1. What is the best method for debugging reference problems?

> 2. Do I need to release after every QueryInterface?

> 3. When implementing a Get property on a com object, do I do an AddRef?

> Thanks
> Dell



Tue, 28 Oct 2003 00:33:04 GMT  
 Questions about reference counting
Hello Richard,

Quote:
> > 1. What is the best method for debugging reference problems?
> I have found that you can use the definition _ATL_DEBUG_REFCOUNT in your
> applications to help you debug reference counting problems.

Better use _ATL_DEBUG_INTERFACES and  _ATL_DEBUG_QI definitions.

Quote:
> > 2. Do I need to release after every QueryInterface?

You should release interface pointer returned by QueryInterface after usage
(in case of QueryInterface succeed).

Quote:
> > 3. When implementing a Get property on a com object, do I do an AddRef?

For [out] interface parameters call AddRef inside property implementation.
Caller is responsible to release it.

--
Best regards,
Vadim Melnik,
{*filter*} Consultant.
_____________________________
http://www.*-*-*.com/



Tue, 28 Oct 2003 02:56:59 GMT  
 Questions about reference counting
Do I AddRef before passing a COM object out during an event notification?

    pMyObject->AddRef();
    Fire_OnMyObjectChanged(pMyObject);

Thanks
Dell


Quote:
> Hello Richard,

> > > 1. What is the best method for debugging reference problems?
> > I have found that you can use the definition _ATL_DEBUG_REFCOUNT in your
> > applications to help you debug reference counting problems.

> Better use _ATL_DEBUG_INTERFACES and  _ATL_DEBUG_QI definitions.

> > > 2. Do I need to release after every QueryInterface?

> You should release interface pointer returned by QueryInterface after
usage
> (in case of QueryInterface succeed).

> > > 3. When implementing a Get property on a com object, do I do an
AddRef?

> For [out] interface parameters call AddRef inside property implementation.
> Caller is responsible to release it.

> --
> Best regards,
> Vadim Melnik,
> {*filter*} Consultant.
> _____________________________
> http://www.*-*-*.com/



Wed, 29 Oct 2003 03:12:38 GMT  
 Questions about reference counting
Hello Dell,

Quote:
> Do I AddRef before passing a COM object out during an event notification?
>     pMyObject->AddRef();
>     Fire_OnMyObjectChanged(pMyObject);

In this case it's unnecessary AddRef call. For [in] parameters you can omit
additional AddRef/Release call. Have a look at "Specifications | Component
Object Model... | Chapter 3... | The IUnknown interface | Reference
Counting" topic in MSDN. The "Rule 2a" exactly corresponds to your case.

--
Best regards,
Vadim Melnik,
{*filter*} Consultant.
_____________________
http://www.*-*-*.com/

Quote:

> Thanks
> Dell



> > Hello Richard,

> > > > 1. What is the best method for debugging reference problems?
> > > I have found that you can use the definition _ATL_DEBUG_REFCOUNT in
your
> > > applications to help you debug reference counting problems.

> > Better use _ATL_DEBUG_INTERFACES and  _ATL_DEBUG_QI definitions.

> > > > 2. Do I need to release after every QueryInterface?

> > You should release interface pointer returned by QueryInterface after
> usage
> > (in case of QueryInterface succeed).

> > > > 3. When implementing a Get property on a com object, do I do an
> AddRef?

> > For [out] interface parameters call AddRef inside property
implementation.
> > Caller is responsible to release it.

> > --
> > Best regards,
> > Vadim Melnik,
> > {*filter*} Consultant.
> > _____________________________
> > http://www.*-*-*.com/



Wed, 29 Oct 2003 18:54:40 GMT  
 Questions about reference counting
Best is:

    pMyObject->AddRef();
    Fire_OnMyObjectChanged(pMyObject);
    pMyObject->Release();

Thus if your object's ref count was zero (just created), this code
will destroy it if the client was not interested.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.*-*-*.com/
=====================================


Quote:
> Do I AddRef before passing a COM object out during an event notification?

>     pMyObject->AddRef();
>     Fire_OnMyObjectChanged(pMyObject);

> Thanks
> Dell



> > Hello Richard,

> > > > 1. What is the best method for debugging reference problems?
> > > I have found that you can use the definition _ATL_DEBUG_REFCOUNT in
your
> > > applications to help you debug reference counting problems.

> > Better use _ATL_DEBUG_INTERFACES and  _ATL_DEBUG_QI definitions.

> > > > 2. Do I need to release after every QueryInterface?

> > You should release interface pointer returned by QueryInterface after
> usage
> > (in case of QueryInterface succeed).

> > > > 3. When implementing a Get property on a com object, do I do an
> AddRef?

> > For [out] interface parameters call AddRef inside property
implementation.
> > Caller is responsible to release it.

> > --
> > Best regards,
> > Vadim Melnik,
> > {*filter*} Consultant.
> > _____________________________
> > http://www.*-*-*.com/



Sat, 01 Nov 2003 09:23:45 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Static vairable dumb question (maintaining reference counts in a class)

2. reference counting question

3. .NET Reference counting not working?

4. Reference Counting

5. resource reference count under fork()

6. reference counting problem

7. Reference counting

8. Help with Release() and counting references

9. Reference count problem in singleton container

10. CreateInstance reference count

11. debug com_ptr_t reference counts

12. reference counting on embedded object

 

 
Powered by phpBB® Forum Software