如何修复内存泄漏

发布于 2025-02-12 00:59:02 字数 741 浏览 0 评论 0原文

我正在使用Pointer(New Ext ...)的项目进行研究,但我不知道如何修复它,我无法使用删除语法,因为它会从字面上打破代码。

list<Virus*> DoClone() 
{
    list<Virus*> l;

    Dengue *d1 = new Dengue(1), *d2 = new Dengue(1);
    for (int i = 0; i < 4; i++)
        d1->m_protein[i] = m_protein[i];
    d1->m_dna = m_dna;
    d1->m_resistance = m_resistance;
    for (int i = 0; i < 4; i++)
        d2->m_protein[i] = m_protein[i];
    d2->m_dna = m_dna;
    d2->m_resistance = m_resistance;

    l.emplace_back(d1);
    l.emplace_back(d2);
    //delete d1;
    //delete d2;
    return l;
}

void DoDie()
{
    this->m_dna = NULL;
    memset(this->m_protein, 0, 4);
    this->m_resistance = 0;
    delete this->m_dna;
}

I am working on the project which using pointer (new ext...) and I don't know how to fix it, I couldn't use the delete syntax because it will break the code literally.

list<Virus*> DoClone() 
{
    list<Virus*> l;

    Dengue *d1 = new Dengue(1), *d2 = new Dengue(1);
    for (int i = 0; i < 4; i++)
        d1->m_protein[i] = m_protein[i];
    d1->m_dna = m_dna;
    d1->m_resistance = m_resistance;
    for (int i = 0; i < 4; i++)
        d2->m_protein[i] = m_protein[i];
    d2->m_dna = m_dna;
    d2->m_resistance = m_resistance;

    l.emplace_back(d1);
    l.emplace_back(d2);
    //delete d1;
    //delete d2;
    return l;
}

void DoDie()
{
    this->m_dna = NULL;
    memset(this->m_protein, 0, 4);
    this->m_resistance = 0;
    delete this->m_dna;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天旳寂寞 2025-02-19 00:59:03
list<Virus*> DoClone() 
{
    //Why are you using a list of pointers?
    //Are you handing these out?
    list<Virus*> l; // Replace list<Virus> l or use smart pointers

    Dengue *d1 = new Dengue(1), *d2 = new Dengue(1);
    for (int i = 0; i < 4; i++){
        d1->m_protein[i] = m_protein[i];
    }
    //if the move semantics of any of them are wrong your code will crash, 
    //but you don't show us what these are.
    d1->m_dna = m_dna;
    d1->m_resistance = m_resistance;
    for (int i = 0; i < 4; i++)
        d2->m_protein[i] = m_protein[i];
    d2->m_dna = m_dna;
    d2->m_resistance = m_resistance;

    l.emplace_back(d1);
    l.emplace_back(d2);

    //You definitely shouldn't be deleting these you jsut gave them to a container to hold!
    //delete d1;
    //delete d2;
    return l;
}

void DoDie(ist<Virus*>& myList)
{
    //You set this to nullptr, but try to delete it in 4 lines time. This looks like a leak
    //Also the fact it was a pointer means you definitely copied it wrong above
    //use std::shared_ptr or std::weak_ptr if you want to share things.
    this->m_dna = NULL;
    memset(this->m_protein, 0, 4);
    
    this->m_resistance = 0;
    delete this->m_dna;

    //Lastly you need to free the memory in your container
    for(auto* item : myList){
     delete item;
    }
    myList.clear();

}
list<Virus*> DoClone() 
{
    //Why are you using a list of pointers?
    //Are you handing these out?
    list<Virus*> l; // Replace list<Virus> l or use smart pointers

    Dengue *d1 = new Dengue(1), *d2 = new Dengue(1);
    for (int i = 0; i < 4; i++){
        d1->m_protein[i] = m_protein[i];
    }
    //if the move semantics of any of them are wrong your code will crash, 
    //but you don't show us what these are.
    d1->m_dna = m_dna;
    d1->m_resistance = m_resistance;
    for (int i = 0; i < 4; i++)
        d2->m_protein[i] = m_protein[i];
    d2->m_dna = m_dna;
    d2->m_resistance = m_resistance;

    l.emplace_back(d1);
    l.emplace_back(d2);

    //You definitely shouldn't be deleting these you jsut gave them to a container to hold!
    //delete d1;
    //delete d2;
    return l;
}

void DoDie(ist<Virus*>& myList)
{
    //You set this to nullptr, but try to delete it in 4 lines time. This looks like a leak
    //Also the fact it was a pointer means you definitely copied it wrong above
    //use std::shared_ptr or std::weak_ptr if you want to share things.
    this->m_dna = NULL;
    memset(this->m_protein, 0, 4);
    
    this->m_resistance = 0;
    delete this->m_dna;

    //Lastly you need to free the memory in your container
    for(auto* item : myList){
     delete item;
    }
    myList.clear();

}
徒留西风 2025-02-19 00:59:02

智能指针进行营救:

list<std::unique_ptr<Virus>> DoClone() 
{
    list<std::unique_ptr<Virus>> l;

    auto d1 = std::make_unique<Dengue>(1);
    auto d2 = std::make_unique<Dengue>(1);

    for (int i = 0; i < 4; i++)
        d1->m_protein[i] = m_protein[i];

    d1->m_dna = m_dna;
    d1->m_resistance = m_resistance;
    for (int i = 0; i < 4; i++)
        d2->m_protein[i] = m_protein[i];
    d2->m_dna = m_dna;
    d2->m_resistance = m_resistance;

    l.emplace_back(std::move(d1));
    l.emplace_back(std::move(d2));

    return l;
}

void DoDie()
{
    m_dna.reset();
    memset(this->m_protein, 0, 4);
    this->m_resistance = 0;
}

如果您尽快使用智能指针,最好是。 这是关于主题的好讲座

另请参阅:
<

R.20:使用unique_ptrsharon_ptr代表所有权

原因

他们可以防止资源泄漏。

示例

考虑:

  void f()
{
    x x;
    x* p1 {new x}; //另请参阅???
    unique_ptr&lt; x&gt; p2 {new x}; //独特的所有权;另请参阅???
    sharon_ptr&lt; x&gt; p3 {new x}; //共享所有权;另请参阅???
    auto p4 = make_unique&lt; x&gt;(); // unique_ownership,比显式使用“新”优先
    auto p5 = make_shared&lt; x&gt;(); //共享所有权,比显式使用“新”可取
}
 

这将泄露用于初始化p1(仅)的对象。

执行

(简单)警告如果new的返回值或指针类型的返回值的函数调用已分配给原始指针。

额外:

小重构:

std::unique_ptr<Dengue> createDengue(int x)
{
    auto d = std::make_unique<Dengue>(x);
    d->m_protein = m_protein; // someone claims this is an std::array so loop is not needed
    d->m_dna = m_dna;
    d->m_resistance = m_resistance;
    return d;
}

list<std::unique_ptr<Virus>> DoClone() 
{
    list<std::unique_ptr<Virus>> l;
    l.emplace_back(createDengue(1));
    l.emplace_back(createDengue(1));
    return l;
}

void DoDie()
{
    m_dna.reset();
    this->m_protein = {}; // someone claims this is an std::array so  this is fine
    this->m_resistance = 0;
}

Smart pointers to the rescue:

list<std::unique_ptr<Virus>> DoClone() 
{
    list<std::unique_ptr<Virus>> l;

    auto d1 = std::make_unique<Dengue>(1);
    auto d2 = std::make_unique<Dengue>(1);

    for (int i = 0; i < 4; i++)
        d1->m_protein[i] = m_protein[i];

    d1->m_dna = m_dna;
    d1->m_resistance = m_resistance;
    for (int i = 0; i < 4; i++)
        d2->m_protein[i] = m_protein[i];
    d2->m_dna = m_dna;
    d2->m_resistance = m_resistance;

    l.emplace_back(std::move(d1));
    l.emplace_back(std::move(d2));

    return l;
}

void DoDie()
{
    m_dna.reset();
    memset(this->m_protein, 0, 4);
    this->m_resistance = 0;
}

It would be best if you learn to use smart pointers ASAP. Here is good lecture on topic.

Also see:
C++ Core Guidelines

R.20: Use unique_ptr or shared_ptr to represent ownership

Reason

They can prevent resource leaks.

Example

Consider:

void f()
{
    X x;
    X* p1 { new X };              // see also ???
    unique_ptr<X> p2 { new X };   // unique ownership; see also ???
    shared_ptr<X> p3 { new X };   // shared ownership; see also ???
    auto p4 = make_unique<X>();   // unique_ownership, preferable to the explicit use "new"
    auto p5 = make_shared<X>();   // shared ownership, preferable to the explicit use "new"
}

This will leak the object used to initialize p1 (only).

Enforcement

(Simple) Warn if the return value of new or a function call with return value of pointer type is assigned to a raw pointer.

Extra:

small refactor:

std::unique_ptr<Dengue> createDengue(int x)
{
    auto d = std::make_unique<Dengue>(x);
    d->m_protein = m_protein; // someone claims this is an std::array so loop is not needed
    d->m_dna = m_dna;
    d->m_resistance = m_resistance;
    return d;
}

list<std::unique_ptr<Virus>> DoClone() 
{
    list<std::unique_ptr<Virus>> l;
    l.emplace_back(createDengue(1));
    l.emplace_back(createDengue(1));
    return l;
}

void DoDie()
{
    m_dna.reset();
    this->m_protein = {}; // someone claims this is an std::array so  this is fine
    this->m_resistance = 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文