tags:

views:

123

answers:

1

The following does not work and gives me a SIGABRT when I run in the debugger:


std::ifstream inFile;

inFile.open("/Users/fernandonewyork/inputText.txt");    

cout << inFile << endl;

vector<string> inText;

if (inFile) {
    string s4;

    while (inFile>>s4) {
        inText.push_back(s4);
    }

}
inFile.close();

The following does:


std::ifstream inFile;

inFile.open("/Users/fernandonewyork/inputText.txt");    

cout << inFile << endl;

vector<string> inText;

if (inFile) {
    string s4("This is no lnger an empty string");

    while (inFile>>s4) {
        inText.push_back(s4);
    }

}
inFile.close();

I was under the impression I was able to simply use s4 without having to worry about any space considerations, or is something else happening here? This is the full error I get from the top code:

malloc: * error for object 0x100010a20: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug Program received signal: “SIGABRT”.

+1  A: 

This egregious bug was covered by an earlier question:

Solution:

Set

_GLIBCXX_FULLY_DYNAMIC_STRING=1

in your Preprocessor Macros in targets info build tab.

There is another settings window with a Preprocessor Macros field (project settings?) but setting this there will have no effect.

— Hmm, that issue is rather different from this one, but ironically your symptom is more common.

Potatoswatter
Applying the above setting (_GLIBCXX_FULLY_DYNAMIC_STRING=1) resolved the issue. Both versions of the code now work.
Fernando