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