Author Topic: Sometimes when I add a watch to a std::string member I get a segmentation fault  (Read 6105 times)

Offline oded_sharon

  • Single posting newcomer
  • *
  • Posts: 3
I am using CodeBlocks 13.12 rev 9.501 on windows with MinGW32 GCC 4.8.1 and GDB 4.6.1

Quite often if I try to add a watch for a std::string variable I will get a Segmentation fault. printing the value in GDB works fine.

====
Here is the output of GDB copied from codeblock
Quote
Building to ensure sources are up-to-date
Selecting target:
Debug
Adding source dir: C:\Cpp\cocos2d-x-2.2.1\projects\WorldInvasion\
Adding source dir: C:\Cpp\cocos2d-x-2.2.1\projects\WorldInvasion\
Adding file: C:\Cpp\cocos2d-x-2.2.1\projects\WorldInvasion\bin\WorldInvasion.exe
Changing directory to: C:/Cpp/cocos2d-x-2.2.1/projects/WorldInvasion/.
Set variable: PATH=.;C:\MinGW_Lib\lib;C:\MinGW\bin;C:\MinGW;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Intel\iCLS Client;C:\Program Files\Intel\iCLS Client;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;C:\Program Files\Microsoft SQL Server\110\Tools\Binn;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin;C:\Program Files (x86)\CMake 2.8\bin;C:\Python27;C:\Program Files (x86)\premake;C:\Cpp\android-ndk-r9c;C:\Cpp\adt-bundle-windows-x86_64-20131030\sdk\tools;C:\Cpp\adt-bundle-windows-x86_64-20131030\sdk\platform-tools;.
Starting debugger: C:\MinGW\bin\gdb.exe -nx -fullname  -quiet  -args C:/Cpp/cocos2d-x-2.2.1/projects/WorldInvasion/bin/WorldInvasion.exe
done
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
Debugger name and version: GNU gdb (GDB) 7.6.1
Child process PID: 6460
At C:\Cpp\cocos2d-x-2.2.1\cocos2dx\tilemap_parallax_nodes\CCTMXXMLParser.cpp:368
> p m_sTMXFileName
$1 = {
  static npos = <optimized out>,
  _M_dataplus = {
    <std::allocator<char>> = {
      <__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>},
    members of std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Alloc_hider:
    _M_p = 0x548d84 "C:\\Cpp\\cocos2d-x-2.2.1\\projects\\WorldInvasion\\Resources/Maps/prototypeMap.tmx"
  }
}

The moment I add the same parameter m_sTMXFileName to the list of watches I get notified of a  Segmentation fault and the following output in the debugger log:

Quote
Program received signal SIGSEGV, Segmentation fault.
In libstdc++-6!_ZNKSs4sizeEv () (C:\MinGW\bin\libstdc++-6.dll)
Program received signal SIGSEGV, Segmentation fault.
Program received signal SIGSEGV, Segmentation fault.

Strangely enough I noticed that if I put the breakpoint in the same method at a different row and put a watch on the same member m_sTMXFileName I don't get a segmentation fault however I get the wrong output displayed.

You can see in my screenshot here: http://postimg.org/image/pf98r01r9/
I'm on a breakpoint in line 366. In my watches frame the value of m_sTMXFileName is something completely wrong. And just below in the GDB pane you can see a print of m_sTMXFileName with the correct value.

The Segmentation fault happens if I put the breakpoint on line 368 in the same example (basically the next statement)

The strange wrong value that the watch is reporting is actually the value of a local string variable on the stack 'externalTilesetFilename' which is defined about two lines above..

Segmentation faults and weird results in the watch frame happen to me all the time when I work with CodeBlocks (GDB always shows correct values) however when I try to build simple example to reproduce this it does not happen :(

Any help will be appreciated
« Last Edit: January 28, 2014, 10:42:17 pm by oded_sharon »

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5913
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Read this: FYI: GDB has a big bug to support MinGW GCC (4.7.x and later), I suggest use my gdb release, which has a workaround this gdb bug.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline oded_sharon

  • Single posting newcomer
  • *
  • Posts: 3
I'm not sure how I'm supposed to install this. After copying the files into my MinGW Directory I get the following error from gdb:
"The application was unable to start correctly (0xc000007b). Click OK to close the application."

I used the gdb2014-01-14.zip. I'll try to find the time and compile it but it will have to wait till I have a day off to play with it.

It might be my python27.dll that is to blame. I'm on 2.7.5 though (it might be a 64bit/32bit issue)
« Last Edit: January 29, 2014, 11:16:24 am by oded_sharon »

Offline oded_sharon

  • Single posting newcomer
  • *
  • Posts: 3
Ok. So I replaced my python with the 32bit version.  Installed the alternative gdb provided by your link.

And it works now. It shows the correct values and does not cause segmentation faults.

Thanks!

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5913
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Nice to see. I also update the page to note the user need a 32 bit python installation.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.