动态库头文件:
testlib.h
#include <vector>
using namespace std;
typedef struct
{
int left;
int right;
int top;
int bottom;
}KeyRect;
vector <KeyRect> tt;
int printvector();
动态库cpp文件:
testlib.cpp
#include "testlib.h"
#include <stdio.h>
int printvector()
{
KeyRect rct;
for(int i=0;i<5;i++)
{
rct.left = i;
rct.right =i+1;
rct.top = i+2;
rct.bottom = i+3;
tt.push_back(rct);
}
for(int j=0;j<tt.size();j++)
{
printf("the %dth elment in vector is %d %d %d %d\n",tt[j].left,tt[j].right,tt[j].top,tt[j].bottom);
}
return 0;
}
用g++ -fPIC -shared -g -o libtest.so testlib.cpp 命令编译生成libtest.so库,
测试用例cpp如下:
test.cpp
#include <stdio.h>
#include "testlib.h"
int main()
{
int result =0;
printvector();
return 0;
}
用g++ test.cpp ./libyang.so -g -o test 生成可移植文件test
用./test命令执行,打印如下:
the 0th elment in vector is 0,1,2,3
the 1th elment in vector is 1,2,3,4
the 2th elment in vector is 2,3,4,5
the 3th elment in vector is 3,4,5,6
the 4th elment in vector is 4,5,6,7
(注:以上五行打印为动态库中函数输出,正确,下面为异常打印)
*** glibc detected *** ./test: double free or corruption (out): 0x0940f090 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x65c591]
/lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0x65dde8]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x660ecd]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x567741]
./test(_ZN9__gnu_cxx13new_allocatorI7KeyRectE10deallocateEPS1_j+0x11)[0x8048d8f]
./test(_ZNSt12_Vector_baseI7KeyRectSaIS0_EE13_M_deallocateEPS0_j+0x25)[0x8048d61]
./test(_ZNSt12_Vector_baseI7KeyRectSaIS0_EED2Ev+0x38)[0x8048cca]
./test(_ZNSt6vectorI7KeyRectSaIS0_EED1Ev+0x39)[0x8048dd1]
/lib/tls/i686/cmov/libc.so.6(__cxa_finalize+0xb8)[0x620588]
./libtestlib.so(+0x1d04)[0x2a3d04]
./libtestlib.so(+0x2b80)[0x2a4b80]
/lib/ld-linux.so.2(+0xe2a6)[0x96e2a6]
/lib/tls/i686/cmov/libc.so.6(+0x2f1bf)[0x6201bf]
/lib/tls/i686/cmov/libc.so.6(+0x2f22f)[0x62022f]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xee)[0x607bde]
./test[0x8048b31]
======= Memory map: ========
002a2000-002a6000 r-xp 00000000 08:01 936760 /home/fy/code/ImageDll/libtestlib.so
002a6000-002a7000 r--p 00003000 08:01 936760 /home/fy/code/ImageDll/libtestlib.so
002a7000-002a8000 rw-p 00004000 08:01 936760 /home/fy/code/ImageDll/libtestlib.so
004ac000-00595000 r-xp 00000000 08:01 807509 /usr/lib/libstdc++.so.6.0.13
00595000-00599000 r--p 000e9000 08:01 807509 /usr/lib/libstdc++.so.6.0.13
00599000-0059a000 rw-p 000ed000 08:01 807509 /usr/lib/libstdc++.so.6.0.13
0059a000-005a1000 rw-p 00000000 00:00 0
005f1000-00744000 r-xp 00000000 08:01 264023 /lib/tls/i686/cmov/libc-2.11.1.so
00744000-00745000 ---p 00153000 08:01 264023 /lib/tls/i686/cmov/libc-2.11.1.so
00745000-00747000 r--p 00153000 08:01 264023 /lib/tls/i686/cmov/libc-2.11.1.so
00747000-00748000 rw-p 00155000 08:01 264023 /lib/tls/i686/cmov/libc-2.11.1.so
00748000-0074b000 rw-p 00000000 00:00 0
008a8000-008cc000 r-xp 00000000 08:01 264031 /lib/tls/i686/cmov/libm-2.11.1.so
008cc000-008cd000 r--p 00023000 08:01 264031 /lib/tls/i686/cmov/libm-2.11.1.so
008cd000-008ce000 rw-p 00024000 08:01 264031 /lib/tls/i686/cmov/libm-2.11.1.so
00960000-0097b000 r-xp 00000000 08:01 133390 /lib/ld-2.11.1.so
0097b000-0097c000 r--p 0001a000 08:01 133390 /lib/ld-2.11.1.so
0097c000-0097d000 rw-p 0001b000 08:01 133390 /lib/ld-2.11.1.so
00a6f000-00a8c000 r-xp 00000000 08:01 136632 /lib/libgcc_s.so.1
00a8c000-00a8d000 r--p 0001c000 08:01 136632 /lib/libgcc_s.so.1
00a8d000-00a8e000 rw-p 0001d000 08:01 136632 /lib/libgcc_s.so.1
00be3000-00be4000 r-xp 00000000 00:00 0 [vdso]
08048000-0804a000 r-xp 00000000 08:01 936765 /home/fy/code/ImageDll/test
0804a000-0804b000 r--p 00001000 08:01 936765 /home/fy/code/ImageDll/test
0804b000-0804c000 rw-p 00002000 08:01 936765 /home/fy/code/ImageDll/test
0940f000-09430000 rw-p 00000000 00:00 0 [heap]
b7700000-b7721000 rw-p 00000000 00:00 0
b7721000-b7800000 ---p 00000000 00:00 0
b7828000-b782a000 rw-p 00000000 00:00 0
b783b000-b783e000 rw-p 00000000 00:00 0
bf854000-bf869000 rw-p 00000000 00:00 0 [stack]
Aborted
通过gdb跟踪,用bt命令查看堆栈为:
#0 0x0012d422 in __kernel_vsyscall ()
#1 0x00298651 in raise () from /lib/tls/i686/cmov/libc.so.6
#2 0x0029ba82 in abort () from /lib/tls/i686/cmov/libc.so.6
#3 0x002cf49d in ?? () from /lib/tls/i686/cmov/libc.so.6
#4 0x002d9591 in ?? () from /lib/tls/i686/cmov/libc.so.6
#5 0x002dade8 in ?? () from /lib/tls/i686/cmov/libc.so.6
#6 0x002ddecd in free () from /lib/tls/i686/cmov/libc.so.6
#7 0x001ef741 in operator delete(void*) () from /usr/lib/libstdc++.so.6
#8 0x08048d8f in __gnu_cxx::new_allocator<KeyRect>::deallocate (this=0x804b030,
__p=0x804c090) at /usr/include/c++/4.4/ext/new_allocator.h:95
#9 0x08048d61 in std::_Vector_base<KeyRect, std::allocator<KeyRect> >::_M_deallocate (this=0x804b030, __p=0x804c090, __n=8)
at /usr/include/c++/4.4/bits/stl_vector.h:146
#10 0x08048cca in ~_Vector_base (this=0x804b030, __in_chrg=<value optimized out>)
at /usr/include/c++/4.4/bits/stl_vector.h:132
#11 0x08048dd1 in ~vector (this=0x804b030, __in_chrg=<value optimized out>)
at /usr/include/c++/4.4/bits/stl_vector.h:313
#12 0x0029d588 in __cxa_finalize () from /lib/tls/i686/cmov/libc.so.6
#13 0x0012fd04 in __do_global_dtors_aux () from ./libtestlib.so
#14 0x00130b80 in _fini () from ./libtestlib.so
#15 0x0011e2a6 in ?? () from /lib/ld-linux.so.2
#16 0x0029d1bf in ?? () from /lib/tls/i686/cmov/libc.so.6
#17 0x0029d22f in exit () from /lib/tls/i686/cmov/libc.so.6
#18 0x00284bde in __libc_start_main () from /lib/tls/i686/cmov/libc.so.6
#19 0x08048b31 in _start ()
其中第8行到第11行打印显示错误在STL Vector的头文件中,请大神指点为什么会出现这种问题,谢谢!
1 个解决方案
#1
./libyang.so 这是什么? 可能这里用的stl和你本机不匹配
#1
./libyang.so 这是什么? 可能这里用的stl和你本机不匹配