【发布时间】:2013-10-29 08:51:36
【问题描述】:
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;
}
然后我在程序的某个地方写:
for (int i = 0; i < 10000; ++i){
foo(i);
}
有事实:
1) gcc 4.0.1,我还不能更新它们。所以当我实现std::tr1::shared_ptr 时,我看到编译器使用了 boost/shared_ptr.hpp (boost 1.33.1)
2) 好吧,程序使用了很多线程,我什至不知道它们是如何工作的以及它们完全做什么(我工作的大型项目),但我知道,我不使用任何共享变量或其他可能导致此行为的原因
3) 有时它只是打印:
对象已创建 0
对象已创建 1
...
对象已创建 9999
一切正常
有时它会打印 0-1-2-3-4(或更多)行,然后停止。此外 - 我知道,对象已经创建,但函数没有返回值,程序只是冻结,当我尝试使用 gdb 附加到程序并键入“where”时 - 我看到了这个:
0) __kernel_vsyscall() 中的 0xb7fd8430
1) 来自 /lib/i686/libpthread.so.0 的 _lll_mutex_lock-wait() 中的 0xb7d9bece
2) 0xb7d98500 in _L_mutex_lock_71 () from /lib/i686/libpthread.so.0
3) 0xbfbefab8 在 ?? ()
4) 0x00000000 在 ?? ()
或者这个:
0) __kernel_vsyscall() 中的 0xb7fd8430
1) 来自 /lib/i686/libpthread.so.0 的 _lll_mutex_lock-wait() 中的 0xb7d9bece
2) 0xb7d98500 in _L_mutex_lock_71 () from /lib/i686/libpthread.so.0
..不知道这里是什么,我只看到“.. in ?? ()”
10) .. 在 __gthread_mutex_lock 中
11) .. 在 __gthread_mutex_lock 中
12) .. 在 std::tr1::_Sp_counted_base::release 中
13) .. in ~shared_count
14) .. 在 ~shared_ptr
好像shared_ptr 坏了?
【问题讨论】:
标签: boost shared-ptr tr1 c++03