-
避免對shared_ptr所管理的對象的直接內(nèi)存管理操作,以免造成該對象的重釋放
shared_ptr并不能對循環(huán)引用的對象內(nèi)存自動管理(這點(diǎn)是其它各種引用計(jì)數(shù)管理內(nèi)存方式的通病)。
-
不要構(gòu)造一個(gè)臨時(shí)的shared_ptr作為函數(shù)的參數(shù)。
如下列代碼則可能導(dǎo)致內(nèi)存泄漏:
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
西津渡