in my code i would like boost::shared_ptr not to call delete but call ptr->deleteMe() instead.
在我的代码中,我想boost :: shared_ptr不要调用delete但是调用ptr-> deleteMe()代替。
Also i have a few C styled functions that return a ptr. Can i make it call lib_freeXYZ(ptr); instead of trying to delete?
我还有一些C风格的函数返回一个ptr。我可以调用lib_freeXYZ(ptr);而不是试图删除?
4 个解决方案
#1
55
Or how about using the stl to provide the wrapper functor - Doug T. description but without the custom caller.
或者如何使用stl提供包装函数 - Doug T.描述但没有自定义调用者。
boost::shared_ptr<T> ptr( new T, std::mem_fun_ref(&T::deleteMe) );
boost::shared_ptr<S> ptr( new S, std::ptr_fun(lib_freeXYZ) );
#2
37
You can give the shared_ptr template a custom deleter function which has the signature
您可以为shared_ptr模板提供具有签名的自定义删除函数
void Deleter( T* ptr);
for a boost::shared_ptr
对于boost :: shared_ptr
So for Deleter you would do
所以对于Deleter你会这样做
boost::shared_ptr<T> ptrToT( new T, Deleter );
then in the body of Deleter:
然后在Deleter的身体:
void Deleter( T* ptr);
{
ptr->deleteMe();
// And make sure YOU ACTUALLY DELETE (or do whatever else you need to
// do to release the resource)
delete ptr;
}
For your specific case when you need something simple (like ptr->deleteMe) see Greg's solution, its very nice.
对于您需要简单的特定情况(如ptr-> deleteMe),请参阅Greg的解决方案,非常好。
#3
10
Doug T. answered your question nicely. I'll tell you about intrusive_ptr. Maybe you can use it in your project too.
Doug T.很好地回答了你的问题。我会告诉你关于intrusive_ptr的事。也许你也可以在你的项目中使用它。
If you have some C library that has already reference counting, but you have to manually call those functions, you can use boost::intrusive_ptr
too, and provide proper definitions for its add_ref and release functions. intrusive_ptr will find and call them. They are responsible to increment the reference count and decrement it, freeing the resource when necassary:
如果你有一些已经引用计数的C库,但你必须手动调用这些函数,你也可以使用boost :: intrusive_ptr,并为其add_ref和release函数提供正确的定义。 intrusive_ptr会找到并调用它们。他们负责增加引用计数并减少它,在需要时释放资源:
void intrusive_ptr_add_ref(foo *f) {
lib_add_ref(f);
}
void intrusive_ptr_release(foo *f) {
if(lib_dec_ref(f) == 0)
lib_free(f);
}
Then you can just create objects from raw pointers of type foo*
. intrusive_ptr will call your functions when its copied/destructed:
然后你可以从foo *类型的原始指针创建对象。 intrusive_ptr将在复制/销毁时调用您的函数:
intrusive_ptr<foo> f(lib_alloc());
// can wrap raw pointers too, which already may be referenced somewhere else
foo *p = get_foo_from_somewhere();
function_taking_intrusive_ptr(p);
#4
1
For the C-style data, do as @Doug. T suggested.
对于C风格的数据,请执行@Doug。 T建议。
For your class, why not do cleanup in a destructor? Even if this is including deleteMe() in the destructor.
对于你的班级,为什么不在析构函数中进行清理?即使这是在析构函数中包含deleteMe()。
#1
55
Or how about using the stl to provide the wrapper functor - Doug T. description but without the custom caller.
或者如何使用stl提供包装函数 - Doug T.描述但没有自定义调用者。
boost::shared_ptr<T> ptr( new T, std::mem_fun_ref(&T::deleteMe) );
boost::shared_ptr<S> ptr( new S, std::ptr_fun(lib_freeXYZ) );
#2
37
You can give the shared_ptr template a custom deleter function which has the signature
您可以为shared_ptr模板提供具有签名的自定义删除函数
void Deleter( T* ptr);
for a boost::shared_ptr
对于boost :: shared_ptr
So for Deleter you would do
所以对于Deleter你会这样做
boost::shared_ptr<T> ptrToT( new T, Deleter );
then in the body of Deleter:
然后在Deleter的身体:
void Deleter( T* ptr);
{
ptr->deleteMe();
// And make sure YOU ACTUALLY DELETE (or do whatever else you need to
// do to release the resource)
delete ptr;
}
For your specific case when you need something simple (like ptr->deleteMe) see Greg's solution, its very nice.
对于您需要简单的特定情况(如ptr-> deleteMe),请参阅Greg的解决方案,非常好。
#3
10
Doug T. answered your question nicely. I'll tell you about intrusive_ptr. Maybe you can use it in your project too.
Doug T.很好地回答了你的问题。我会告诉你关于intrusive_ptr的事。也许你也可以在你的项目中使用它。
If you have some C library that has already reference counting, but you have to manually call those functions, you can use boost::intrusive_ptr
too, and provide proper definitions for its add_ref and release functions. intrusive_ptr will find and call them. They are responsible to increment the reference count and decrement it, freeing the resource when necassary:
如果你有一些已经引用计数的C库,但你必须手动调用这些函数,你也可以使用boost :: intrusive_ptr,并为其add_ref和release函数提供正确的定义。 intrusive_ptr会找到并调用它们。他们负责增加引用计数并减少它,在需要时释放资源:
void intrusive_ptr_add_ref(foo *f) {
lib_add_ref(f);
}
void intrusive_ptr_release(foo *f) {
if(lib_dec_ref(f) == 0)
lib_free(f);
}
Then you can just create objects from raw pointers of type foo*
. intrusive_ptr will call your functions when its copied/destructed:
然后你可以从foo *类型的原始指针创建对象。 intrusive_ptr将在复制/销毁时调用您的函数:
intrusive_ptr<foo> f(lib_alloc());
// can wrap raw pointers too, which already may be referenced somewhere else
foo *p = get_foo_from_somewhere();
function_taking_intrusive_ptr(p);
#4
1
For the C-style data, do as @Doug. T suggested.
对于C风格的数据,请执行@Doug。 T建议。
For your class, why not do cleanup in a destructor? Even if this is including deleteMe() in the destructor.
对于你的班级,为什么不在析构函数中进行清理?即使这是在析构函数中包含deleteMe()。