0

I took the example code from http://en.cppreference.com/w/cpp/memory/shared_ptr and compiled it locally, compiled it like this:

g++ -std=c++0x -D_GLIBCXX_USE_NANOSLEEP my_file.cpp 

When I run it, I got the following output:

  Base::Base()
  Derived::Derived()
Created a shared Derived (as a pointer to Base)
  p.get() = 0x608029, p.use_count() = 1
Segmentation fault

Any idea why it has a seg fault?

I'm fairly new to C++, how would you usually debug this?

Thanks!

foobar
  • 10,854
  • 18
  • 58
  • 66
  • How did you compile with `-std=c++0x`? `std::shared_ptr` wasn't available until C++11, so you'd need `-std=c++11` – Cory Kramer May 21 '15 at 19:14
  • 3
    @CoryKramer C++0x was the working name of the standard, when it was believed that the process would be finished in '09 at the latest. The options are equivalent. – molbdnilo May 21 '15 at 19:27

2 Answers2

2

If you are using the source code from the linked page verbatim, suggested changes to the command line:

  1. Use -std=c++11 or -std=c++14.
  2. Enable use of pthread library and make the compiler thread-aware.
g++ -Wall -std=c++11  -pthread my_file.cpp
g++ -Wall -std=c++14  -pthread my_file.cpp
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • It's not just for the library, it also makes gcc thread-aware (so that shared ptr's refcount becomes thread-safe, among other things ) – Cubbi May 21 '15 at 19:35
  • @Cubbi not sure I understand your comment. Are you saying that if using standard C++ threads you should use `-pthread`? I thought `-std=c++11` should be enough. – vsoftco May 21 '15 at 19:36
  • @vsoftco unless gcc changed the defaults in 5.1, it needs -pthread (not to be confused with -lpthread) – Cubbi May 21 '15 at 19:40
  • 1
    @Cubbi can you briefly tell me why? Is it related to the `shared_ptr` only? I'm using threads (via OpenMP) in one of my applications and I want to make sure I'm not doing something stupid. – vsoftco May 21 '15 at 19:45
  • @vsoftco -fopenmp implies -pthread, according to man gcc. It passes -D_REENTRANT, at least (actually i may be wrong on shared ptr safety, that depends on _GTHREAD – Cubbi May 21 '15 at 19:49
  • 1
    @Cubbi Thanks, but it is still unclear for me. Looking at `man g++`: *pthread Adds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker.* Say I just use `std::thread` in my program, and compile with `-std=c++11`. Should I use the `-pthread` flag? I thought that the `-std=c++11` will take care of that, as the new standard has implicit support for multi-threading, and the latter are not forced to be implemented via `pthread` (although most of the time they are). – vsoftco May 21 '15 at 20:00
  • 1
    @vsoftco GCC is designed to not generate thread-aware code unless requested. It requires `-pthread` for pthread-based implementations; other platforms will require some other option (like `-mthread` for Windows threads, `-threads` for HP-UX dce threads.) Hopefully, some time in the future, `-std=c++11`/`std=c++14`/etc will automatically imply `-pthread`/`-mthread`/etc. – DanielKO May 21 '15 at 20:42
0

The example suffers from the 'base class not having virtual destructor on base class' problem. When destroying the Derived object with a pointer to Base the code simply calls the destructor of Base. Which is a Bad Thing (TM)

marom
  • 5,064
  • 10
  • 14
  • As mentioned on the cppreference page, there is no need for a virtual destructor, the smart pointer keeps track of which destructor to call. Related: http://stackoverflow.com/q/3899790/3093378 – vsoftco May 21 '15 at 19:29