0

我正在努力用 C++11 编写一个小文件句柄类。我知道 STL 中已经有很多可以实际处理文件的内容,但出于学习目的,我想自己做这件事。

不幸的是,我似乎不明白,异常对 C++ 程序的内存泄漏行为有什么影响,因为 Valgrind 告诉我,以下代码中有 2 个内存泄漏:

文件.h

#ifndef FILE_H
#define FILE_H

#include <iostream>
#include <memory>
#include <string>

#include <stdio.h>

class FileDeleter {
public:
    void operator()(FILE *p);
};

class File {
public:
    File(const std::string path);

private:
    const std::string _path;
    std::unique_ptr<FILE, FileDeleter> _fp;

};

#endif // FILE_H

文件.cpp

#include <cstring>
#include <iostream>
#include <stdexcept>
#include <utility>

#include <stdio.h>

#include "file.h"

void FileDeleter::operator()(FILE *p)
{
if (!p)
    return;

    if (fclose(p) == EOF)
        std::cerr << "FileDeleter: Couldn't close file" << std::endl;
}

File::File(const std::string path)
    : _path(std::move(path)),
      _fp(fopen(_path.c_str(), "a+"))
{
    if (!_fp)
        throw std::runtime_error("Couldn't open file");
}

主文件

#include <cstdlib>
#include "file.h"

int main()
{
    File my_file("/root/.bashrc");

    return EXIT_SUCCESS;
}

我确实选择打开 /root/.bashrc 是为了让 File ctor 抛出异常。如果我不扔在那里,Valgrind 会非常高兴。在使用异常时我在这里缺少什么?你如何“正确”实现一个简单的文件句柄(异常安全)?

提前致谢!

注意:仍然缺少读/写操作,因为我已经在基础知识上苦苦挣扎。

编辑#1: 这是实际的 Valgrind 输出,使用 --leak-check=full:

==7998== HEAP SUMMARY:
==7998==     in use at exit: 233 bytes in 3 blocks
==7998==   total heap usage: 5 allocs, 2 frees, 817 bytes allocated
==7998== 
==7998== 38 bytes in 1 blocks are possibly lost in loss record 1 of 3
==7998==    at 0x4C27CC2: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7998==    by 0x4EEC4F8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.18)
==7998==    by 0x4EEDC30: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/libstdc++.so.6.0.18)
==7998==    by 0x4EEE047: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.18)
==7998==    by 0x40137D: main (in ../a.out)
==7998== 
==7998== 43 bytes in 1 blocks are possibly lost in loss record 2 of 3
==7998==    at 0x4C27CC2: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7998==    by 0x4EEC4F8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.18)
==7998==    by 0x4EEDC30: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/libstdc++.so.6.0.18)
==7998==    by 0x4EEE047: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.18)
==7998==    by 0x400EBF: File::File(std::string) (in /home/frank/3Other/Code/Laboratory/c++/c++namedpipe/a.out)
==7998==    by 0x401390: main (in ../a.out)
==7998== 
==7998== 152 bytes in 1 blocks are possibly lost in loss record 3 of 3
==7998==    at 0x4C27730: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7998==    by 0x4E8F8F2: __cxa_allocate_exception (in /usr/lib/libstdc++.so.6.0.18)
==7998==    by 0x400E9B: File::File(std::string) (in /home/frank/3Other/Code/Laboratory/c++/c++namedpipe/a.out)
==7998==    by 0x401390: main (in ../a.out)
==7998== 
==7998== LEAK SUMMARY:
==7998==    definitely lost: 0 bytes in 0 blocks
==7998==    indirectly lost: 0 bytes in 0 blocks
==7998==      possibly lost: 233 bytes in 3 blocks
==7998==    still reachable: 0 bytes in 0 blocks
==7998==         suppressed: 0 bytes in 0 blocks

编辑#2: 修复了析构函数中抛出的异常。

编辑#3: 删除 FileException 类,改用 std::runtime_error 。

编辑#4: 在删除器中添加了 NULL 检查。

4

1 回答 1

2

我看到以下问题:

  1. fclose(NULL)不被允许 ...
    • 但幸运std::unique_ptr的是没有调用删除器 if get() == nullptr,所以这里应该没问题
  2. FileDeleter:operator()从析构函数调用,这意味着它不应该抛出. 在您的特定情况下,从 fclose 返回的任何错误都将导致std::terminate被调用
  3. 您的异常应该按值存储字符串,而不是引用。该引用是指在您调用时将被销毁的临时文件what()
    • 正如 Vlad & Praetorian 指出的那样,您可以删除有缺陷的代码而不是修复它,让我们std::runtime_error为您处理。TBH,除非您打算将一些特定于文件的数据添加到异常中,或者您打算单独捕获文件异常,否则您根本不需要这个类。

编辑:使用提供的 valgrind 输出,看起来堆栈展开从未完成。由于我最初关于FileDeleter::operator()被调用和抛出的想法看起来是错误的,所以一个合理的测试应该是:如果你在 main 的主体中插入一个 try/catch 会发生什么?


一般注意事项:

  1. 永远不要在析构函数中抛出异常
  2. 永远不要从析构函数中调用其他可能抛出的东西

因为如果在异常处理/堆栈展开期间调用您的析构函数,程序将立即终止。

于 2013-12-13T20:28:59.537 回答