简体   繁体   English

在Microsoft Visual C ++中调试共享指针

[英]Shared pointers debugging in Microsoft Visual C++

Is there a way in Microsoft Visual C++ to have a view of the points in the code from which a shared pointer is referenced? Microsoft Visual C ++中是否有一种方法可以查看引用共享指针的代码中的点?

Eg: 例如:

class MyClass;
class MyDerClass : public MyClass;
boost::shared_ptr<MyClass> pmc1 = new MyClass;
boost::shared_ptr<MyClass> pmc2 = pmc1;
boost::shared_ptr<MyDerClass> pmc3 = pmc1;
...
delete pmc1;   // object still around until pmc2 and pmc3 are deleted

Can I know which pointers (at the source code level) still point to that object? 我可以知道哪些指针(在源代码级别)仍然指向该对象?

I don't think this is possible. 我不认为这是可能的。 You can inspect the reference count of one shared_ptr to see how many references to the underlying object are outstanding, but that is not quite what you are after. 可以检查一个shared_ptr的引用计数,以查看对底层对象的引用数量是多少,但这并不是您所追求的。

In order to know which outstanding instances of shared_ptr<T> point to the same T* , you would need to augment or re-implement shared_ptr<> to track (presumably only in a debug build) all shared_ptr<> instances in a static global thread-safe map. 为了知道shared_ptr<T>哪些未完成实例指向同一个T* ,您需要扩充或重新实现shared_ptr<>以跟踪(可能仅在调试版本中)静态全局中的所有shared_ptr<>实例线程安全的地图。 That doesn't quite get you what you want, but if you also add a string (a "label") argument to each shared_ptr<> constructor (perhaps using __FUNCTION__ and some macroizing for context labeling), you can at least dump the map at a certain point in time and review what is pointing to what. 这并不能让你得到你想要的东西,但如果你还为每个shared_ptr<>构造函数添加一个字符串(一个“label”)参数(可能使用__FUNCTION__和一些宏观化进行上下文标记),你至少可以转储地图在某个时间点,并审查什么指向什么。

It may be that you don't really want to inspect all of this in the debugger, but perhaps there are certain situations where you want an assertion and detailed report of a problem? 可能你真的不想在调试器中检查所有这些,但是在某些情况下你需要一个问题的断言和详细报告? Like "when I get here, I should be holding the last reference to this object, and if that's not true I want to raise an assertion and report what else is holding one"? 就像“当我到达这里时,我应该持有对这个对象的最后一个引用,如果这不是真的,我想提出一个断言并报告还有什么东西”? If that's the case, you might be able to get there by enabling some detailed object-by-object logging as references are added and removed. 如果是这种情况,您可以通过在添加和删除引用时启用一些详细的逐个对象日志记录来实现目标。

We had a similar issue regarding mutexes, namely that we wanted to know if there were potential deadlock situations resulting from code paths that could lock and hold two or more mutexes in different orders. 我们有一个关于互斥锁的类似问题,即我们想知道是否存在可能因不同顺序锁定和保存两个或多个互斥锁的代码路径导致的潜在死锁情况。 We ended up building a "safe mutex" that tracked all mutex-locked-order tuples in a global data structure, and logic to detect incompatible orderings. 我们最终构建了一个“安全互斥体”,它跟踪全局数据结构中的所有互斥锁定顺序元组,以及检测不兼容顺序的逻辑。 This was too slow to production, but works nicely in a debug build. 这对于生产来说太慢了,但在调试版本中运行良好。 It could also tell us if a given thread had a mutex locked, to flag issues like holding mutexes during RPC calls, which an be a bad idea in some situations. 它还可以告诉我们一个给定的线程是否锁定了互斥锁,以标记在RPC调用期间持有互斥锁等问题,这在某些情况下是个坏主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM