c++中的智能指针引用计数
Smart pointer reference counting in C++
我正在阅读Safe c++ ,我对引用计数有疑问。问题在下面的代码注释部分提供?
#include <iostream>
using namespace std;
template <typename T>class RefCountPtr {
public:
explicit RefCountPtr(T* p = NULL) {
Create(p);
}
RefCountPtr(const RefCountPtr<T>& rhs) {
Copy(rhs);
}
RefCountPtr<T>& operator=(const RefCountPtr<T>& rhs) {
if(ptr_ != rhs.ptr_) {
Kill();
Copy(rhs);
}
return *this;
}
RefCountPtr<T>& operator=(T* p) {
if(ptr_ != p) {
Kill();
Create(p);
}
return *this;
}
~RefCountPtr() {
Kill();
}
T* Get() const {
return ptr_;
}
T* operator->() const {
// SCPP_TEST_ASSERT(ptr_ != NULL, "Attempt to use operator -> on NULL pointer.");
return ptr_;
}
T& operator* () const {
// SCPP_TEST_ASSERT(ptr_ != NULL, "Attempt to use operator * on NULL pointer.");
return *ptr_;
}
int GetCount() { return *count_; }
private:
T* ptr_;
int* count_;
void Create(T* p) {
ptr_ = p;
if(ptr_ != NULL) {
count_ = new int;
*count_ = 1;
} else {
count_ = NULL;
}
}
void Copy(const RefCountPtr<T>& rhs) {
ptr_ = rhs.ptr_;
count_ = rhs.count_;
if(count_ != NULL)
++(*count_);
}
void Kill() {
if(count_ != NULL) {
if(--(*count_) == 0) {
delete ptr_;
delete count_;
}
}
}
};
void main()
{
int* pFirstInt = new int;
RefCountPtr<int> refPointer(pFirstInt);
cout << "count: " << refPointer.GetCount() << std::endl;
RefCountPtr<int> refSecondPointer = refPointer; // this calls copy constructor.
cout << "second count: " << refSecondPointer.GetCount() << std::endl;
RefCountPtr<int> refThirdPointer;
refThirdPointer = pFirstInt; **// Question why are we not incrementing same int pointer? How can we correct this?
// If we are not incrementing same pointer why author has provided
// operator = for data type T?
// Note: As expected program is crashing while exiting as we are deleting int pointer twice.**
std::cout << "Third pointer: " << refThirdPointer.GetCount() << std::endl;
RefCountPtr<int> refFourthPointer;
refFourthPointer = refSecondPointer;
cout << "Fourth count: " << refFourthPointer.GetCount() << std::endl;
return;
}
/*
count: 1
second count: 2
Third pointer: 1
Fourth count: 3
Press any key to continue . . .
*/
问题是:
refThirdPointer = pFirstInt;
从一个已经被另一个智能指针管理的原始指针进行赋值。
因此,你最终有两个不相关的智能指针试图管理pFirstInt
。它们是不相关的,因为它们有各自的引用计数,并且幸福地不知道彼此的存在。这会导致您正在观察的双重删除。
相关文章:
- C++取消引用指针.为什么会发生变化
- 深层复制具有自引用指针的类
- Visual c ++,使用字符串引用/指针调用 dll 函数
- std::unordered_map::提取引用/指针失效
- 为什么在引用指针时将 const 放在 & 符号的左侧有效,而在右侧则无效?
- 区分接受常量参数的函数引用/指针和与函数参数同名的非常量参数
- 如何在 c++ 中使用带有数学运算的引用/指针?
- 了解通过引用传递取消引用指针时C++堆/堆栈分配
- 取消引用指针并立即为其分配变量,导致分段错误
- 如何获取指向类(而不是对象)的引用/指针
- 将类型参数传递给自引用指针
- 我应该如何定义返回指针的函数?(引用指针与指针指针)
- 在析构函数内取消引用指针时出现分段错误
- 来自引用指针的内存泄漏
- 引用指针后面的值
- 无法取消引用指针
- 引用指针调用成员函数
- 未定义的引用指针的变量模板在clang中功能,而不是GCC
- 取消引用指针以创建数组的副本
- 有关启动引用指针的引用的问题