473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

smart pointer release

200 100+
Hello everyone,


About the release method implementation of smart pointer of COM, there are two approaches below, and approach 1 is preferred is recommended by Inside COM -- should be better.

Anyone know why approach 1 is better than approach 2?

(m_pI is interface pointer to a COM interface of type T, and it is a member variable of the COM smart pointer class)

Approach 1:

Expand|Select|Wrap|Line Numbers
  1. void Release()
  2. {
  3.     if (m_pI != NULL)
  4.     {
  5.         T* pOld = m_pI;
  6.         m_pI = NULL;
  7.         pOld->Release();
  8.     }
  9. }
  10.  
Approach 2:

Expand|Select|Wrap|Line Numbers
  1. void Release()
  2. {
  3.     if (m_pI != NULL)
  4.     {
  5.         m_pI -> Release();
  6.         m_pI = NULL;
  7.     }
  8. }
  9.  

thanks in advance,
George
Feb 10 '08 #1
2 3000
weaknessforcats
9,208 Expert Mod 8TB
I have no idea since the destructor of CComPtr uses Approach 2.
Feb 10 '08 #2
George2
200 100+
Hi weaknessforcats,


Any comments?

--------------------
Imagine this situation: object A holds a smart pointer to object B,
which in turn holds a smart pointer to object A. For whatever reason, A
decides to release its reference on B, by calling Release() on the smart
pointer. This happens to be the last reference, so B destroys itself. In
its destructor, it releases its reference on A - again by calling
Release(). This, too, happens to be the last reference on A (the two
objects kept each other alive with a circular reference), so in its
destructor A calls Release() on its smart pointer to B.

The bottom line of this scenario is that smart pointer's Release() ends
up being called from itself, recursively. Approach 1 handles this
gracefully, but Approach 2 will enter an infinite recursion.
--------------------

I have no idea since the destructor of CComPtr uses Approach 2.

regards,
George
Feb 11 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Johnny Hansen | last post by:
Hello, I've been trying to implement smart pointers in C++ (combined with a reference counter) because I want to do some memory management. My code is based on the gamedev enginuity articles,...
8
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good...
28
by: Protoman | last post by:
I'd like to now if this smart pointer class if thread-safe: Code: // COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06 // ALL UNAUTHORIZED THIRD PARTY USE IS PROHIBITED // I HAVE WORKED VERY HARD...
3
by: coala | last post by:
In Smart Pointer class, we have a piece of code: inline void release(void) const { if (mPtr) mPtr->release(); mPtr = 0; } It can set mPtr but it is still const? Why could this work and why...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
10
by: =?iso-8859-1?q?Ernesto_Basc=F3n?= | last post by:
I am implementing my custom smart pointer: template <typename T> class MySmartPtr { public: MySmartPtr(T* aPointer) { mPointer = aPointer; }
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
13
by: Phil Bouchard | last post by:
I am currently writting a smart pointer which is reasonnably stable and I decided supporting allocators for completion because of its increase in efficiency when the same pool used by containers is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.