3월 23, 2017

[EC++ 17] Store  new ed objects in smart pointers in standalone statements.


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

위 코드는 1. new  메모리 할당 ->  shared_ptr  ctor call ->  priority()  call의 세 단계에 걸쳐 수행된다. 그런데 컴파일러에 의해  shared_ptr  ctor call과  priority()  call의 순서가 바뀔 수 있는데, 이 경우  priority() 가 예외를 던지면 memory leak이 발생하게 된다. 따라서  new ed object를   shared_ptr 에 넘겨주는 부분은 별도의 문장으로 따로 빼내는 것이 좋다.



Summary

  • Store  new ed objects in smart pointers in standalone statements. Failure to do this can lead to subtle resource leaks when exceptions are thrown.

Reference

  • Effective C++ by Scott Meyers

댓글 1개:

  1. I appreciate you for providing this wonderful information. This is a very comprehensive blog. Thank you for your time and dedication! Please continue to share such blogs. Also, Checkout business-administration-degree-form

    답글삭제