我將對象(TTF_Font
)存儲在shared_ptr
中,該對象是通過第三方API提供給我的。我不能在對象上使用new或delete,所以shared_ptr
也提供了一個「釋放」函數。使用.reset()釋放單獨擁有的boost :: shared_ptr
// Functor
struct CloseFont
{
void operator()(TTF_Font* font) const
{
if(font != NULL) {
TTF_CloseFont(font);
}
}
};
boost::shared_ptr<TTF_Font> screenFont;
screenFont = boost::shared_ptr<TTF_Font>(TTF_OpenFont("slkscr.ttf", 8), CloseFont());
如果,以後,我要明確地釋放該對象是正確的做到這一點:
screenFont.reset();
然後讓screenFont
(實際shared_ptr
對象)來破壞自然?
感謝您的回覆。就我而言,我必須在另一個資源之前明確地釋放它,這是我發現的唯一途徑。 – 2008-10-01 05:55:54
只需要清楚 - reset()不會釋放持有的資源,除非它導致refcount降至零 - 它不會強制將refcount設置爲零。 – 2008-10-01 05:59:08