public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Re: ATTN: g++ maintainer: Using string instances to pass arguments to dlls
@ 2005-10-04 20:14 James R. Phillips
  2005-10-05  1:07 ` Charles Wilson
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: James R. Phillips @ 2005-10-04 20:14 UTC (permalink / raw)
  To: cygwin

Does fixing this bug require upstream intervention, or can you just develop a
patch, and submit it to upstream?

Anecdotal evidence [1] exists that this issue may be what prevents compiling a
working version of octave 2.1.71 with gcc 3.4.4.  This comes from John Ewing
(octave upstream), who states that he is able to compile a working version with
gcc 3.4.4 only if it is statically linked.

John also wonders [2] why libstdc++ is static as opposed to shared on cygwin. 
I have searched the archives, and verified its static nature, but was unable to
find an explanation.

[1] http://www.octave.org/mailing-lists/help-octave/2005/3734
[2] http://www.octave.org/mailing-lists/help-octave/2005/3738

jrp


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 12+ messages in thread
* ATTN: g++ maintainer: Using string instances to pass arguments to  dlls
@ 2005-10-04 15:02 Pavel Tsekov
  2005-10-04 19:09 ` Gerrit P. Haase
  0 siblings, 1 reply; 12+ messages in thread
From: Pavel Tsekov @ 2005-10-04 15:02 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: TEXT/PLAIN, Size: 3428 bytes --]

Hello,

I noticed the following problem while porting an internal C++ application
from linux to Cygwin. If a std::string instance created in one module
(exe or dll) is passed to another say as an argument of a function call,
the program crashes or hangs. I did debug for a while and it turned
out that it is not the program itself that causes the crash - the cause
lies in the std::string implementation, the fact that libstdc++ is
provided as a static archive and that it is compiled without
_GLIBCXX_FULLY_DYNAMIC_STRING defined.

What happens is that each module which links agains libstdc++ get its very
personal copy of the class member _S_empty_rep_storage. Now since
_GLIBCXX_FULLY_DYNAMIC_STRING is not defined an empty string instance i.e.
one that is created by the default constructor of std::string gets a
pointer to _S_empty_rep_storage i.e. the actual allocation of memory is
delayed until memory is really needed. If _GLIBCXX_FULLY_DYNAMIC_STRING is
defined each string instance would get a pointer to a newly heap
allocated block of memory instead . Now look at the ouput of gdb and nm
used on the attached testcase:

Administrator@mordor ~/src/testcase/tc
$ nm dll.dll | grep _S_empty_rep_storage
1000c030 d .data$_ZNSs4_Rep20_S_empty_rep_storageE
1000c030 D __ZNSs4_Rep20_S_empty_rep_storageE

Administrator@mordor ~/src/testcase/tc
$ nm main.exe | grep _S_empty_rep_storage
00442120 d .data$_ZNSs4_Rep20_S_empty_rep_storageE
00442120 D __ZNSs4_Rep20_S_empty_rep_storageE

===
Breakpoint 1, main (argc=1, argv=0x10042980) at main.cc:9
9       {
(gdb) n
10        string s, s1;
(gdb)
12        export1 (s);
(gdb) print s
$1 = {static npos = 4294967295, _M_dataplus = {<allocator<char>> =
{<new_allocator<char>> = {<No data fields>}, <No data fields>},
    _M_p = 0x44212c ""}}
(gdb) print s1
$2 = {static npos = 4294967295, _M_dataplus = {<allocator<char>> =
{<new_allocator<char>> = {<No data fields>}, <No data fields>},
    _M_p = 0x44212c ""}}
===

Here the two strings share _S_empty_rep_storage.

===
(gdb) step
export1 (s=@0x22eea0) at dll.cc:7
7         string s1;
(gdb) n
9         s1.push_back ('A');
(gdb) n
10        s.push_back ('A');
(gdb) print s
$3 = (string &) @0x22eea0: {static npos = 4294967295,
  _M_dataplus = {<allocator<char>> = {<new_allocator<char>> = {<No data
fields>}, <No data fields>}, _M_p = 0x44212c ""}}
(gdb) print s1
$4 = {static npos = 4294967295, _M_dataplus = {<allocator<char>> =
{<new_allocator<char>> = {<No data fields>}, <No data fields>},
    _M_p = 0x10042aec "A"}}
(gdb)
===

Here s1 points to the dll local _S_empty_rep_storage.

The _M_p member of _M_dataplus is pointing to different copies of
_S_empty_rep_storage - one stored in the executable which calls the dll
and another one in the dll itself.

Now the second push_back() call in export1 () will end up calling
_M_mutate() to actually allocate storage. _M_mutate() will call
_M_rep()->_M_dispose() which will end up free()-ing the memory reserved
for _S_empty_rep_storage in the main exe. There is a check to prevent free()-ing
_S_empty_rep_storage:

#ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
          if (__builtin_expect(this != &_S_empty_rep(), false))
#endif

... but it doesn't work well in the case when a string instance created
in one module is passed to another and libstdc++ is statically linked
because of the fact that each module has its own copy
_S_empty_rep_storage.

Can we get this fixed ?

[-- Attachment #2: Type: APPLICATION/octet-stream, Size: 423 bytes --]

[-- Attachment #3: Type: text/plain, Size: 218 bytes --]

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2005-10-11 20:56 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-04 20:14 ATTN: g++ maintainer: Using string instances to pass arguments to dlls James R. Phillips
2005-10-05  1:07 ` Charles Wilson
2005-10-05  9:02 ` Pavel Tsekov
2005-10-05 10:37   ` Gerrit P. Haase
2005-10-05 17:15     ` Pavel Tsekov
2005-10-05 22:05       ` Gerrit P. Haase
2005-10-06  7:31         ` Pavel Tsekov
2005-10-05 10:35 ` Gerrit P. Haase
2005-10-06 21:16   ` Yaakov S (Cygwin Ports)
2005-10-11 20:56     ` Gerrit P. Haase
  -- strict thread matches above, loose matches on Subject: below --
2005-10-04 15:02 Pavel Tsekov
2005-10-04 19:09 ` Gerrit P. Haase

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).