如何在各种情况下使用智能指针? [英] How are smart pointers supposed to be used in various cases?

查看:57
本文介绍了如何在各种情况下使用智能指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次编辑(以下是一些答案,仍然存在的问题,原始问题.)


一位朋友向我介绍了共享指针,因为它们非常适合具有多个层,这些层取决于同一个对象的寿命,并且您要做的就是创建对象而无需删除其他代码.由于设计已经变得复杂,因此清理起来变得容易得多.

我意识到您可以根据需要拥有指向相同数据的多个共享指针,但是由于reset会将其引用计数设置为零,因此请勿尝试在其中一个调用reset,然后尝试使用另一个.当我需要重新开始新一轮的一切时,我已经使用了reset,但是我没有使用它进行清理.

这很干净,因为使用共享或弱指针的向量,您甚至不必调用clear即可将其删除,重新分配给向量或仅使其超出范围就足够了.

似乎只有在有循环引用的情况下才可能发生共享指针泄漏,在这种情况下,两个共享指针相互之间拥有共享指针,如果无法避免,这似乎并不常见,但可以通过使一个弱点来解决(或原始)指针来打破循环.可能发生泄漏的另一种可能性是,您已经习惯于使用智能指针,以至于在您不知不觉地声明诸如原始指针的向量之类的东西时,可能会忽略所需的清除操作.

弱指针的挑战似乎是要确保它们的有效性,以便他们检查相关共享指针的有效性以确保其有效性.弱指针的好处也许在于,您不必担心删除或创建它们,只需重新生成它们.因此,如果原件被破坏,它们可能会在不应持久的层次上表现出色.我的朋友这样做是为了使GUI层的指针几乎都是弱的.

我不知道关于使函数返回类型和参数成为弱指针而不是在那里具有原始或共享指针的答案,但是至少使返回类型为弱使得我们显然在使用智能指针.由于相同的原因,我正在考虑对参数进行相同的操作.

似乎交易都应该有很强的指针.也许我错了.我将保留并行接口,因为它们具有指向相同数据的共享指针.欢迎提供建议.

原始问题


问题是,智能指针应该如何工作?除一个地方外,其他所有事物都应该具有弱指针吗?还是有可能存在多个共享指向同一原始指针的指针的情况?

我一直在想,有可能有多个指向同一数据的共享指针.我错了吗?

交易是否应该保持弱势指针?

具有相同对象的并行接口又如何呢?必须弱一点吗?

我一直在使用家用旋转的智能指针系统很长时间了,因为显示出泄漏的迹象,这对我来说并不令人难忘.但这似乎确实使我可以创建指向同一原始指针的多个(称为弱")指针,而不会出现泄漏.我一直以为这就是共享指针的工作方式.我的家纺版本是否与众不同?也许没什么不同,但我仍然不应该使用这种方式.

最好的答案是,有多个共享指针指向同一个原始指针,只是不要在其中一个上调用reset,然后再尝试使用另一个? (如果是这样,当使用指向同一原始指针的多个共享指针时,搜索reset()的所有情况似乎是一种追踪可能的泄漏原因的好方法.)

如果您有多个指向同一数据的共享指针,则对其中一个调用reset只会导致ref计数递减,对吗?如果强(共享)指针超出范围,则将调用析构函数并调用Reset(),这将释放引用并减少引用计数,对吗?还是应该使引用计数变为零?

另外,让函数返回共享指针或将共享指针作为参数是错误还是不好?看起来是正确的,让函数返回弱指针是一件好事,但是笨拙地将弱指针作为参数传递.还是只返回原始指针会更好?

谢谢!

1st Edit (Some answers, still questions, original questions below.)


A friend introduced me to shared pointers because they were good for having multiple layers that depended on the life of the same object and that all you had to do was create the object without further code to delete. Since the design was already getting complex it just made cleaning up that much easier.

I''m realizing you can have as many shared pointers to the same data as you want but since reset sets the their ref count to zero, don''t try to call reset on one then try and use another. I''ve used reset when I''ve needed to start over with a new round of everything but I haven''t used it for cleanup.

It''s neat because with vectors of shared or weak pointers you don''t even have to call clear to delete them, reassigning to the vector or just letting it go out of scope is enough.

It seems that a leak with shared pointers is only possible if you have cyclic references, where two shared pointers hold shared pointers to each other, which would appear to be uncommon if not avoidable, but which could be fixed by having one be a weak (or raw) pointer to break the cycle. Perhaps another possibility for a leak is in that you get so used to using smart pointers that you might overlook the cleanup needed when you happened to unconsciously declare something such as a vector of raw pointers.

The challenge with weak pointers seems to be to make sure they''re valid so they check the validity of their related shared pointer to make sure. Maybe a benefit with weak pointers is that you don''t have to worry about deleting or creating them, just regenerating them. So maybe they''d be good in layers where they shouldn''t persist if the original are destroyed. My friend made it so that the GUI layer had mostly weak pointers.

I don''t know the answer about making function return types and parameters weak pointers over having raw or shared pointers there but at least having the return type as weak makes it evident we''re using smart pointers. I''m thinking about doing the same with parameters for the same reason.

It seems like transactions should all have strong pointers. Maybe I''m wrong. I''m going to leave the parallel interfaces as having shared pointers to the same data. Advice is welcome.

Original question


The question is, how are smart pointers supposed to work? Is everything except one place supposed to have weak pointers? Or is there ever a situation where you can you have multiple shared pointers to same raw pointer?

I have been thinking that it''s possible to have multiple shared pointers that point to the same data. Am I wrong?

Are transactions supposed to hold a weak pointer?

What about parallel interfaces that have the same object? Must one be weak?

I''ve been using a home-spun smart pointer system for a long time and it''s not memorable to me as having shown signs of leaking. But it does seem to allow me to create multiple, what I call strong (and weak) pointers to the same raw pointer without appearing to leak. I''ve always assumed this is how shared pointer works. Is my home-spun version different? Maybe it''s not different but I still shouldn''t be using this way.

Is the answer that it''s OK, best, to have multiple shared pointers to the same raw pointer, just don''t call reset on one of them and then try to use another? (If so, when using multiple shared pointers to the same raw pointer, then searching for all cases of reset() would seem like a good way to track down possible reasons for leaks.)

If you had multiple shared pointers pointing to the same data, calling reset on one would just cause the ref count to decrement, correct? If the strong (shared) pointer goes out of scope, the destructor would be called and Reset(), which would just release the ref and decrement the ref count, correct? Or should it cause the ref count to go to zero?

Also, is it wrong or bad to have functions return a shared pointer or take a shared pointer as a parameter? It seems like it''d be correct, good to have functions return a weak pointer, but clumsy to pass a weak pointer as parameter. Or is it better just to return a raw pointer?

Thanks!

推荐答案

以下链接对于智能指针
Following link would be a good reference for smart pointers http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=239[^]


这篇关于如何在各种情况下使用智能指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆