If you trying to do multiple things in one statement, you should think carefully abnormal behavior especially exceptions. Let's see an example:

processWidget(std::tr1::shared_ptr<Widget>(new Widget()), priority());

What you expected might be

new Widget;
shared_ptr<Widget>(...);
priority();

But it may execute in the order of 

new Widget;
priority();
shared_ptr<Widget>(...);

if priority() yield exception, you are running into resource leak. Because the pointer will be lost before stored into shared_ptr.

So, what's the suggestion?

Just store newed object in smart pointer in standalone statement.

std::tr1::shared_ptr<Widget> pw(new Widget);
processWidget(pw, priority());

 

 

 

相关文章:

  • 2021-08-19
  • 2022-12-23
  • 2022-12-23
  • 2021-12-25
  • 2022-12-23
  • 2022-12-23
  • 2021-08-27
  • 2021-09-14
猜你喜欢
  • 2022-12-23
  • 2022-01-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-18
  • 2022-12-23
相关资源
相似解决方案