1
class MyClass{
public:
  MyClass() {}
  virtual ~MyClass() {}
};

extern "C" int foo(int tryNumber)
{
    std::tr1::shared_ptr<MyClass> myClass(new MyClass());
    std::cout << "Object has been created " << tryNumber << << std::endl;
    return 0;
}

Then somewhere in my program I write:

for (int i = 0; i < 10000; ++i){
    foo(i);
}

There are the facts:

1) gcc 4.0.1, and I can't update them yet. So when I implement std::tr1::shared_ptr, I see the complier uses boost/shared_ptr.hpp (boost 1.33.1)

2) Well, the program uses many threads, I even don't know how they do work and what they do completely (the large project at my job), but I know, that I don't use any shared variables or something else that can cause this behavior

3) Sometimes it just prints:

Object has been created 0

Object has been created 1

...

Object has been created 9999

And everything is ok

Sometimes it prints 0-1-2-3-4 (or more) lines and then stops. Furthermore - I know, that the object has been created, but function hasn't returned the value and program just freezes, and when I try to attach to the program with gdb and type "where" - I see this:

0) 0xb7fd8430 in __kernel_vsyscall ()

1) 0xb7d9bece in _lll_mutex_lock-wait() from /lib/i686/libpthread.so.0

2) 0xb7d98500 in _L_mutex_lock_71 () from /lib/i686/libpthread.so.0

3) 0xbfbefab8 in ?? ()

4) 0x00000000 in ?? ()

Or this:

0) 0xb7fd8430 in __kernel_vsyscall ()

1) 0xb7d9bece in _lll_mutex_lock-wait() from /lib/i686/libpthread.so.0

2) 0xb7d98500 in _L_mutex_lock_71 () from /lib/i686/libpthread.so.0

..dunno what is here, I see only " .. in ?? ()"

10) .. in __gthread_mutex_lock

11) .. in __gthread_mutex_lock

12) .. in std::tr1::_Sp_counted_base::release

13) .. in ~shared_count

14) .. in ~shared_ptr

Seems it like shared_ptr is broken?

sehe
  • 374,641
  • 47
  • 450
  • 633
Alexey Teplyakov
  • 316
  • 3
  • 10
  • I would guess that you are doing something else, that you down show us, that messes up the stack, probably some kind of undefined behavior happening. The code you show us is not enough to help you with your problem. – Some programmer dude Oct 29 '13 at 08:56
  • @Joachim Pileborg believe me, I'm not doing anything else. It is the large project and I'm editing a local piece of code. I faced with this issue few days ago [link](http://stackoverflow.com/questions/19585236/the-exception-isnt-captured-by-catch-block), so I tried to isolate the problem and this topic describes what I found out(the problem occurs after shared_ptr adding) – Alexey Teplyakov Oct 29 '13 at 09:08
  • 1
    Use some kind of memory debugger, like [Valgrind](http://valgrind.org/). It might help you in this case. – Some programmer dude Oct 29 '13 at 09:11

1 Answers1

1

I just solved this issue. Changed this:

#include <tr1/memory> to #include <boost/shared_ptr.hpp>

std::tr1::shared_ptr to boost::shared_ptr

The solution is described here link

Alexey Teplyakov
  • 316
  • 3
  • 10
  • 1
    This is quite a useful reference link. I'm optimizing the tags so future users may find this as quickly as possible. You might also add some more background from the linked thread(s) to your answer to guard against link-rot – sehe Oct 29 '13 at 22:43