简体   繁体   English

奇怪的glibc检测到free()无效的指针错误

[英]Strange glibc detected free() invalid pointer error

I realise that there are a lot of questions on so with this error 我意识到这个错误有很多疑问

    *** glibc detected *** /usr/bin/obj_to_bob_debug: free(): invalid pointer: 0x0000000000bde130 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7ec66)[0x7f2d1f33fc66]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(_ZNSsD1Ev+0x23)[0x7f2d1fc31c13]
/usr/bin/obj_to_bob_debug[0x4023b7]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f2d1f2e278d]
/usr/bin/obj_to_bob_debug[0x402109]
======= Memory map: ========
00400000-00415000 r-xp 00000000 fc:00 1061803                            /usr/bin/obj_to_bob_debug
00614000-00615000 r--p 00014000 fc:00 1061803                            /usr/bin/obj_to_bob_debug
00615000-00616000 rw-p 00015000 fc:00 1061803                            /usr/bin/obj_to_bob_debug
00bde000-00bff000 rw-p 00000000 00:00 0                                  [heap]
7f2d1f2c1000-7f2d1f476000 r-xp 00000000 fc:00 275257                     /lib/x86_64-linux-gnu/libc-2.15.so
7f2d1f476000-7f2d1f676000 ---p 001b5000 fc:00 275257                     /lib/x86_64-linux-gnu/libc-2.15.so
7f2d1f676000-7f2d1f67a000 r--p 001b5000 fc:00 275257                     /lib/x86_64-linux-gnu/libc-2.15.so
7f2d1f67a000-7f2d1f67c000 rw-p 001b9000 fc:00 275257                     /lib/x86_64-linux-gnu/libc-2.15.so
7f2d1f67c000-7f2d1f681000 rw-p 00000000 00:00 0
7f2d1f681000-7f2d1f696000 r-xp 00000000 fc:00 262189                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f2d1f696000-7f2d1f895000 ---p 00015000 fc:00 262189                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f2d1f895000-7f2d1f896000 r--p 00014000 fc:00 262189                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f2d1f896000-7f2d1f897000 rw-p 00015000 fc:00 262189                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f2d1f897000-7f2d1f992000 r-xp 00000000 fc:00 275262                     /lib/x86_64-linux-gnu/libm-2.15.so
7f2d1f992000-7f2d1fb91000 ---p 000fb000 fc:00 275262                     /lib/x86_64-linux-gnu/libm-2.15.so
7f2d1fb91000-7f2d1fb92000 r--p 000fa000 fc:00 275262                     /lib/x86_64-linux-gnu/libm-2.15.so
7f2d1fb92000-7f2d1fb93000 rw-p 000fb000 fc:00 275262                     /lib/x86_64-linux-gnu/libm-2.15.so
7f2d1fb93000-7f2d1fc75000 r-xp 00000000 fc:00 1054000                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7f2d1fc75000-7f2d1fe74000 ---p 000e2000 fc:00 1054000                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7f2d1fe74000-7f2d1fe7c000 r--p 000e1000 fc:00 1054000                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7f2d1fe7c000-7f2d1fe7e000 rw-p 000e9000 fc:00 1054000                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7f2d1fe7e000-7f2d1fe93000 rw-p 00000000 00:00 0
7f2d1fe93000-7f2d1feb5000 r-xp 00000000 fc:00 275249                     /lib/x86_64-linux-gnu/ld-2.15.so
7f2d200a8000-7f2d200ad000 rw-p 00000000 00:00 0
7f2d200b2000-7f2d200b5000 rw-p 00000000 00:00 0
7f2d200b5000-7f2d200b6000 r--p 00022000 fc:00 275249                     /lib/x86_64-linux-gnu/ld-2.15.so
7f2d200b6000-7f2d200b8000 rw-p 00023000 fc:00 275249                     /lib/x86_64-linux-gnu/ld-2.15.so
7fffe2f9f000-7fffe2fc0000 rw-p 00000000 00:00 0                          [stack]
7fffe2ffe000-7fffe3000000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

Before I post up a load of code, I'll try to explain what I find strange about it... I compile my program (4 .cpp files and 3 .h file) with g++ and 95% of the time it runs fine. 在发布大量代码之前,我将尝试解释我对此有什么奇怪的地方……我使用g ++和95%的正常运行时间来编译我的程序(4个.cpp文件和3个.h文件) 。 It parses .obj files and writes out to my own binary format .bob. 它解析.obj文件,并写成我自己的二进制格式.bob。 On files where this error occurs it is consistent for the exact same call to it eg. 在发生此错误的文件上,对它的完全相同的调用是一致的,例如。

root@DevVm64-Liam:/# /usr/bin/obj_to_bob_debug /usr/local/apache2/htdocs/uploads/3dmodels/3/24/sample\ obj\ file.obj

However for any file that fails, changing the input file name's length makes it run fine eg. 但是对于任何失败的文件,更改输入文件名的长度可以使其正常运行,例如。

root@DevVm64-Liam:/# /usr/bin/obj_to_bob_debug /usr/local/apache2/htdocs/uploads/3dmodels/3/24/sample\ obj\ file1.obj

but changing the file name so that it is the same length the error will occur. 但是更改文件名以使其长度相同将发生错误。

This happens regardless of [properly escaped] spaces in the file name. 无论文件名中是否有[正确转义的]空格,都会发生这种情况。

Rather that posting up a load of code (I have no idea where in my code the error is occuring) Can anyone suggest why this might be happening? 而是发布大量的代码(我不知道错误在我的代码中的什么地方发生),有人可以建议为什么这可能发生吗? If necessary I can try to cut out the bulk of the code and post up the possible areas it could be happening. 如有必要,我可以尝试减少大量代码,并张贴可能发生的区域。

Note: I never call free or delete as I only use one or two auto_ptr 注意:我从不打免费或删除电话,因为我只使用一两个auto_ptr

Ive a feeling but I'm not sure that theres a problem with this bit of code 我有一种感觉,但我不确定这部分代码是否有问题

    string outFile;

    //if no output file given set to same as input file with obj replaced with bob
    if (argc == 2) {
        char* tempOutFile = new char[fineName.length()];
        strcpy(tempOutFile, (fileName.substr(0, fileName.length()-4) + ".bob").c_str());
        outFile.assign(tempOutFile);
        delete tempOutFile;
    } else {
        outFile = argv[2];
    }

I've been reading that there can be problems with strcpy but I don't see any problems with the code... 我一直在阅读strcpy可能有问题,但是我看不到代码有任何问题...

Pretty sure this is an undefined behaviour. 相当确定这是未定义的行为。 strcpy is copying to an uninitialized pointer. strcpy正在复制到未初始化的指针。

char* tempOutFile; // = ???
// now your copying from the sub-string to an unknown memory address
strcpy(tempOutFile, (fileName.substr(0, fileName.length()-4) + ".bob").c_str());

All of this to simply copy the substring to another string? 所有这些仅仅是将子字符串复制到另一个字符串? Just do so: 只要这样做:

string outFile;

//if no output file given set to same as input file with obj replaced with bob
if (argc == 2) {
    outFile = fileName.substr(0, fileName.length()-4) + ".bob";
} else {
    outFile = argv[2];
}

Also, don't forget that name use to call the program is stored in argv[0] . 另外,不要忘记用于调用程序的名称存储在argv[0] So if argc == 2 , your program really has only one argument, argv[1] . 因此,如果argc == 2 ,则您的程序实际上只有一个参数argv[1]

     $ ./prog arg1 arg2
argv   ^-- 0  ^--1 ^--2
argc == 3 (size of argv)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM