【发布时间】:2014-09-23 02:55:20
【问题描述】:
我想基于引用计数器为脚本引擎做某种垃圾收集:
class HeapValue
{
private:
size_t _refCount;
public:
HeapValue()
: _refCount( 0 )
{ }
virtual ~HeapValue() { }
inline void reference() throw()
{
++this->_refCount;
}
inline void unreference() throw()
{
if( 0 == --this->_refCount )
{
delete this;
}
}
};
但我的对象不仅是 HeapValues,它们也是 Scopes:
class Scope
{
protected:
std::map< std::string, Value > _vars;
public:
inline Value & getRef( const std::string & name ) throw()
{
return this->_vars[ name ];
}
inline Value getCpy( const std::string & name ) const
{
std::map< std::string, Value >::const_iterator itr = this->_vars.find( name );
if( this->_vars.end() != itr )
{
return itr->second;
}
else
{
throw std::exception( "Scope::getCpy() - Accessing undeclared indentifier in readonly mode." );
}
}
};
class Object : public Scope, public HeapValue
{
};
假设我创建了一个对象,当 HeapValue 类删除自身时会发生什么?我假设它不会调用 Scope 析构函数,因为 Scope 不是 HeapValue ?
谢谢你:)
编辑:添加类对象定义
编辑:
我的 Value 类是一个变体:
class Value
{
private:
union
{
int _i;
double _r;
std::string * _s; // Owned/copied
Object * _o; // Not owned/copied
}
_data;
// etc...
};
还有:
class HeapValue
{
//...
inline void reference() throw()
{
++this->_refCount;
}
inline void unreference() throw()
{
--this->_refCount )
}
};
Value & Value::operator = ( const Value & val )
{
switch( this->_type )
{
// ...
case E_OBJECT :
switch( val._type )
{
// ...
case E_INTEGER :
this->_data._o->unreference();
if( this->_data._o->getRefCount() == 0 ) delete this->_data._o; // Deletion moved here, outside of HeapValue ?
this->_data._i = val._data.i;
this->_type = E_INTEGER;
break;
// ...
}
}
// ...
}
【问题讨论】:
-
抱歉,我忘记了那段代码。
-
Scope没有虚拟析构函数,这看起来很危险——你如何删除你的对象? -
我不会使用 Scope 指针删除对象。当它们的引用计数器下降到 0 时,这些对象将被删除。我也可以向 Scope 添加一个空的析构函数,但它会改变什么吗?
-
代码不知道哪些对象在堆栈上,哪些不在。如果您调用 unreference() 并且 _refCount 为 0,它将尝试删除它。结果是未定义的(未定义的行为)。这可能会导致各种错误,通常是不可取的。
-
是的,但我不打算在堆栈上创建它们。
标签: c++ inheritance polymorphism virtual destructor