Free(): invalid pointer error when libtorch runs under Linux

[questions]

Libtorch GPU 1.2.0 and GCC version 5.4.0 are compiled under linux environment. The compilation is normal, but the following errors occur during operation:

*** 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

Query similar problems in the official issue of pytoch, which are related to C + + abi

C + + ABI related issues

When you upgrade GCC Version (above 5.1), if you recompile the whole code, you will basically not encounter ABI problems. However, if you also rely on dynamic libraries compiled by other third parties, which are compiled with the old version of GCC (such as GCC 4.8), you may encounter ABI problems. At this time, add a definition - D during compilation_ GLIBCXX_ USE_ CXX11_ ABI = 0 , then there should be no problem.

The GCC version of the libtorch GPU (1.2.0) tripartite library that the project relies on should be a running problem caused by still using the old abi

[solution]

Cmakelist.txt

add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)

After compiling and running, the problem is solved!

[note]

After configuring cmake, pay attention to GCC and cxx versions to ensure the consistency of versions

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

Confirm that the version is 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

(SINGING)

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

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

Read More: