• 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
Re: C++ std::string tries to free() a not allocated pointer ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++ std::string tries to free() a not allocated pointer ?


  • Subject: Re: C++ std::string tries to free() a not allocated pointer ?
  • From: David Hayes <email@hidden>
  • Date: Wed, 18 Nov 2009 13:45:16 -0500

When you say the string length is zero does that mean that you are creating a std::string but not initializing it to any value?

I would be surprised if it is using the stack.  If you are creating a string and not initializing it with any value I would guess that std::string is initializing the pointer to NULL and the destructor is not checking for a valid pointer before calling free or delete.  I would suggest creating the string with an initial value of "".

Dave


On 2009-11-18, at 1:06 PM, Jean-Denis Muys wrote:

> Hi,
>
> I solved my GDB not finding source code files. Here is the actual problem at hand, for which I am (perhaps not totally) stuck.
>
> I have a weird problem in C++ code behavior, where a std::string uses the stack (I think) for its internal storage and yet calls free in its destructor. Of course, malloc complains:
>
> malloc: *** error for object 0xa0b8cdb0: pointer being freed was not allocated
> *** set a breakpoint in malloc_error_break to debug
>
> Has anybody seen something similar?
>
> Here are the details:
>
> 1- This happens when the string length is zero. Then of course, it doesn't really need any internal storage.
>
> 2- The std::string is the sole member of the class SQLstring, for which it is a wrapper:
>
> class SQLString
> {
> 	std::string realStr;
> public:
> 	~SQLString() {}
> 	SQLString(const char * s, size_t n) : realStr(s, n) {}
> };
>
> [other public member functions irrelevant because not called.]
>
> 3- a buffer is allocated with 23 bytes in the library, form which a SQLString is constructed.
> The calling sequence I stepped through in the library is:
>
> buffer = new char[23];
> buffer[0] = 0;
> length = 0;
> return SQLString(buffer, length);
>
> 4- that return statement constructs a temporary SQLString through that call chain:
>
> #0  0x99056cb4 in std::string::_S_construct<char const*> ()
> #1  0x99056d85 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string ()
> #2  0x041fc8c7 in sql::SQLString::SQLString (this=0xb02e6a4c, s=0x4beccb0 "", n=0) at sqlstring.h:43
>
> I have a hard time stepping through here, because I couldn't find the source code for std::string::_S_construct<char const*> (). The best I found is in string-inst.cc:
>
> template
>  C*
>  S::_S_construct(const C*, const C*, const allocator<C>&,
> 		    forward_iterator_tag);
>
> It's declared, but no source code.
>
> 5- trying to reduce the problem through a toy program running the same call sequence failed: the toy program doesn't misbehave.
>
> At this point, I call for help, in case somebody has ever encountered something similar. Please note that this is 100% reproducible on my machine. My code is a 4D plugin that calls to mySQL through their connector-C++ driver. So it's not very easy to replicate my working environment (though not impossible).
>
> Thanks a million.
>
> Jean-Denis
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Xcode-users mailing list      (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: C++ std::string tries to free() a not allocated pointer ?
      • From: Clark Cox <email@hidden>
References: 
 >C++ std::string tries to free() a not allocated pointer ? (From: Jean-Denis Muys <email@hidden>)

  • Prev by Date: Re: C++ std::string tries to free() a not allocated pointer ?
  • Next by Date: Re: C++ std::string tries to free() a not allocated pointer ?
  • Previous by thread: Re: C++ std::string tries to free() a not allocated pointer ?
  • Next by thread: Re: C++ std::string tries to free() a not allocated pointer ?
  • Index(es):
    • Date
    • Thread