开发者

Xcode 3.2.1 and C++ string fails!

开发者 https://www.devze.com 2022-12-08 22:17 出处:网络
In Xcode 3.2.1 on Mac OS X Snow Leopa开发者_运维问答rd, I open a project under: Command Line Tool of type C++ stdc++.

In Xcode 3.2.1 on Mac OS X Snow Leopa开发者_运维问答rd, 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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消