69,371
社区成员
发帖
与我相关
我的任务
分享
#include<stdlib.h>
#include<stdio.h>
int main()
{
int *ptr = malloc(10*sizeof(int));
int *ptr2 = ptr;
if(ptr)
{
free(ptr);
ptr =NULL;
printf("delete one");
}
if(ptr2)
{
free(ptr2);
ptr2=NULL;
printf("delete two");
}
return 0;
}
template <class T>
class CRefCs : public T
{
public:
CRefCs()
:m_cRef(0)
{
}
virtual ~CRefCs()
{
}
public:
inline static ULONG ourmax( const ULONG & a, const ULONG & b )
{
return a > b ? a : b;
}
ULONG AddRef()
{
LONG lRef = InterlockedIncrement( &m_cRef );
_ASSERTE(lRef > 0);
return ourmax(ULONG(m_cRef), 1ul);
}
ULONG Release()
{
LONG lRef = InterlockedDecrement( &m_cRef );
//_ASSERTE(lRef >= 0);
if (lRef == 0)
{
m_cRef++;
delete this;
return ULONG(0);
} else
{
return ourmax(ULONG(m_cRef), 1ul);
}
}
#if (_MSC_VER > 1310)
volatile LONG m_cRef;
#else
LONG m_cRef;
#endif
};