RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1530280
Accepted
Shamus Rezol
Shamus Rezol
Asked:2023-07-13 00:56:51 +0000 UTC2023-07-13 00:56:51 +0000 UTC 2023-07-13 00:56:51 +0000 UTC

未调用派生类虚拟析构函数

  • 772

有一种解决方案,其中动态库(SharedLibrary)提供接口(抽象类),另一个项目提供其实现(派生类)。最小示例概念:

解决方案的概念结构

该类Website为放置 URL 的缓冲区分配 64 KB。此外,在不离开构造函数的情况下,模拟异常。预期会Website调用析构函数,但仅调用基类析构函数InternetResource:

Website::ctor
InternetResource::dtor
main::catch 'Oops!'

因此,分配的内存没有被释放:

预期网站实例删除后调试内存泄漏

解决方案

来源

//
// Application/Main.cpp
//

#include <iostream>

#include "Website.hpp"

int main()
{
    try
    {
        __debugbreak(); // for heap snapshot (1)
        Website site("https://stackoverflow.com"); // throws
        return 1; // unattainable
    }
    catch (const std::exception& e)
    {
        std::cout
            << "main::catch '"
            << e.what()
            << "'"
            << std::endl;
    }
    __debugbreak(); // for heap snapshot (2)
    return 0;
}

//
// Application/Website.hpp
//

#pragma once

#include <SharedLibrary/InternetResource.hpp>

class Website : public InternetResource
{
public:
    Website(const char* url);

    virtual ~Website();

    void* Buffer = nullptr;
};

//
// Application/Website.cpp
//

#include "Website.hpp"

#include <iostream>
#include <memory>

Website::Website(const char* url)
{
    std::cout << "Website::ctor" << std::endl;
    this->Buffer = malloc(1 << 16); // 64 kB
    memcpy_s(this->Buffer, 1 << 16, url, strlen(url));
    throw std::exception("Oops!");
}

Website::~Website()
{
    std::cout << "Website::dtor" << std::endl;
    free(this->Buffer);
    this->Buffer = nullptr;
}

//
// SharedLibrary/Dll.hpp
//

#pragma once

#ifdef SHARED_LIBRARY_EXPORTS
#   define SHARED_LIBRARY_API __declspec(dllexport)
#else
#   define SHARED_LIBRARY_API __declspec(dllimport)
#endif

//
// SharedLibrary/InternetResource.hpp
//

#pragma once

#include "Dll.hpp"

class SHARED_LIBRARY_API InternetResource
{
public:
    virtual ~InternetResource();
};

//
// SharedLibrary/InternetResource.cpp
//

#include "InternetResource.hpp"

#include <iostream>

InternetResource::~InternetResource()
{
    std::cout << "InternetResource::dtor" << std::endl;
}
--
-- Premake5.lua
--
workspace "WhatTheDtor"
    location("Build/")

    configurations { "Debug" }
    platforms { "x86" }

function cpp_commons()
    language "C++"
    cppdialect "C++17"
    
    defines { "DEBUG" }
    symbols "On"

    runtime "Debug"
    staticruntime "Off" -- /MDd

    exceptionhandling ("Default") -- /EHsc
end

project "SharedLibrary"
    kind "SharedLib"
    cpp_commons()
    
    files { "./SharedLibrary/**.cpp", "./SharedLibrary/**.hpp" }
    
    defines { "SHARED_LIBRARY_EXPORTS" }

project "Application"
    kind "ConsoleApp"
    cpp_commons()
    
    files { "./Application/**.cpp", "./Application/**.hpp" }

    includedirs { "." }
    dependson { "SharedLibrary" }
    links { "SharedLibrary" }

回放

  1. Application通过和收集文件SharedLibrary。
  2. 使用 premake5 创建解决方案文件。
premake5 vs2022
  1. 打开“./Build/WhatTheDtor.sln”。
  2. 使用堆分析构建并开始调试。
  3. 在嵌入断点处拍摄堆的两个快照。

就这样。在这里您可以看到 64 KB 的未释放内存。

c++
  • 2 2 个回答
  • 44 Views

2 个回答

  • Voted
  1. Best Answer
    Stanislav Volodarskiy
    2023-07-13T06:59:30Z2023-07-13T06:59:30Z

    下面是一个示例,说明如何创建void *一个缓冲区,如果构造函数抛出异常,该缓冲区将正常释放。void *隐藏在unique_ptr.

    总的想法是复杂对象的字段应该照顾好自己。然后编译器将在构造函数中生成正确的异常处理代码。

    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <memory>
    
    using namespace std;
    
    void freeVoid(void* data) {
        cout << "freeVoid\n";
        free(data);
    }
    
    class Website {
    public:
        Website(const char* url) : buffer(malloc(1 << 16), freeVoid) {
            cout << "Website::ctor\n";
            memcpy(buffer.get(), url, strlen(url) + 1);
            // cout << static_cast<char *>(buffer.get()) << '\n';
            throw exception();
        }
    
        virtual ~Website() {
            cout << "Website::dtor\n";
        }
    
        unique_ptr<void, decltype(freeVoid) *> buffer;
    };
    
    int main() {
        try {
            Website w("site");
        } catch (exception &e) {
            cout << "caught\n";
        }
    }
    
    $ g++ website.cpp && ./a.out 
    Website::ctor
    freeVoid
    caught
    
    • 1
  2. Shamus Rezol
    2023-07-13T00:56:51Z2023-07-13T00:56:51Z

    文档说:

    …展开堆栈的过程开始。这涉及到在与 catch 处理程序关联的 try 块的开头和异常的抛出站点之间完全构造但尚未销毁的所有自动对象的销毁。

    MSDN:C++ 中的异常和堆栈展开[项目 5]

    换句话说,如果构造函数中抛出异常,那么除了将整个构造函数放入 try-catch 中之外,没有其他方法可以释放资源。例如:

    Website::Website(const char* url)
    {
        try
        {
            std::cout << "Website::ctor" << std::endl;
            this->Buffer = malloc(1 << 16); // 64 kB
            memcpy_s(this->Buffer, 1 << 16, url, strlen(url));
            throw std::exception("Oops!");
        }
        catch (...)
        {
            std::cout << "**forcing dtor from ctor**" << std::endl;
            this->~Website();
            throw;
        }
    }
    
    • -3

相关问题

  • 编译器和模板处理

  • 指针。找到最小数量

  • C++,关于枚举类对象初始化的问题

  • 函数中的二维数组

  • 无法使用默认构造函数创建类对象

  • C++ 和循环依赖

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5