C++ using getline() prints: pointer being freed was not allocated in XCode
Asked Answered
J

1

10

I'm trying to use std:getline() but getting a strange runtime error:

malloc: * error for object 0x10000a720: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug

This is the code that produces this error:

//main.cpp
#include <iostream>
#include <sstream>

int main (int argc, char * const argv[])
{
   std::istringstream my_str("demo string with spaces");
   std::string word;

   while (std::getline(my_str, word, ' ')) {
        std::cout << word << std::endl;
   }
   return 0;
}

Before each word I get this error. From the comments it seems to be a OSX/XCode specific error. Any hints on that?

Update: The error is only printed in Debug mode. If I build this code in Release mode everything is fine.

Update 2: More info on that issue can be found here.

Solution:

Set

_GLIBCXX_FULLY_DYNAMIC_STRING=1

in your Preprocessor Macros in targets info build tab.

System info:

OSX 10.6.2 | XCode 3.2 | g++ 4.2 | debug config for i386

Jacey answered 10/2, 2010 at 5:11 Comment(9)
It runs fine on my machine. I'm not using a MacBook though so that might have something to do with it maybe but on XP with VS2008 it compiles and runs fine.Trehalose
Likewise, it compiles and runs without errors on my PPC Powerbook. Mac OS 10.4 with g++ 4.0.1. And it looks right...Cottrill
Compiles and runs fine under Visual Studio 2008 for me too.Whiplash
Works fine for me on my MacBook Pro with 10.5.8 g++ 4.0.1 and valgrind doesn't report anything suspicious either.Slant
Works fine for me, MacBook Pro, 10.6.2 and g++ 4.2.1. Where does malloc_error_break get called?Flite
@Potatoswatter: right before it prints the word.Jacey
@dan: Looks like your setup may be wonky somehow. Try it with terminal g++ in a fresh terminal, then consider a reboot, then consider a reinstallation of the developer tools.Cottrill
getline => string::append => string::reserve => free: I can reproduce it only within XCode, not from the command line.Flite
To reproduce with Xcode, make sure to include -D_GLIBCXX_DEBUG on the g++ command line.Bordie
S
10

At least one person has reported problems with g++ 4.2.1 on Apple that seem possibly related to yours having to do with an improper configuration of the standard library with the _GLIBCXX_FULLY_DYNAMIC_STRING definition (not that I understand any of what I'm typing here).

You might get a bit of a clue from the newsgroup thread that includes this message:

Superfluid answered 10/2, 2010 at 6:47 Comment(2)
This is it. I reproduced the problem and fixed it by adding _GLIBCXX_FULLY_DYNAMIC_STRING to the target settings window. (Do not add it to the "Preprocessing" section of the Project settings window; that does nothing. XCode, grrrrr…)Flite
Since this is a team effort, would someone like to report a bug?Flite

© 2022 - 2024 — McMap. All rights reserved.