• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
getline 'free' problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

getline 'free' problem


  • Subject: getline 'free' problem
  • From: Aron Nopanen <email@hidden>
  • Date: Wed, 16 Sep 2009 22:41:02 +1200

Hi All,

Not a Cocoa problem as such, but not sure if there's a better list for it:

On Snow Leopard (using gcc 4.2), I'm getting a 'double free' error in the guts of the C++ std::getline. The 'free' in question is being performed by std::string::reserve. This happens any time I run 'getline' (reading from cin) on a fresh, virgin string. (The error is only seen in the Debug configuration.)

If, prior to the 'getline' call, I run any of:
- reserve
- push_back
- = "blah"
...on the string, I don't get the error.

Any idea what's going awry?

A very simple test case follows:

#include <iostream>
#include <string>

using namespace std;

int main (int argc, char * const argv[]) {
	string line;

// Doing any of the following prior to 'getline' makes everything OK:
//	line.push_back('x');
//	line.reserve(10);
//	line = "x";

	getline(cin, line);

	cout << "Line is: \"" << line << "\"" << endl;

   return 0;
}

The error:
GetlineTest(1362) malloc: *** error for object 0x1000041e0: pointer being freed was not allocated


Stack trace:
#0 0x7fff87af0b91 in malloc_error_break
#1 0x7fff87a1a083 in free
#2 0x7fff827181e8 in std::string::reserve
#3 0x7fff82718243 in std::string::push_back
#4 0x7fff827012b5 in std::getline<char, std::char_traits<char>, std::allocator<char> >
#5 0x100001cfb in std::getline<char, std::char_traits<char>, std::allocator<char> > at basic_string.h:2451
#6 0x10000141c in main at main.cpp:14


Thanks,
Aron
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: getline 'free' problem
      • From: Jason Foreman <email@hidden>
  • Prev by Date: [JOB] Established iPhone app developer seeks Cocoa Network Engineer
  • Next by Date: Last Apple Help question (hopefully)
  • Previous by thread: [JOB] Established iPhone app developer seeks Cocoa Network Engineer
  • Next by thread: Re: getline 'free' problem
  • Index(es):
    • Date
    • Thread