|
Neohapsis is currently accepting applications for employment. For more information, please visit our website www.neohapsis.com or email hr@neohapsis.com |
Are AddRef and Release really thread safe ???
From: Benny (benny.nguyen
USA.XEROX.COM)
Date: Mon Mar 17 2003 - 21:13:37 CST
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Typical COM implementation
ULONG ClassXXX::AddRef()
{
return InterlockedIncrement(&m_dwRef);
}
ULONG ClassXXX::Release()
{
ULONG l = InterlockedDecrement(&m_dwRef);
if (l == 0)
delete this;
return l;
}
The InterlockedXXXX functions guarantee that the variable will not be
updated simutaneously by more than one thread and the return value is the
resulting updated value (NT 4.0 or later) but consider this scenario
1. m_dwRef is at 0
2. Thread A enters AddRef and calls InterlockedIncrement(&m_dwRef)
3. Thread A returns from InterlockedIncrement and now m_dwRef is 1
4. Thread B enters Release and calls InterlockedDecrement(&m_dwRef)
5. Thread B returns from InterlockedDecrement and now m_dwRef is 0
6. Thread A returns from AddRef() and continues executing using the COM ptr
7. Thread B sees that ref count now is 0 and call delete this
8. Thread A crashes
Is it possible ????
----------------------------------------------------------------
Users Guide http://discuss.microsoft.com/archives/mailfaq.asp
contains important info. Save time, search the archives at
http://discuss.microsoft.com/archives/index.html .
To unsubscribe, mailto:DCOM-signoff-request
DISCUSS.MICROSOFT.COM
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]