如何通过仅知道其基指针来删除整个对象
How to delete whole object by knowing only its base-pointer
我正在尝试用 C++ 实现自动释放池,但在解除分配时遇到了问题。
所以我们有 root-class object:
class object {
public:
object() {
retainCount_ = 0;
}
~object() {
std::cout << "object " << Description() << " is dealocated" << std::endl;
}
/* code with retain/release here */
int retainCount() {
return retainCount_;
}
std::string Description() {
std::stringstream ss;
ss << this;
return ss.str();
}
private:
int retainCount_;
};
还有一些实现:
class Integer : public object {
public:
int i;
Integer(int ii) : i(ii) {}
~Integer() {
std::cout << "Integer " << Description() << " is dealocated" << std::endl;
}
};
当然还有释放池 class,它与 root-class 指针一起工作:
class release_pool {
public:
void insert(object* obj) {
pointers_.insert(obj);
}
void flush() {
std::set<object*>::iterator it = pointers_.begin();
std::set<object*>::iterator tmp;
const int N = pointers_.size();
for (int i = 0; i < N; i++) {
tmp = it;
it++;
if ((*tmp)->retainCount() == 0 ) {
object* obj = *tmp;
std::cout << "delete obj: " << obj->Description() << std::endl;
pointers_.erase(tmp);
delete obj;
}
}
}
private:
std::set<object*> pointers_;
};
main.cpp 测试代码:
int main () {
release_pool pool;
Integer* obj = new Integer(5);
pool.insert(obj);
std::cout << "before flush: " << obj->i << "\n";
pool.flush();
std::cout << "after flush: " << obj->i << "\n";
return 0;
}
构建后,我得到下一个:
before flush: 5
delete obj: 0x7f9a84c025d0
object 0x7f9a84c025d0 is dealocated
after flush: 5
最后:调用 root-class 的析构函数,但不调用 Integer 的析构函数。因此,我们泄漏了分配给 Integer 对象的内存。你有什么想法来解决它吗?我如何删除整个对象,而不是它的根部分。
您需要将对象析构函数设为虚拟。
我正在尝试用 C++ 实现自动释放池,但在解除分配时遇到了问题。 所以我们有 root-class object:
class object {
public:
object() {
retainCount_ = 0;
}
~object() {
std::cout << "object " << Description() << " is dealocated" << std::endl;
}
/* code with retain/release here */
int retainCount() {
return retainCount_;
}
std::string Description() {
std::stringstream ss;
ss << this;
return ss.str();
}
private:
int retainCount_;
};
还有一些实现:
class Integer : public object {
public:
int i;
Integer(int ii) : i(ii) {}
~Integer() {
std::cout << "Integer " << Description() << " is dealocated" << std::endl;
}
};
当然还有释放池 class,它与 root-class 指针一起工作:
class release_pool {
public:
void insert(object* obj) {
pointers_.insert(obj);
}
void flush() {
std::set<object*>::iterator it = pointers_.begin();
std::set<object*>::iterator tmp;
const int N = pointers_.size();
for (int i = 0; i < N; i++) {
tmp = it;
it++;
if ((*tmp)->retainCount() == 0 ) {
object* obj = *tmp;
std::cout << "delete obj: " << obj->Description() << std::endl;
pointers_.erase(tmp);
delete obj;
}
}
}
private:
std::set<object*> pointers_;
};
main.cpp 测试代码:
int main () {
release_pool pool;
Integer* obj = new Integer(5);
pool.insert(obj);
std::cout << "before flush: " << obj->i << "\n";
pool.flush();
std::cout << "after flush: " << obj->i << "\n";
return 0;
}
构建后,我得到下一个:
before flush: 5
delete obj: 0x7f9a84c025d0
object 0x7f9a84c025d0 is dealocated
after flush: 5
最后:调用 root-class 的析构函数,但不调用 Integer 的析构函数。因此,我们泄漏了分配给 Integer 对象的内存。你有什么想法来解决它吗?我如何删除整个对象,而不是它的根部分。
您需要将对象析构函数设为虚拟。