tags:

views:

2109

answers:

3

Hi, 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

+7  A: 

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",
sixlettervariables
Meta-comment: you aren't really doing much to help your debugging though.
sixlettervariables
does this disable debugging for the project completely?
yuval
As far as libstc++ it is according to the GCC user's manual.
sixlettervariables
is there any way make that affect all projects in xcode rather than doing it for every single one?
yuval
You can also change from GCC 4.2 to GCC 4.0.
sixlettervariables
thank you so much guys!
yuval
I realize this is well after the fact, but Xcode 3.2.2 "fixes" the problem by disabling C++ Standard Template Library Debug Mode. Per an updated version of the Xcode link provided above: "C++ Standard Template Library Debug Mode (GLIBCXX_DEBUG) is not supported with gcc 4.2. In Xcode 3.2 and 3.2.1 this would cause C++ programs compiled with gcc 4.2 to behave oddly, usually with string I/O to streams. In Xcode 3.2.2 the macros that invoke STL Debug Mode are suppressed when using gcc 4.2 so that C++ programs behave correctly when built with the Debug configuration."
Ben Hocking
A: 

Thanks I was looking for the same answer here. By changing the compiler version to 4.0 and editing those files you listed, does that change it for all of XCode? Or just the current project we are working on? Thanks!

Jon
I have no idea, I asked the previous responded on Oct 21, but never got a response... consider opening a new thread and linking to this one.
yuval
Changing the settings in a project only affect that project, not other projects or global defaults like the compiler version.
cdespinosa
+2  A: 

An easier way of accomplishing the same thing: paste 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.

adam n
thank you for this!
yuval