Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未充分共享\u ptr 我正在阅读Scott Meyerses C++,现在在管理资源方面。他解释说,共享ptr是一个引用计数智能指针,它就像一个垃圾收集器,只是它不能打破引用循环。这是什么意思?什么是引用的中断周期 struct A { shared_ptr<A> p ; } ; if ( true ) // complete extra scope for this example, just to make these things go out of scope { shared_ptr<A> p1 = make_shared<A>() ; shared_ptr<A> p2 = make_shared<A>() ; p1->p = p2 ; p2->p = p1 ; } // At this point, they're out of scope and clearly won't be used again // However, they will NOT be destroyed because they both have a strong reference to each other_C++_Memory Management - Fatal编程技术网

未充分共享\u ptr 我正在阅读Scott Meyerses C++,现在在管理资源方面。他解释说,共享ptr是一个引用计数智能指针,它就像一个垃圾收集器,只是它不能打破引用循环。这是什么意思?什么是引用的中断周期 struct A { shared_ptr<A> p ; } ; if ( true ) // complete extra scope for this example, just to make these things go out of scope { shared_ptr<A> p1 = make_shared<A>() ; shared_ptr<A> p2 = make_shared<A>() ; p1->p = p2 ; p2->p = p1 ; } // At this point, they're out of scope and clearly won't be used again // However, they will NOT be destroyed because they both have a strong reference to each other

未充分共享\u ptr 我正在阅读Scott Meyerses C++,现在在管理资源方面。他解释说,共享ptr是一个引用计数智能指针,它就像一个垃圾收集器,只是它不能打破引用循环。这是什么意思?什么是引用的中断周期 struct A { shared_ptr<A> p ; } ; if ( true ) // complete extra scope for this example, just to make these things go out of scope { shared_ptr<A> p1 = make_shared<A>() ; shared_ptr<A> p2 = make_shared<A>() ; p1->p = p2 ; p2->p = p1 ; } // At this point, they're out of scope and clearly won't be used again // However, they will NOT be destroyed because they both have a strong reference to each other,c++,memory-management,C++,Memory Management,但是,如果这样做会发生什么 db.reset() ; // removes the last strong-reference to the database, so it is destroyed! db = record->parentDatabase() ; // tries to lock the weak_ptr, but fails because it's dead! So it can only return null, or throw an exception. 如果

但是,如果这样做会发生什么

db.reset() ; // removes the last strong-reference to the database, so it is destroyed!
db = record->parentDatabase() ; // tries to lock the weak_ptr, but fails because it's dead! So it can only return null, or throw an exception.
如果您将
弱\u ref
放在其他地方怎么办

class Database
{
public:
    // ...
    void shared_ptr<Record> recordNamed ( string const& name ) const ;
private:
    map<string,weak_ptr<Record> > mRecords ;
} ;

class Record
{
public:
    shared_ptr<Database> parentDatabase () const ;
    void reloadFromDataStore () const ; // reloads props from database
private:
    shared_ptr<Database> mpParentDatabase ;
    // maybe other data here too...
} ;

shared_ptr<Database> db = ... ; // get it from somewhere
shared_ptr<Record> record = db->recordNamed ( "christopher" ) ;
解决方案B 拥有拥有所有事物寿命的“代币”:

shared_ptr<LifespanToken> token = ... ;

Database* ptr = new Database ( token ) ;
Record * record = ptr->recordNamed ( "christopher" ) ;
shared_ptr令牌=;
数据库*ptr=新数据库(令牌);
记录*Record=ptr->recordNamed(“克里斯托弗”);

这个想法是代币拥有寿命。这是解决保留周期问题的手动方法,但这需要您了解人们将如何使用该系统!你必须事先知道很多。这在数据库的上下文中是有意义的,但您不希望对代码的每一部分都这样做。谢天谢地,循环主要出现在能够做到这一点的上下文中,如果A对B有一个
共享的
,而B对A有一个
共享的
,那么大多数循环都可以用
弱的\u ptr

修复,这就是一个循环。@MarkRansom我想在现实世界的例子中看到,多好的设计啊be@newone我没有一个具体的例子,但是你可以看到,当你想要一个父母有一个指向孩子的指针,但孩子需要一个指向父母的指针,这只是一个例子。所以,在你的例子中,我们最终导致内存泄漏?@stella是的,宝贝,这就是发生的事情,但是没有人设计这样的程序,你也很少需要一个共享的ptr。@newone-这显然是错误的;共享ptr被大量使用。从技术上讲,你是对的:它们是不需要的。你可以决定所有东西的寿命,但是你的开发时间会增加三倍。循环是出于必要而出现的(这是垃圾收集器存在的一个原因!),这就是弱\u ptr的起源。@iadcont这就是为什么我想在一个软件中看到一个真实世界的例子,在这个例子中,您实际上需要一个共享的\u ptr,并且除此之外,还需要循环依赖。在我看来,这是一个设计缺陷。你是说这种情况发生在自制项目还是商业软件中?我还没有遇到过这样的事情…@newone-我做的不是“自制的”,但也不是真正的“商业的”。但请相信我:这是一件大事(而且很重要)。我们不能浪费时间让像内存管理这样琐碎的事情占用50%的开发时间——我们还有更好的事情要做。我在我的帖子中添加了一个详细的例子,说明什么时候会出现周期。
class Database
{
public:
    // ...
    void shared_ptr<Record> recordNamed ( string const& name ) const ;
private:
    map<string,weak_ptr<Record> > mRecords ;
} ;

class Record
{
public:
    shared_ptr<Database> parentDatabase () const ;
    void reloadFromDataStore () const ; // reloads props from database
private:
    shared_ptr<Database> mpParentDatabase ;
    // maybe other data here too...
} ;

shared_ptr<Database> db = ... ; // get it from somewhere
shared_ptr<Record> record = db->recordNamed ( "christopher" ) ;
db->close() ; // removes all references to Records and resets their references to the database
shared_ptr<LifespanToken> token = ... ;

Database* ptr = new Database ( token ) ;
Record * record = ptr->recordNamed ( "christopher" ) ;