首页 > 解决方案 > 无效的 free(): boost::filesystem::path::~path()

问题描述

我正在使用 g++ 6.3.0 编译器在 CentOS 7.5.1804 上开发一个程序

当我运行程序时,它返回一个错误:

-bash-4.2$ ./lost_drv 
*** Error in `./lost_drv': free(): invalid pointer: 0x0000000002375cf8 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81499)[0x7f5c65e85499]
./lost_drv(_ZN5boost10filesystem4pathD2Ev+0x18)[0x521564]
/lib64/libc.so.6(__cxa_finalize+0x9a)[0x7f5c65e3df4a]
/lib64/libboost_filesystem.so.1.53.0(+0x7573)[0x7f5c68317573]
======= Memory map: ========

Valgrind 揭示了 boost::filesystem::path 析构函数的问题

-bash-4.2$ valgrind ./lost_drv 
==32523== Memcheck, a memory error detector
==32523== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==32523== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==32523== Command: ./lost_drv
==32523== 
==32523== Invalid free() / delete / delete[] / realloc()
==32523==    at 0x4C2B1CD: operator delete(void*) (vg_replace_malloc.c:576)
==32523==    by 0x521563: boost::filesystem::path::~path() (path.hpp:55)
==32523==    by 0x73DBF49: __cxa_finalize (in /usr/lib64/libc-2.17.so)
==32523==    by 0x5053572: ??? (in /usr/lib64/libboost_filesystem.so.1.53.0)
==32523==    by 0x40101C9: _dl_fini (in /usr/lib64/ld-2.17.so)
==32523==    by 0x73DBBD8: __run_exit_handlers (in /usr/lib64/libc-2.17.so)
==32523==    by 0x73DBC26: exit (in /usr/lib64/libc-2.17.so)
==32523==    by 0x73C444B: (below main) (in /usr/lib64/libc-2.17.so)

错误发生在最后的 return 语句之后,因为一切都在清理。该程序显然在此之前运行良好。

这是一个扩展程序,但我通过在主语句之后立即放置一个 return 语句来简化它:

int main(int argc, char *argv[])
{
 return 0;
...

我正在使用 boost::filesystem 来检查路径完整性......这样的代码:

try
    {
     boost::filesystem::path fn(ifilenm);
     if(!boost::filesystem::exists(fn) || !boost::filesystem::is_regular_file(fn))
        {
         cout << "ERROR: Input file does not exist or isn't a file:\t" << ifilenm << "\n";
         return -2;
        }
    }
 catch (const boost::filesystem::filesystem_error& ex)
    {
     cout << "ERROR: Failed accessing input file.  File doesn't exist or isn't a file or isn't accesible:\t" << ifilenm << "\n";
     return -3;
    }

当然,在这种退化的情况下不会运行。在执行之前也没有初始化任何全局 boost 变量。

我已经尝试过使用系统 boost install (1.53) 以及针对使用 g++ 6.3 编译器构建的 boost 1.59 的静态和动态链接。相同的行为。

还有一点……这个可执行文件的另一个版本通过链接到其他一些第 3 方库来访问一些附加功能。那个版本没有这个问题。

另一个数据点 - 如果我使用默认的 CentOS 7 编译器(g++ 4.8.5)重建整个业务,我不会收到此错误。但这不是一个整体的选择,因为用于完整 monty 的 3rd 方库需要 g++ 6

知道发生了什么吗?谢谢

标签: c++boostfree

解决方案


推荐阅读