Xcode string error 3.2.1 and C ++!

In Xcode 3.2.1 on Mac OS X Snow Leopard, I open a project under: A command-line tool like C ++ stdC ++. I have the following simple code:

#include <iostream> #include <string> using namespace std; int main(){ string myvar; cout << "Enter something: " << endl; cin >> myvar; cout << endl << myvar << endl; return 0; } 

The program compiles and prompts me to "Enter something." When I enter something, and then press enter, I get the following error:

 myproject(766) malloc: *** error for object 0x1000041c0: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug Program received signal: "SIGABRT". sharedlibrary apply-load-rules all (gdb) 

When compiling on an earlier version of Xcode (3.1.2) on another computer (the project opens with the option "command line utility", which does not exist in 3.2.1), the code works without NO PROBLEM.

Does anyone know what is going on? Thanks, Yuval

+4
source share
2 answers

As far as I can tell, I am not experiencing this issue in Release mode for x86_64 . But I see a problem in Debug x86_64 . If I follow the instructions given by Howard in this post , I can run it in debug mode:

  • Project β†’ Edit Active Object ...
  • Click the Create Tab tab
  • Search "preprocessor"
  • Delete _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1

Build and run, you will notice that this works. Another interesting point is that using __gnu_debug::string (from the <debug/string> header) does not cause an error.

EDIT: from the lips of horses ( known issues in Xcode 3.2.1 )

The gcc 4.2 compiler is not compatible with the standard C ++ library debugging standard by default. C ++ programs compiled with Xcode 3.2 may not work in the Debug configuration. To fix this, install the compiler version in 4.0 or edit the debug preprocessor macros and delete the entries: _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1

You can do this for all projects by going to /Developer/Library/Xcode/Project Templates/Application/Command Line Tool/C++ Tool/C++Tool.xcodeproj/ and editing project.pbxproj and deleting the lines around line 138:

 "_GLIBCXX_DEBUG=1", "_GLIBCXX_DEBUG_PEDANTIC=1", 
+8
source

A simpler way to accomplish the same thing: insert these lines at the very beginning of your program (before any #include statements):

 #define _GLIBCXX_FULLY_DYNAMIC_STRING 1 #undef _GLIBCXX_DEBUG #undef _GLIBCXX_DEBUG_PEDANTIC 

Obviously, this will only affect the current project.

+2
source

All Articles