2

我已经在个人解析器上工作了几个月了。在每一步,我都验证了它能够使用 clang++ 或 g++ 编译和正常工作。从现在开始就可以了(这个项目不使用除 STL 之外的任何其他库)。但现在我想把这个项目分成两部分:一个共享库和一个只有 main.cpp 文件的程序。我想这样做是因为“真正的”项目是库,可执行文件只是应用程序的一个示例和/或测试库的一种方式。

注意:我编译库时没有警告:

  • 对于每个 cpp 文件:g++/clang++ -STD=c++11 -Wall -fPIC -c file.cpp -o file.o
  • 对于库: g++/clang++ -STD=c++11 -Wall -fPIC -shared -Wl,-soname,libname.so.0 files.o... -o libname.so.0.1

当我使用与库相同的编译器编译程序时,一切都很好。但是当我用另一个编译它(仍然没有警告)时,应用程序在执行时崩溃:

*** Error in `./math': double free or corruption (fasttop): 0x0000000000f55910 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7b1d6)[0x7f80d76eb1d6]
./libtssa.so.0(__gnu_cxx::new_allocator<std::shared_ptr<TextNode> >::deallocate(std::shared_ptr<TextNode>*, unsigned long)+0x20)[0x7f80d82baee0]
./libtssa.so.0(std::_Vector_base<std::shared_ptr<TextNode>, std::allocator<std::shared_ptr<TextNode> > >::_M_deallocate(std::shared_ptr<TextNode>*, unsigned long)+0x3e)[0x7f80d82bae3e]
./libtssa.so.0(std::_Vector_base<std::shared_ptr<TextNode>, std::allocator<std::shared_ptr<TextNode> > >::~_Vector_base()+0x2c)[0x7f80d82badcc]
./libtssa.so.0(std::vector<std::shared_ptr<TextNode>, std::allocator<std::shared_ptr<TextNode> > >::~vector()+0x50)[0x7f80d82bad40]
./libtssa.so.0(std::vector<std::shared_ptr<TextNode>, std::allocator<std::shared_ptr<TextNode> > >::~vector()+0x15)[0x7f80d82bace5]
./libtssa.so.0(Node<TextNode>::~Node()+0x15)[0x7f80d82bacc5]
./libtssa.so.0(TextNode::~TextNode()+0x31)[0x7f80d82baca1]
./libtssa.so.0(TextNode::~TextNode()+0x15)[0x7f80d82bac65]
./libtssa.so.0(void __gnu_cxx::new_allocator<TextNode>::destroy<TextNode>(TextNode*)+0x19)[0x7f80d82bac49]
./libtssa.so.0(_ZNSt16allocator_traitsISaI8TextNodeEE10_S_destroyIS0_EENSt9enable_ifIXsr16__destroy_helperIT_EE5valueEvE4typeERS1_PS5_+0x28)[0x7f80d82bac28]
./libtssa.so.0(void std::allocator_traits<std::allocator<TextNode> >::destroy<TextNode>(std::allocator<TextNode>&, TextNode*)+0x1d)[0x7f80d82babed]
./libtssa.so.0(std::_Sp_counted_ptr_inplace<TextNode, std::allocator<TextNode>, (__gnu_cxx::_Lock_policy)1>::_M_dispose()+0x1f)[0x7f80d82ba99f]
./math[0x4079ce]
./math[0x40744f]
./math[0x407106]
./math(std::shared_ptr<TextNode>::~shared_ptr()+0x18)[0x407120]
./libtssa.so.0(void std::_Destroy<std::shared_ptr<TextNode> >(std::shared_ptr<TextNode>*)+0x15)[0x7f80d82baf85]
./libtssa.so.0(void std::_Destroy_aux<false>::__destroy<std::shared_ptr<TextNode>*>(std::shared_ptr<TextNode>*, std::shared_ptr<TextNode>*)+0x2f)[0x7f80d82baf4f]
./libtssa.so.0(void std::_Destroy<std::shared_ptr<TextNode>*>(std::shared_ptr<TextNode>*, std::shared_ptr<TextNode>*)+0x1d)[0x7f80d82baf0d]
./libtssa.so.0(void std::_Destroy<std::shared_ptr<TextNode>*, std::shared_ptr<TextNode> >(std::shared_ptr<TextNode>*, std::shared_ptr<TextNode>*, std::allocator<std::shared_ptr<TextNode> >&)+0x21)[0x7f80d82bad81]
./libtssa.so.0(std::vector<std::shared_ptr<TextNode>, std::allocator<std::shared_ptr<TextNode> > >::~vector()+0x3f)[0x7f80d82bad2f]
./libtssa.so.0(std::vector<std::shared_ptr<TextNode>, std::allocator<std::shared_ptr<TextNode> > >::~vector()+0x15)[0x7f80d82bace5]
./libtssa.so.0(Node<TextNode>::~Node()+0x15)[0x7f80d82bacc5]
./libtssa.so.0(TextNode::~TextNode()+0x31)[0x7f80d82baca1]
./libtssa.so.0(TextNode::~TextNode()+0x15)[0x7f80d82bac65]
./libtssa.so.0(void __gnu_cxx::new_allocator<TextNode>::destroy<TextNode>(TextNode*)+0x19)[0x7f80d82bac49]
./libtssa.so.0(_ZNSt16allocator_traitsISaI8TextNodeEE10_S_destroyIS0_EENSt9enable_ifIXsr16__destroy_helperIT_EE5valueEvE4typeERS1_PS5_+0x28)[0x7f80d82bac28]
./libtssa.so.0(void std::allocator_traits<std::allocator<TextNode> >::destroy<TextNode>(std::allocator<TextNode>&, TextNode*)+0x1d)[0x7f80d82babed]
./libtssa.so.0(std::_Sp_counted_ptr_inplace<TextNode, std::allocator<TextNode>, (__gnu_cxx::_Lock_policy)1>::_M_dispose()+0x1f)[0x7f80d82ba99f]
./math[0x4079ce]
./math[0x40744f]
./math[0x407106]
./math(std::shared_ptr<TextNode>::~shared_ptr()+0x18)[0x407120]
./libtssa.so.0(Alternative::analyse(Analysis&, unsigned long) const+0x4a5)[0x7f80d82bdd15]
./libtssa.so.0(Optionnal::analyse(Analysis&, unsigned long) const+0xe5)[0x7f80d82bfed5]
./libtssa.so.0(Sequence::analyse(Analysis&, unsigned long) const+0x11f)[0x7f80d82c034f]
./libtssa.so.0(Sequence::analyse(Analysis&, unsigned long) const+0x11f)[0x7f80d82c034f]
./libtssa.so.0(Alternative::analyse(Analysis&, unsigned long) const+0x228)[0x7f80d82bda98]
./libtssa.so.0(Sequence::analyse(Analysis&, unsigned long) const+0x11f)[0x7f80d82c034f]
./libtssa.so.0(Alternative::analyse(Analysis&, unsigned long) const+0x228)[0x7f80d82bda98]
./libtssa.so.0(Sequence::analyse(Analysis&, unsigned long) const+0x11f)[0x7f80d82c034f]
./libtssa.so.0(Alternative::analyse(Analysis&, unsigned long) const+0x228)[0x7f80d82bda98]
./libtssa.so.0(Sequence::analyse(Analysis&, unsigned long) const+0x11f)[0x7f80d82c034f]
./libtssa.so.0(Alternative::analyse(Analysis&, unsigned long) const+0x228)[0x7f80d82bda98]
./libtssa.so.0(Sequence::analyse(Analysis&, unsigned long) const+0x11f)[0x7f80d82c034f]
./libtssa.so.0(Alternative::analyse(Analysis&, unsigned long) const+0x228)[0x7f80d82bda98]
./libtssa.so.0(Sequence::analyse(Analysis&, unsigned long) const+0x11f)[0x7f80d82c034f]
./libtssa.so.0(Alternative::analyse(Analysis&, unsigned long) const+0x228)[0x7f80d82bda98]
./libtssa.so.0(Sequence::analyse(Analysis&, unsigned long) const+0x11f)[0x7f80d82c034f]
./libtssa.so.0(Alternative::analyse(Analysis&, unsigned long) const+0x228)[0x7f80d82bda98]
./libtssa.so.0(Sequence::analyse(Analysis&, unsigned long) const+0x11f)[0x7f80d82c034f]
./libtssa.so.0(Alternative::analyse(Analysis&, unsigned long) const+0x228)[0x7f80d82bda98]
./libtssa.so.0(Sequence::analyse(Analysis&, unsigned long) const+0x11f)[0x7f80d82c034f]
./libtssa.so.0(Alternative::analyse(Analysis&, unsigned long) const+0x228)[0x7f80d82bda98]
./libtssa.so.0(Sequence::analyse(Analysis&, unsigned long) const+0x11f)[0x7f80d82c034f]
./libtssa.so.0(Alternative::analyse(Analysis&, unsigned long) const+0x228)[0x7f80d82bda98]
./libtssa.so.0(Sequence::analyse(Analysis&, unsigned long) const+0x11f)[0x7f80d82c034f]
./libtssa.so.0(Alternative::analyse(Analysis&, unsigned long) const+0x228)[0x7f80d82bda98]
./libtssa.so.0(Sequence::analyse(Analysis&, unsigned long) const+0x11f)[0x7f80d82c034f]
./libtssa.so.0(Alternative::analyse(Analysis&, unsigned long) const+0x228)[0x7f80d82bda98]
./libtssa.so.0(Sequence::analyse(Analysis&, unsigned long) const+0x11f)[0x7f80d82c034f]
./libtssa.so.0(Alternative::analyse(Analysis&, unsigned long) const+0x228)[0x7f80d82bda98]
======= Memory map: ========
00400000-0040d000 r-xp 00000000 08:06 28180757                           /home/philippe/Documents/developpement/analyse_syntaxique/exemples/math/build/math
0060c000-0060d000 r--p 0000c000 08:06 28180757                           /home/philippe/Documents/developpement/analyse_syntaxique/exemples/math/build/math
0060d000-0060e000 rw-p 0000d000 08:06 28180757                           /home/philippe/Documents/developpement/analyse_syntaxique/exemples/math/build/math
00ee8000-00f6c000 rw-p 00000000 00:00 0                                  [heap]
7f80d7670000-7f80d7819000 r-xp 00000000 08:01 130189                     /usr/lib64/libc-2.17.so
7f80d7819000-7f80d7a18000 ---p 001a9000 08:01 130189                     /usr/lib64/libc-2.17.so
7f80d7a18000-7f80d7a1c000 r--p 001a8000 08:01 130189                     /usr/lib64/libc-2.17.so
7f80d7a1c000-7f80d7a1e000 rw-p 001ac000 08:01 130189                     /usr/lib64/libc-2.17.so
7f80d7a1e000-7f80d7a22000 rw-p 00000000 00:00 0 
7f80d7a22000-7f80d7a37000 r-xp 00000000 08:01 131583                     /usr/lib64/libgcc_s-4.7.2.so.1
7f80d7a37000-7f80d7c36000 ---p 00015000 08:01 131583                     /usr/lib64/libgcc_s-4.7.2.so.1
7f80d7c36000-7f80d7c37000 r--p 00014000 08:01 131583                     /usr/lib64/libgcc_s-4.7.2.so.1
7f80d7c37000-7f80d7c38000 rw-p 00015000 08:01 131583                     /usr/lib64/libgcc_s-4.7.2.so.1
7f80d7c38000-7f80d7d35000 r-xp 00000000 08:01 130197                     /usr/lib64/libm-2.17.so
7f80d7d35000-7f80d7f34000 ---p 000fd000 08:01 130197                     /usr/lib64/libm-2.17.so
7f80d7f34000-7f80d7f35000 r--p 000fc000 08:01 130197                     /usr/lib64/libm-2.17.so
7f80d7f35000-7f80d7f36000 rw-p 000fd000 08:01 130197                     /usr/lib64/libm-2.17.so
7f80d7f36000-7f80d801a000 r-xp 00000000 08:01 131588                     /usr/lib64/libstdc++.so.6.0.17
7f80d801a000-7f80d821a000 ---p 000e4000 08:01 131588                     /usr/lib64/libstdc++.so.6.0.17
7f80d821a000-7f80d8222000 r--p 000e4000 08:01 131588                     /usr/lib64/libstdc++.so.6.0.17
7f80d8222000-7f80d8224000 rw-p 000ec000 08:01 131588                     /usr/lib64/libstdc++.so.6.0.17
7f80d8224000-7f80d8239000 rw-p 00000000 00:00 0 
7f80d8239000-7f80d8310000 r-xp 00000000 08:06 28180741                   /home/philippe/Documents/developpement/analyse_syntaxique/build/libtssa.so.0.1
7f80d8310000-7f80d8510000 ---p 000d7000 08:06 28180741                   /home/philippe/Documents/developpement/analyse_syntaxique/build/libtssa.so.0.1
7f80d8510000-7f80d8511000 r--p 000d7000 08:06 28180741                   /home/philippe/Documents/developpement/analyse_syntaxique/build/libtssa.so.0.1
7f80d8511000-7f80d8517000 rw-p 000d8000 08:06 28180741                   /home/philippe/Documents/developpement/analyse_syntaxique/build/libtssa.so.0.1
7f80d8517000-7f80d8536000 r-xp 00000000 08:01 130182                     /usr/lib64/ld-2.17.so
7f80d8714000-7f80d871a000 rw-p 00000000 00:00 0 
7f80d8732000-7f80d8735000 rw-p 00000000 00:00 0 
7f80d8735000-7f80d8736000 r--p 0001e000 08:01 130182                     /usr/lib64/ld-2.17.so
7f80d8736000-7f80d8738000 rw-p 0001f000 08:01 130182                     /usr/lib64/ld-2.17.so
7fff03120000-7fff03142000 rw-p 00000000 00:00 0                          [stack]
7fff031fe000-7fff03200000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

在整个项目中我除了通过模板函数make_shared(...)之外没有做任何动态分配,所以我认为可能原因的数量非常有限。

问题 :

  • 有什么明显的我应该检查的。
  • 你有什么建议可以帮助我自己找到这个错误,因为我真的不习惯阅读回溯,或者通过 STL 进行调试,或者除了在每一行写 cout 之外的任何其他技术,现在是我开始学习的时候了(我使用 GNU/Linux 并且不想使用任何非自由工具)。
4

0 回答 0