Reputation: 316
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?
Upvotes: 1
Views: 112
Reputation: 316
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
Upvotes: 1