Linux下Libtorch运行出现free(): invalid pointer报错

【问题】

Linux环境下编译Libtorch-gpu 1.2.0,GCC版本5.4.0,编译正常,但运行时出现以下报错:

*** Error in `./xxx/xxx/xxx': free(): invalid pointer: 0x00007f52a2101c50 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81329)[0x7f520ec08329]
/xxx/build/lib/libobject_detector.so(_ZN8nvinfer115PluginRegistrarINS_17YoloPluginCreatorEED2Ev+0x48)[0x7f52a1eeeb68]
/lib64/libc.so.6(__cxa_finalize+0x9a)[0x7f520ebc105a]
/data/hxy/git/6s_factory_analysis_ddb/code/build/lib/libobject_detector.so(+0x17123)[0x7f52a1ec8123]
======= Memory map: ========
00400000-00484000 r-xp 00000000 08:01 144443223
00684000-00687000 r--p 00084000 08:01 144443223
00687000-00688000 rw-p 00087000 08:01 144443223
00688000-00689000 rw-p 00000000 00:00 0 
00919000-107cb9000 rw-p 00000000 00:00 0                                 [heap]
200000000-200200000 ---p 00000000 00:00 0 
200200000-200400000 rw-s 00000000 00:05 40161                            /dev/nvidiactl

查询Pytorch官方Issue中类似问题,问题与C++ ABI相关

C++ ABI相关问题

当你升级GCC版本(5.1以上),如果把整个代码都全部重新编译一遍,那么你基本上不会遇到ABI问题的。但是如果你还依赖于其他第三方编译出来的动态库,而它们是用老版本的GCC(如:GCC 4.8)编译出来的,那么就有可能会遇到ABI的问题。这个时候,在编译的时候添加一个定义-D_GLIBCXX_USE_CXX11_ABI=0,那么应该就不会有什么问题了。

工程依赖的Libtorch-gpu(1.2.0)三方库编译时的GCC版本应该时是仍然使用旧的ABI导致的运行问题

【解决办法】

CMakeList.txt中添加

add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)

编译运行后,问题解决!

【注意】

配置cmake后,注意gcc和cxx版本,确保版本一致性

cmake -DCMAKE_CXX_COMPILER=$(which g++) -DCMAKE_C_COMPILER=$(which gcc) ..

确认版本都为5.4.0

-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/local/bin/gcc
-- Check for working C compiler: /usr/local/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/local/bin/g++
-- Check for working CXX compiler: /usr/local/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done

引用:

https://zhuanlan.zhihu.com/p/125197727

https://github.com/pytorch/pytorch/issues/30507

上一篇:qt5.15.2 的坑


下一篇:CMake编译32位版本时出现“cannot find -lXXX”错误的两种可能情况