#ifndef __TOOLS_H__
#define __TOOLS_H__
#include <iostream>
#include <string>
class Trace
{
public:
Trace(const std::string & pFunc):mFunc(pFunc)
{
std::cout<<"Enter"<<" "<<mFunc<<std::endl;
}
~Trace()
{
std::cout<<"Exit"<<" "<<mFunc<<std::endl;
}
private:
std::string mFunc;
};
#define trace Trace lUsetoTrace(std::string(__FUNCTION__));
#endif
#include <memory>
#include <stdexcept>
#include <vector>
#include "Tools.h"
using namespace std;
//基本原理:在return函数或有异常发生函数结束时,静态变量的析构函数会自动执行
class TestClass
{
public:
//如何管理类成员动态内存
TestClass()
{
/*使用异常保证由类管理的动态内存不会泄露
使用try因为我们在类中使用了指针维护动态内存
而 tag 0 区域可能会有异常
这导致了mDymnic指向的动态内存泄露
使用异常保证了发生异常a被正确回收
但这仅对构造函数的情况有效,因为没有return
*/
try
{
trace
mDymanic = new int;
//tag 0 start
//...
throw runtime_error("TestSub Init Failed!");
//tag 0 end
}
catch(exception &e)
{
cout<<"E:"<<endl;
cout<<" "<<e.what()<<endl;
cout<<" "<<"delete a.."<<endl;
delete mDymanic;
throw runtime_error("TestClass init error!");
}
}
//如何管理临时动态内存
void print()
{
cout<<"start ";
cout<<*mDymanic;
cout<<" end"<<endl;
/*不使用new来开辟多余一个的临时空间或成员空间
而使用标准库代替
使用智能指针维护单个临时对象的
使用try因为tag 1 中可能有异常或return发生将导致使用new分配的内存无法正确的和delete配对
使用静态变量管理动态内存将保证析构函数的执行,从而delete内存
*/
try
{
//auto_ptr 没有非配数组的版本,使用标准库来管理buffer是准确的
vector<char> lbuffer(100);
char *buffer = lbuffer.data();
size_t bufferLen = lbuffer.size();
//start to use buffer
auto_ptr<int> p(new int);
//tag 1 start
//...
//int *p = new int;
throw runtime_error("dynamic error!");
return ;
//这个delete可能永远都不会执行因为前面的程序流可能会先return或有异常从而结束函数
//所以靠下面的delete总会执行来保证new分配的内存会被回收是显然不行的
//delete p;
//tag 1 end
}
catch(exception &e)
{
cout<<"E:"<<endl;
cout<<" "<<e.what()<<endl;
throw runtime_error("print failed!");
}
}
~TestClass()
{
cout<<"TestClass uninit"<<endl;
delete mDymanic;
}
private:
int *mDymanic;
};
int main()
{
try
{
trace
TestClass t;
t.print();
}
catch(exception &e)
{
cout<<"E:"<<endl;
cout<<" "<<e.what()<<endl;
cout<<" "<<"Main catch a except.."<<endl;
}
return 0;
}