11

I have been trying to debug a crash in my application that crashes (i.e. asserts a * glibc detected * free(): invalid pointer: 0x000000000070f0c0 ***) while I'm trying to do a simple assign to a string. Note that I'm compiling on a linux system with gcc 4.2.4 with an optimization level set to -O2. With -O0 the application no longer crashes.

E.g.

std::string abc;

abc = "testString";

but if I changed the code as follows it no longer crashes

std::string abc("testString");

So again I scratched my head! But the interesting pattern was that the crash moved later on in the application, AGAIN at another string. I found it weird that the application was continuously crashing on a string assign. A typical crash backtrace would look as follows:

#0  0x00007f2c2663bfb5 in raise () from /lib64/libc.so.6
(gdb) bt
#0  0x00007f2c2663bfb5 in raise () from /lib64/libc.so.6
#1  0x00007f2c2663dbc3 in abort () from /lib64/libc.so.6
#2  0x00000000004d8cb7 in people_streamingserver_sighandler (signum=6) at src/peoplestreamingserver.cpp:487
#3  <signal handler called>
#4  0x00007f2c2663bfb5 in raise () from /lib64/libc.so.6
#5  0x00007f2c2663dbc3 in abort () from /lib64/libc.so.6
#6  0x00007f2c26680ce0 in ?? () from /lib64/libc.so.6
#7  0x00007f2c270ca7a0 in std::string::assign (this=0x7f2c21bc8d20, __str=<value optimized out>)
    at /home/bbazso/ThirdParty/sources/gcc-4.2.4/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:238
#8  0x00007f2c21bd874a in PEOPLESProtocol::GetStreamName (this=<value optimized out>,
    pRawPath=0x2342fd8 "rtmp://127.0.0.1/mp4:pop.mp4", lStreamName=@0x7f2c21bc8d20)
    at /opt/trx-HEAD/gcc/4.2.4/lib/gcc/x86_64-pc-linux-gnu/4.2.4/../../../../include/c++/4.2.4/bits/basic_string.h:491
#9  0x00007f2c21bd9daa in PEOPLESProtocol::SignalProtocolCreated (pProtocol=0x233a4e0, customParameters=@0x7f2c21bc8de0)
    at peoplestreamer/src/peoplesprotocol.cpp:240

This was really weird behavior and so I started to poke around further in my application to see if there was some sort of memory corruption (either heap or stack) error that could be occurring that could be causing this weird behavior. I even checked for ptr corruptions and came up empty handed. In addition to visual inspection of the code I also tried the following tools:

  • Valgrind using both memcheck and exp-ptrcheck
  • electric fence
  • libsafe
  • I compiled with -fstack-protector-all in gcc
  • I tried MALLOC_CHECK_ set to 2
  • I ran my code through lint checks as well as cppcheck (to check for mistakes)
  • And I stepped through the code using gdb

So I tried a lot of stuff and still came up empty handed. So I was wondering if it could be something like a linker issue or a library issue of some sort that could be causing this problem. Are there any know issues with the std::string that make is susceptible to crashing in -O2 or maybe it has nothing to do with the optimization level? But the only pattern that I can see thus far in my problem is that it always seems to crash on a string and so I was wondering if anyone knew of any issues that my be causing this type of behavior.

Thanks a lot!

4
  • And valgrind says nothing? Can you give more detail on libc, libstdc++, kernel and Linux distro?
    – LiraNuna
    Feb 20, 2010 at 21:15
  • Can you show this: VANSProtocol::GetStreamName and this: VANSProtocol::SignalProtocolCreated?
    – Tom
    Feb 20, 2010 at 21:17
  • Can you try a newer GCC?
    – AndiDog
    Feb 20, 2010 at 21:18
  • maybe you should start ripping out chunks of code until it works again, so you can find out what what calls reproduce it. Not a clean solution, but I've done it for problems like this. Kinda like a binary search in your code by removing bits of code until you narrow it down.
    – Chris H
    Feb 20, 2010 at 21:21

4 Answers 4

10

This is an initial guess using all information I can extract from your back trace.

You are most likely mixing and matching gcc version, linker and libstdc++ that results an unusual behaviour on the host machine:

  1. libc is the system's: /lib64/libc.so.6
  2. libstdc++ is in a "ThirdParty" directory - this is suspicions, as it tells me it might be compiled elsewhere with a different target - /home/bbazso/ThirdParty/sources/gcc-4.2.4/x86_64-pc-linux-gnu/libstdc++-v3/
  3. Yet another libstdc++ in /opt: /opt/trx-HEAD/gcc/4.2.4/lib/gcc/x86_64-pc-linux-gnu/4.2.4/../../../../include/c++/4.2.4/bits/basic_string.h:491

In addition, GCC may mix the system's ld instead of itself which may cause further weird memory maps usage.

6
  • 3
    +1. Having 2 libstdc++s (however that happened) seems very likely to be the cause (or related to the cause) of the problem. I've had problems with passing std::strings across DSOs when using GNU's std::string implementation (it does some optimizations for the empty string that can cause issues) without the extra complications of multiple libstdc++s. Feb 20, 2010 at 22:46
  • I'm also wondering on the motive to not use distro's native GCC.
    – LiraNuna
    Feb 20, 2010 at 23:02
  • Thanks! This seems like a pretty good reason. I am in the midst of changing my build so that I only use one gcc and it will be the one from the native distro ubuntu's gcc 4.2.4-5. Also, could you go more into detail about the optimizations for an empty string or point me to a site that provides info on this? I'm interested in knowing what it does for an empty string that may cause this type of issue. Thanks again!
    – bbazso
    Feb 22, 2010 at 15:01
  • I changed my code to use the same gcc across the board and this still did not get rig of the crash. The optimization theory seems interesting, but I have yet to find any know issue for this kind of crash in gcc 4.2.4? I do know that if I add -fno-default-inline when compiling, this gets rid of the crash, but I have no idea why?
    – bbazso
    Feb 22, 2010 at 19:52
  • @bbazso: Are you sure your version of GCC uses it's own self-compiled libstdc++?
    – LiraNuna
    Feb 24, 2010 at 10:19
7

Can you repeat the crash with a basic two line program?

#include <string>

int main()
{
    std::string abc;
    abc = "testString";
}

If that crashes, please post your exact compile / link options?

If not, start paring down your code. Remove things lines a handful at a time until the bug goes away. Once you have some other change you can add to cause the crash and remove to make it go away, that should help you locate the problem.

2
  • I tested exactly that, with -O2 and -O0 and nothing.
    – Tom
    Feb 20, 2010 at 21:19
  • 2
    @Tom - okay, so take my next recommendation. Start paring down the rest of the code until the assignment no longer crashes. Feb 20, 2010 at 21:20
3

Happened to me because of using malloc for a class which had std::strings as data members. Tricky.

3
  • can you provide more information about this? I'm getting the same problems and I suspect this may be the reason, but I don't understand what's wrong with using malloc for a struct which has std::strings as data members.
    – 9a3eedi
    Jul 31, 2018 at 9:34
  • It turns out that you shouldn't use malloc to allocate structs that contain C++ code like classes, because malloc only takes in a numebr of bytes and does not understand C++'s stuff! You're supposed to use new for this kind of thing. Thanks for the headsup
    – 9a3eedi
    Jul 31, 2018 at 11:16
  • +1, this can also happen if using a raw memory operation (memset) on a class/struct that contains an std::string (seen this with GCC/libstdc++ does not happen with clang/libc++) Jan 2, 2021 at 17:58
0

As you said it's a weird behavior.

To be honnest with i think you are wasting time looking into a possible bug with std::strings. Strings are perfectly safe as long as you are using them well.

Anyway, with the informations you are giving : First, are you using threads ? It's might be a thread problem. Second, you check your program using valgrind. Have you no warnings at all ?

Note : The most critical valgrind's warnings are invalid read and invalid write.

PS : As said in commentary, you should probably use g++ to compile C++ code ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.