Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 3.2.1 and C++ string fails!

Tags:

c++

xcode

In Xcode 3.2.1 on Mac OS X Snow Leopard, I open a project under: Command Line Tool of type 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 fine, and prompts me to "Enter Something". When I type in 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 a different computer (opened the project with the 'command line utility' option, which does not exist in 3.2.1), the code runs with NO PROBLEM.

Does anybody know what's going on? Thanks, Yuval

like image 828
Yuval Karmi Avatar asked Dec 29 '22 13:12

Yuval Karmi


1 Answers

As far as I can tell, I'm not experiencing this issue in Release mode for x86_64. But I am seeing the issue in Debug x86_64. If I follow the directions given by Howard in this post, I'm able to get it running in debug mode:

  1. Project -> Edit Active Target ...
  2. Click Build tab
  3. Search for "preprocessor"
  4. Delete _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1

Build and run, you'll notice it works. Another interesting observation is that using __gnu_debug::string (from the <debug/string> header) alone does not trigger the error.

EDIT: from the horses mouth (known issues in XCode 3.2.1)

The default gcc 4.2 compiler is not compatible with the Standard C++ Library Debug Mode. C++ programs compiled with Xcode 3.2 may not work in the Debug configuration. To fix this, set the Compiler Version to 4.0, or edit the Debug configuration’s Preprocessor Macros and remove the entries: _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1

You can do this for all projects by navigating 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",
like image 194
user7116 Avatar answered Jan 11 '23 09:01

user7116