Re: C++ std::string tries to free() a not allocated pointer ?
Re: C++ std::string tries to free() a not allocated pointer ?
- Subject: Re: C++ std::string tries to free() a not allocated pointer ?
- From: Jean-Denis Muys <email@hidden>
- Date: Thu, 19 Nov 2009 17:37:49 +0100
I finally managed to step through all this. It's not pretty.
I stepped through this assembly for the routine that allocates the std::string, the first in the stack backtrace:
#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
Its signature is:
template
C*
S::_S_construct(const C*, const C*, const allocator<C>&,
forward_iterator_tag);
the first two parameters are the pointers to the first and the last characters of the source string. This is crucial, because that routine tests for these two pointers to be equal, which happens when the source string is "".
in that case, it will return a special value for the std::string internal buffer, always the same one, which is not allocated, but which is at a fixed offset from the code itself. That sounds like a static const to me.
My commented relevant assembly is at the end of this post.
So of course this pointer is not allocated, and of course malloc complains when the destructor tries to free it.
So is this a bug in the standard library? As I wrote, a toy program trying to reproduce this fails to exhibit the issue.
So my theory is the following: the library and my code were linked with different, incompatible versions of the standard C++ library
1- The library, which calls the constructor, uses a version of the library that optimizes the special case of empty strings with a static const char[].
2- My code, which calls the destructor, uses a version of the library that doesn't expect that optimization.
Next questions:
1- How can I confirm that theory, given that I compiled both, the library from the terminal using the generated make file, and my code from XCode.
2- How can I find/control/check/change which version of the standard C++ library is linked with either my code or the library?
Though I found the cause of the problem, I feel no closer to a solution.
Jean-Denis
Commented assembly:
<+0000> push ëp save stack pointer
<+0001> mov %esp,ëp new base pointer
<+0003> sub $0x38,%esp reserve locals
<+0006> mov ëx,-0xc(ëp) save b
<+0009> mov %esi,-0x8(ëp) save si
<+0012> mov íi,-0x4(ëp) save di
<+0015> call 0x99056cc8 <Start+20> call next instruction
<+0020> pop ëx b = address of this instruction
<+0021> mov 0xc(ëp),êx a = param2 = buffer end
<+0024> cmp êx,0x8(ëp) compare buffer with param1=buffer start
<+0027> jne 0x99056d49 <Start+149> if not empty string, we jump. so here we continue
<+0029> lea 0x7b360e8(ëx),êx a=this code plus that offset. Address of something. Here = 0xa0b8cdb0
<+0035> add $0xc,êx a= 0xa0b8cdbc
<+0038> mov êx,-0x1c(ëp) save that address in local -0x1c (function result)
<+0041> jmp 0x99056d52 <Start+158> exit from routine
<snip>
<+0158> mov -0x1c(ëp),êx store function result in register a
<+0161> mov -0xc(ëp),ëx restore saved registers
<+0164> mov -0x8(ëp),%esi
<+0167> mov -0x4(ëp),íi
<+0170> leave
<+0171> ret
_______________________________________________
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