-
避免對shared_ptr所管理的對象的直接內存管理操作,以免造成該對象的重釋放
shared_ptr并不能對循環引用的對象內存自動管理(這點是其它各種引用計數管理內存方式的通病)。
-
不要構造一個臨時的shared_ptr作為函數的參數。
如下列代碼則可能導致內存泄漏:
void test()
{
foo(boost::shared_ptr<implementation>(new implementation()),g());
}
正確的用法為:
void test()
{
boost::shared_ptr<implementation> sp (new implementation());
foo(sp,g());
}
-
Employee boss("Morris, Melinda", 83000);
Employee* s = &boss;
This is usually not a good idea. As a rule of thumb, C++ pointers should
only refer to objects allocated wth new.
copy:http://www.diybl.com/course/3_program/c++/cppjs/20090403/163770.html
西津渡