[英]boost shared_ptr casting to void*
I'm using libev which requires casting my data to a void* to be in compliance with their predefined structs. 我正在使用libev,这需要将我的数据强制转换为void *以符合其预定义的结构。 I need to cast boost::shared_ptr to a void* and then cast the void* back to a boost::shared_ptr.
我需要将boost :: shared_ptr转换为void *,然后将void *转换为boost :: shared_ptr。 Here is my code to do that
这是我的代码
void foo(boost::shared_ptr<string>& a_string)
{
void* data = (void*)a_string.get();
boost::shared_ptr<string> myString((string*)data);
}
I'm pretty sure this works fine, however the way my code is setup I believe all shared_ptr references to my string are going out of scope as this casting method does not increase the use_count and thus shared_ptr is freeing the memory while I still need it. 我很确定这可以正常工作,但是我的代码设置方式相信我对字符串的所有shared_ptr引用都超出了范围,因为此强制转换方法不会增加use_count,因此shared_ptr会释放内存,而我仍然需要它。
Is there a way to manually increment/decrement the use_count? 有没有办法手动增加/减少use_count? Ideally I would increment the use_count when I cast to a void*, pass my void* to another function, cast the void* back to a shared_ptr and decrement the use_count.
理想情况下,当我强制转换为void *时,我会增加use_count,将void *传递给另一个函数,将void *强制转换为shared_ptr,然后递减use_count。
Or if anyone knows another solution to this problem I could use any help. 或者,如果有人知道该问题的另一种解决方案,我可以使用任何帮助。
这样做的唯一真正方法是将一个shared_ptr
分配到可以生存很长时间的某个位置,然后将void*
设置为指向该位置。
If you cast a void*
back to a boost::shared_ptr
, it will be a new shared pointer, not linked to any other shared pointers that also point to the memory pointed to by the `void*
variable. 如果将
void*
boost::shared_ptr
回boost::shared_ptr
,它将是一个新的共享指针,不链接到任何其他共享指针,这些指针也指向由`void*
指向的内存。
What I think you need to do is add enabled_shared_from_this
support to the classes you are thinking of using with shared_ptrs with that code. 我认为您需要做的就是将
enabled_shared_from_this
支持添加到您想与该代码一起用于shared_ptrs的类。
This allows you to get a shared_ptr that will share ownership with existing shared_ptrs through a member function ( shared_from_this
) on your class. 这使您可以获得一个shared_ptr,它将通过类上的成员函数(
shared_from_this
)与现有的shared_ptrs共享所有权。
See the boost enabled_shared_from_this docs for more details. 有关更多详细信息,请参见boosted enabled_shared_from_this文档 。
Try a weak_ptr
: 试试一个
weak_ptr
:
shared_ptr<int> p(new int(5));
weak_ptr<int> q(p);
Edit: Sorry I did not read the question properly; 编辑:对不起,我没有正确阅读问题; you could try to keep it in scope so it does not get cleaned up
您可以尝试将其保留在范围内,以免被清理
Update: A weak pointer may actually work if you reference the variable after your call because then the compiler cant optimise the destruction of the a_string
shared_ptr (hence prevent decr refcount to zero -> release) before you make use of the underlying pointer 更新:如果在调用后引用变量,弱指针实际上可能会起作用,因为在使用基础指针之前,编译器无法优化
a_string
shared_ptr的销毁(因此可将decr a_string
设为零->发布)。
so you could do this: 所以你可以这样做:
void foo(boost::shared_ptr<string>& a_string)
{
void* data = (void*)a_string.get();
boost::shared_ptr<string> myString((string*)data);
weak_ptr<string> temp(a_string); // prevent destruction before above line
// or reference a_string in any meaningless way that CANT be optimised out
// pre-emptively by the compiler
}
a_string
might still need to referenced somewhere outside of foo
depending on the context and what you are doing with the void pointer (and if it creates a new copy or operates on the void data) 根据上下文以及您对void指针的操作(以及是否创建新副本或对void数据进行操作),
a_string
可能仍需要引用foo
之外的某个位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.