【问题标题】:Boost shared_ptr issue with TR1 libraries使用 TR1 库提升 shared_ptr 问题
【发布时间】: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 坏了?

【问题讨论】:

  • 我猜你正在做其他事情,你向​​我们展示了,这弄乱了堆栈,可能发生了某种未定义的行为。您向我们展示的代码不足以帮助您解决问题。
  • @Joachim Pileborg 相信我,我没有做任何其他事情。这是一个大型项目,我正在编辑一段本地代码。几天前我遇到了这个问题link,所以我试图隔离这个问题,这个主题描述了我发现的(这个问题发生在shared_ptr添加之后)
  • 使用某种内存调试器,例如Valgrind。在这种情况下,它可能会对您有所帮助。

标签: boost shared-ptr tr1 c++03


【解决方案1】:

我刚刚解决了这个问题。改变了这个:

#include &lt;tr1/memory&gt;#include &lt;boost/shared_ptr.hpp&gt;

std::tr1::shared_ptrboost::shared_ptr

这里描述了解决方案link

【讨论】:

  • 这是一个非常有用的参考链接。我正在优化标签,以便未来的用户可以尽快找到它。您还可以从链接线程中添加更多背景到您的答案中,以防止链接腐烂
猜你喜欢
  • 2010-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
  • 2020-03-17
  • 2011-04-19
相关资源
最近更新 更多