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

青春壹個敷衍的年華 提交于 2019-11-27 18:06:03

问题


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


回答1:


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:

  • http://gcc.gnu.org/ml/gcc-bugs/2009-10/msg00807.html


来源:https://stackoverflow.com/questions/2234557/c-using-getline-prints-pointer-being-freed-was-not-allocated-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!