This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
global string object in a shared library
- From: "Tomas Varsavsky" <tomasv at alphalink dot com dot au>
- To: gcc-help at gcc dot gnu dot org
- Date: Thu, 28 Aug 2003 15:07:01 +1100
- Subject: global string object in a shared library
- Reply-to: tomasv at alphalink dot com dot au
Hi,
I'm having problems using a global string in my shared library. The source
file looks something like this:
---------
#include <string>
...
...
string myString;
...
...
int myFunction()
{
...
...
myString = "Hello World";
...
...
}
---------
My main() just calls the function myFunction in the shared lib. I get a
segmentation fault on the line
myString = "Hello World"
Backtrace points at line 154 of basic_string.h, which reads:
bool
_M_is_shared() const
{ return _M_references > 0; } <--- line 154
I compile with -fpic, -shared and -g with version 3.2.2 of g++ in HP-UX 10.20
to create a shared library, which end up looking like this:
[ASAC SDP 2.3] snapadm@sdp2 188: chatr libauc.sl
libauc.sl:
shared library
shared library dynamic path search:
SHLIB_PATH disabled second
embedded path enabled first /opt/sybase/opt/TWWfsw/gcc322/lib
shared library list:
dynamic /opt/sybase/DS/0/lib/libsybdb.sl
dynamic /usr/lib/libnsl_s.1
dynamic /opt/sybase/opt/TWWfsw/gcc322/lib/libstdc++.sl.5
dynamic /usr/lib/libM.1
dynamic /opt/sybase/opt/TWWfsw/gcc322/lib/libgcc_s.sl
shared vtable support disabled
static branch prediction disabled
kernel assisted branch prediction enabled
lazy swap allocation disabled
text segment locking disabled
data segment locking disabled
data page size: 4K
instruction page size: 4K
If I write a similar program using int instead of string then it all works ok
so I think the problem _is_ with the string class, or the way that I'm using
it.
I suspect it's something to do with the global object not being
constructed/allocated when my library loads because if I change the line
string myString;
to
string myString = "test";
And then use gdb to set a breakpoint somewhere in myFunction() and do:
(gdb) p myString
$1 = {static npos = <incomplete type>,
_M_dataplus = {<allocator<char>> = {<No data fields>}, _M_p = 0x0},
static _S_empty_rep_storage = {0, 0, 0, 0}}
It seems to not be allocated/assigned.
What am I doing wrong? Any ideas?
Thanks for your help.
Regards,
Tom