std::cout causes EXC_BAD_ACCESS from a dynamic library (apple gcc 3.3)

Vivek Rajan vrajan@evl.uic.edu
Wed Jul 2 15:57:00 GMT 2003


gcc version information:

$ c++ -v
Reading specs from /usr/libexec/gcc/darwin/ppc/3.3/specs
Thread model: posix
gcc version 3.3 20030304 (Apple Computer, Inc. build 1435)

I'm using std::cout in a class constructor that is built as a dynamic 
library. If I create a global instance of the class the program crashes 
with the following stack trace:

------------------- stack trace --------------------
Program received signal EXC_BAD_ACCESS, Could not access memory.
0x000b7e4c in std::ostream::sentry::sentry(std::ostream&) () at 
/usr/include/gcc/darwin/3.3/c++/cmath:391
391         {
(gdb) where
#0  0x000b7e4c in std::ostream::sentry::sentry(std::ostream&) () at 
/usr/include/gcc/darwin/3.3/c++/cmath:391
#1  0x000b80e4 in std::basic_ostream<char, std::char_traits<char> >& 
std::operator<< <std::char_traits<char> >(std::basic_ostream<char, 
std::char_traits<char> >&, char const*) () at 
/usr/include/gcc/darwin/3.3/c++/cmath:391
#2  0x000b6470 in Foo::Foo() (this=0x9020) at foo.cpp:6
#3  0x000b6428 in Foo::Foo() (this=0x9020) at foo.cpp:5
#4  0x000082dc in __static_initialization_and_destruction_0(int, int) 
(__initialize_p=1, __priority=65535) at main.cpp:3
#5  0x00008340 in _GLOBAL__I_foo () at main.cpp:9
#6  0x8fe16594 in __dyld_call_module_initializers_for_objects ()
#7  0x8fe160a0 in __dyld_call_module_initializers ()
#8  0x8fe133ac in __dyld__dyld_make_delayed_module_initializer_calls ()
#9  0x00001dd8 in _call_mod_init_funcs () at 
/SourceCache/Csu/Csu-45/crt.c:299
#10 0x00001cc0 in _start (argc=1, argv=0xbffff518, envp=0xbffff520) at 
/SourceCache/Csu/Csu-45/crt.c:217
#11 0x00001c20 in start ()
------------------- stack trace --------------------

Here is the source that demonstrates the problem.

------------------- foo.h --------------------
class Foo
{
     public:
         Foo();
         ~Foo() {}
};
------------------- foo.h --------------------

------------------- foo.cpp --------------------
#include <iostream>
#include "foo.h"

Foo::Foo()
{
     std::cout << "Foo::Foo()" << std::endl;
}
------------------- foo.cpp --------------------

------------------- main.cpp --------------------
#include "foo.h"

Foo foo;
int main()
{
     return 1;
}
------------------- main.cpp --------------------

The command line options used to build the above code:

$ c++ -g -c foo.cpp -o foo.o
$ c++ -g -dynamiclib foo.o -o libfoo.dylib
$ c++ -g -c main.cpp -o main.o
$ c++ -g main.o -o main -L. -lfoo


It seems like a bug to me. Any ideas?

Thanks,

-- Vivek



More information about the Gcc mailing list