public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/24196]  New: Using string instances to pass arguments to dlls fails
@ 2005-10-04 19:07 gerrit at gcc dot gnu dot org
  2005-10-04 19:08 ` [Bug libstdc++/24196] " gerrit at gcc dot gnu dot org
                   ` (25 more replies)
  0 siblings, 26 replies; 29+ messages in thread
From: gerrit at gcc dot gnu dot org @ 2005-10-04 19:07 UTC (permalink / raw)
  To: gcc-bugs

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 ?


-- 
           Summary: Using string instances to pass arguments to dlls fails
           Product: gcc
           Version: 3.4.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: gerrit at gcc dot gnu dot org
 GCC build triplet: i686-pc-cygwin
  GCC host triplet: i686-pc-cygwin
GCC target triplet: i686-pc-cygwin


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24196


^ permalink raw reply	[flat|nested] 29+ messages in thread
[parent not found: <bug-24196-4@http.gcc.gnu.org/bugzilla/>]

end of thread, other threads:[~2015-03-23 13:02 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-04 19:07 [Bug libstdc++/24196] New: Using string instances to pass arguments to dlls fails gerrit at gcc dot gnu dot org
2005-10-04 19:08 ` [Bug libstdc++/24196] " gerrit at gcc dot gnu dot org
2005-10-04 19:10 ` pinskia at gcc dot gnu dot org
2005-10-04 19:28 ` ptsekov at gmx dot net
2005-10-04 20:53 ` pcarlini at suse dot de
2005-10-05  8:36 ` ptsekov at gmx dot net
2005-10-05  8:51 ` pcarlini at suse dot de
2005-10-05 10:20 ` pcarlini at suse dot de
2005-10-06 11:50 ` pcarlini at suse dot de
2005-10-06 12:50 ` ptsekov at gmx dot net
2005-10-06 12:53 ` pcarlini at suse dot de
2005-10-06 17:00 ` pcarlini at suse dot de
2005-10-06 17:01 ` pcarlini at suse dot de
2005-10-07 12:13 ` ptsekov at gmx dot net
2005-10-07 17:06 ` pcarlini at suse dot de
2006-05-17 10:03 ` dave dot korn at artimi dot com
2006-05-17 10:09 ` pcarlini at suse dot de
2006-07-06  1:06 ` dannysmith at users dot sourceforge dot net
2009-04-25 19:10 ` ktietz at gcc dot gnu dot org
2009-04-25 19:17 ` paolo dot carlini at oracle dot com
2009-06-29 15:12 ` dave dot korn dot cygwin at gmail dot com
2009-10-25  5:40 ` johnw at gnu dot org
2009-10-25  5:44 ` johnw at gnu dot org
2009-10-25  5:50 ` johnw at gnu dot org
2009-10-25  5:51 ` paolo dot carlini at oracle dot com
2010-03-20 19:46 ` davek at gcc dot gnu dot org
2010-06-01 20:51 ` steven at gcc dot gnu dot org
     [not found] <bug-24196-4@http.gcc.gnu.org/bugzilla/>
2014-02-16 13:12 ` jackie.rosen at hushmail dot com
2015-03-23 13:36 ` redi at gcc dot gnu.org

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).