[英]Are shared_ptr on static objects good?
I'm wondering whether smart pointers on static objects are reasonable. 我想知道静态对象上的智能指针是否合理。 For example let's say I have some static resource and want to pass a reference to that static resource to some other objects which need these resource to work with.
例如,假设我有一些静态资源,并且想将对该静态资源的引用传递给其他需要使用这些资源的对象。
One way would be to use the RAW-Pointers pointing to that resource. 一种方法是使用指向该资源的RAW指针。 But now I'm wondering whether smart pointers (shared_ptr) are the better way and if so, how to do it correctly.
但是现在我想知道智能指针(shared_ptr)是否是更好的方法,如果是的话,如何正确地做到这一点。 (should the smart pointer be static as well?).
(智能指针也应该是静态的吗?)。
Background of the question: if no more objects holding a smart pointer anymore, the static object which the smart pointer is point to, would be freed (which is not the best idea...). 问题的背景:如果不再有任何持有智能指针的对象,则该智能指针指向的静态对象将被释放(这不是最好的主意...)。
One example (which ends in a crash at the end of runtime): 一个示例(在运行时结束时崩溃):
struct SomeType {
SomeType() { cout << "ctor..." << endl; }
~SomeType() { cout << "dtor..." << endl; }
void sayHello() { cout << "Hello!" << endl; }
};
void someFunction(shared_ptr<SomeType> smartPointer) {
smartPointer->sayHello();
}
static SomeType st;
static shared_ptr<SomeType> pt{ &st };
void anotherFunction() {
someFunction(pt);
}
int main() {
anotherFunction();
cin.get();
}
The following two lines are not valid. 以下两行无效。
static SomeType st;
static shared_ptr<SomeType> pt{ &st };
When pt
is destroyed at the end of your process' lifetime, it will delete st
. 当
pt
在您的进程生命周期结束时被销毁时,它将delete st
。 st
was never allocated with a matching new
. st
从未分配有匹配的new
。 This is undefined behavior. 这是未定义的行为。
shared_ptr
is useful for managing the complex lifetime of shared objects while static
objects have very simple lifetimes. shared_ptr
对于管理共享对象的复杂生存期非常有用,而static
对象的生存期非常简单。 It is incorrect to use shared_ptr
to manage static
objects and there would be nothing to gain in doing so. 使用
shared_ptr
管理static
对象是不正确的,这样做没有任何好处。
let's assume that I (as the class author) don't know whether the object is static or dynamic
假设我(作为类作者)不知道对象是静态的还是动态的
Code that needs to be agnostic absolutely should use shared_ptr<T>
. 绝对需要不可知的代码应使用
shared_ptr<T>
。
This code you gave is invalid, because it causes deletion of an object with static lifetime. 您提供的此代码无效,因为它会导致删除具有静态生存期的对象。
static SomeType st;
static shared_ptr<SomeType> pt{ &st };
This is just fine: 很好:
static SomeType st;
static shared_ptr<SomeType> pt{ std::shared_ptr<SomeType>{}, &st };
and that pt
can be used interchangeably with "normal" shared_ptr instances. 并且该
pt
可以与“普通” shared_ptr实例互换使用。 std::shared_ptr
is particularly convenient in this regard, because the presence or absence of a deleter doesn't affect the pointer type (in contrast, std::unique_ptr<T, custom_deleter<T>>
is a different type from `std::unique_ptr>) 在这方面,
std::shared_ptr
特别方便,因为是否存在删除器不会影响指针类型(相反, std::unique_ptr<T, custom_deleter<T>>
是与`std不同的类型: :的unique_ptr>)
This and other ways to specify a deleter that doesn't do anything are described at: 指定删除器不执行任何操作的这种方法和其他方法在以下位置进行了描述:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.