Bug#610535: g++: Using a global std::string in a constructor in another files generates a crashing binary
Package: g++
Version: 4:4.4.5-1
Severity: normal
Tags: upstream
Hello,
I found a strange bug I can't explain myself. I am not a C++ expert so
maybe this construction is illegal.
I provide a sample to reproduce the bug.
===============================
File main.cpp
#include "Application.hpp"
Application g_Application;
int main(void)
{
return 0;
}
===============================
File Application.hpp
class Application {
public:
Application( );
};
===============================
File Application.cpp
#include <string>
#include <iostream>
#include "Application.hpp"
const std::string foo( "foobar" );
Application::Application( )
{
std::cout <<foo << std::endl;
}
===============================
File Makefile
all: crash crash-not
crash: Application.o main.o
$(CXX) -o $@ $^
crash-not: main.o Application.o
$(CXX) -o $@ $^
clean:
rm -f main.o Application.o
rm -f crash crash-not
===============================
Depending on the order of the .o files in the link command I have a
crash or not.
"crash-not" does work as expected.
"crash" does crash.
$ gdb ./crash
GNU gdb (GDB) 7.0.1-debian
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/rousseau/bug/crash...done.
(gdb) r
Starting program: /home/rousseau/bug/crash
Program received signal SIGSEGV, Segmentation fault.
operator<< <char, std::char_traits<char>, std::allocator<char> > (
this=<value optimized out>)
at /usr/include/c++/4.4/bits/basic_string.h:2503
2503 return __ostream_insert(__os, __str.data(), __str.size());
(gdb) bt
#0 operator<< <char, std::char_traits<char>, std::allocator<char> > (
this=<value optimized out>)
at /usr/include/c++/4.4/bits/basic_string.h:2503
#1 Application (this=<value optimized out>) at Application.cpp:9
#2 0x0000000000400cb6 in __do_global_ctors_aux ()
#3 0x0000000000400893 in _init ()
#4 0x00007ffff7dc2c50 in ?? () from /usr/lib/libstdc++.so.6
#5 0x0000000000400c45 in __libc_csu_init ()
#6 0x00007ffff72f0be0 in __libc_start_main (main=<value optimized out>,
argc=<value optimized out>, ubp_av=<value optimized out>,
init=0x400c00 <__libc_csu_init>, fini=0x7ffff762e300 <initial>,
rtld_fini=0x7ffff7decd20 <_dl_fini>, stack_end=0x7fffffffdec8)
at libc-start.c:187
#7 0x00000000004009a9 in _start ()
(gdb)
I also have the problem on Mac OS X using g++ 4.2.1 but on this OS
"crash" does work and "crash-not" does crash.
I also tried with g++ 4.5 (from experimental) but with the same results.
I guess the bug is upstream. But I don't really know to which part of
the compilation chain to report the bug.
I would be happy to report the bug upstream. Do you know which part of
the toolchain is concerned?
If my C++ construction is not supported by the language feel free to
refer me to the C++ specification with details.
Thanks
-- System Information:
Debian Release: squeeze/sid
APT prefers testing
APT policy: (990, 'testing'), (500, 'unstable'), (500, 'stable')
Architecture: amd64 (x86_64)
Kernel: Linux 2.6.32-5-amd64 (SMP w/1 CPU core)
Locale: LANG=fr_FR.UTF-8, LC_CTYPE=fr_FR.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/bash
Versions of packages g++ depends on:
ii cpp 4:4.4.5-1 The GNU C preprocessor (cpp)
ii g++-4.4 4.4.5-8 The GNU C++ compiler
ii gcc 4:4.4.5-1 The GNU C compiler
ii gcc-4.4 4.4.5-8 The GNU C compiler
g++ recommends no packages.
Versions of packages g++ suggests:
pn g++-multilib <none> (no description available)
-- no debconf information
Reply to: