2

我对 shared_ptr.reset() 的行为有疑问。

在这种情况下,我有以下类的循环引用。我有一本书和一个所有者,它们彼此都有 std::shared_ptrs,创建了一个循环引用。

书.h

#pragma once

class Owner;

class Book
{
public:
    Book(std::string title);
    ~Book();
    void OutputDetails();
    void SetOwner(std::shared_ptr<Owner> owner);
    void OutputOwnerInformation();
private:
    std::string m_title;
    std::shared_ptr<Owner> m_owner; // Book hangs onto the owner and creates a circular dependency
};

书本.cpp

#include "stdafx.h"
#include <iostream>
#include "Book.h"
#include "Owner.h"

Book::Book(std::string title) : m_title(title) {}

Book::~Book() {
    std::cout << "Book Destroyed" << std::endl;
}

void Book::SetOwner(std::shared_ptr<Owner> owner) {
    m_owner = owner; // strong reference
}

void Book::OutputOwnerInformation() {
    std::cout << "Owner is: " << m_owner->GetName() << std::endl;
}

void Book::OutputOwnerInformation() {
    std::cout << "Owner is: " << m_owner->GetName() << std::endl;
}

所有者.h

#pragma once

class Book; // To avoid circular #includes

class Owner
{
public:
    Owner(std::string name, std::shared_ptr<Book> book);
    ~Owner();
    void OutputDetails();
    std::string GetName();
private:
    std::string m_name;
    std::shared_ptr<Book> m_book; // Owner hangs onto the book
};

所有者.cpp

#include "stdafx.h"
#include "Owner.h"
#include "Book.h"

Owner::Owner(std::string name, std::shared_ptr<Book> book) : m_name(name), m_book(book) {}

Owner::~Owner() {
    std::cout << "Owner Destroyed" << std::endl;
}

void Owner::OutputDetails() {
    std::cout << m_name << " owns " << std::endl;
    m_book->OutputDetails();
}

std::string Owner::GetName() {
    return m_name;
}

这是main.cpp。在这种情况下, book 和 owner 之间存在强引用,一旦 _tmain 退出其作用域,就会发生内存泄漏。当我在各自的析构函数中插入断点时,不会调用 book 和 owner 的析构函数。

主文件

#include "stdafx.h"
#include <memory>
#include "Book.h"
#include "Owner.h"

int _tmain(int, _TCHAR*)
{
    {
        std::shared_ptr<Book> book = std::shared_ptr<Book>(new Book("Moby Dick"));
        std::shared_ptr<Owner> owner = std::shared_ptr<Owner>(new Owner("George Heriot", book));

        // Introduced a circular dependency so
        // neither gets deleted
        book->SetOwner(owner);

        owner->OutputDetails();
        book->OutputOwnerInformation();
    }

    return 0;
}

我想看看我是否可以重置()指针,以便调用析构函数并打破循环依赖。根据我对 shared_ptr.reset() 的理解,对象应该是空的。

http://www.cplusplus.com/reference/memory/shared_ptr/reset/

但是,我在两个析构函数中的断点都没有被击中。我的假设是,因为我已经重置了 book 和 owner,所以两者的引用计数都会下降到 0,并且当 _tmain 返回时它们会被销毁。

main2.cpp

#include "stdafx.h"
#include <memory>
#include "Book.h"
#include "Owner.h"

int _tmain(int, _TCHAR*)
{
    {
        std::shared_ptr<Book> book = std::shared_ptr<Book>(new Book("Moby Dick"));
        std::shared_ptr<Owner> owner = std::shared_ptr<Owner>(new Owner("George Heriot", book));

        // Introduced a circular dependency so
        // neither gets deleted
        book->SetOwner(owner);

        owner->OutputDetails();
        book->OutputOwnerInformation();

        owner.reset();
        book.reset();
    }

    return 0;
}

我知道这已经是很糟糕的代码,我可以使用 weak_ptr 来删除循环依赖,但我只是好奇为什么 reset() 不会破坏这种依赖。

4

1 回答 1

4

尝试打印owner.use_count()book.use_count()在重置它们之前。您将看到使用计数为 2。重置调用将使它们ownerbook计数减 1,但仍有其他shared_ptr对象与它们共享所有权并且您没有重置,因此引用计数不会达到零.

如果您考虑一下,您应该意识到当然reset()不能打破循环,因为无论如何都reset()发生在shared_ptr析构函数中。如果析构函数可以像那样破坏循环,那么首先创建循环就没有问题。

于 2014-12-18T15:52:18.617 回答