2008 July 16th Wenesday (七月 十六日 水曜日)
2008-08-01 20:39
344 查看
There is a memory leak example.
void test2(int n)
{
double * ar = new double
;
...
if (oh_no)
throw exception();
...
delete [] ar;
return;
}
Unwinding the stack removes the variable ar from the stack. But the premature termination of the function
means the delete [] statement at the end of the function is skipped. The pointer is gone, but the memory block
it pointed to is still intact and inaccessible. In short, there is a memory leak.
The leak can be avoided.
void test3(int n)
{
double * ar = new double
;
...
try {
if (oh_no)
throw exception();
}
catch(exception & ex)
{
delete [] ar;
throw;
}
...
delete [] ar;
return;
}
void test2(int n)
{
double * ar = new double
;
...
if (oh_no)
throw exception();
...
delete [] ar;
return;
}
Unwinding the stack removes the variable ar from the stack. But the premature termination of the function
means the delete [] statement at the end of the function is skipped. The pointer is gone, but the memory block
it pointed to is still intact and inaccessible. In short, there is a memory leak.
The leak can be avoided.
void test3(int n)
{
double * ar = new double
;
...
try {
if (oh_no)
throw exception();
}
catch(exception & ex)
{
delete [] ar;
throw;
}
...
delete [] ar;
return;
}
相关文章推荐
- 2008 July 30th Wednesday (七月 三十日 水曜日)
- 2008 April 16th Wednesday (四月 十六日 水曜日)
- July 16th Monday (七月 十六日 月曜日)
- 2008 January 16th Wednesday (一月 十六日 水曜日)
- 2008 July 9th Wednesday (七月 九日 水曜日)
- 2008 July 23th Wednesday (七月 二十三日 水曜日)
- July 16th Thursday (七月 十六日 木曜日)
- 2008 July 2nd Wednesday (七月 二日 水曜日)
- 2008 July 3th Thursday (七月 三日 木曜日)
- 2008 July 15th Tuesday (七月 十五日 火曜日)
- 2008 July 4th Friday (七月 四日 金曜日)
- 2008 July 17th Thursday (七月 十七日 木曜日)
- 2008 July 29th Tuesday (七月 二十九日 火曜日)
- 2008 October 16th Thursday (十月 十六日 木曜日)
- July 4th Wednesday (七月 四日 水曜日)
- July 29th Wednesday (七月 二十九日 水曜日)
- 2008 July 7th Monday (七月 七日 月曜日)
- 2008 July 22th Tuesday (七月 二十二日 火曜日)
- 2008 July 28th Monday (七月 二十八日 月曜日)
- 2008 July 8th Tuesday (七月 八日 火曜日)