读书人

vector 内存泄漏?解决方法

发布时间: 2013-03-06 16:20:31 作者: rapoo

vector 内存泄漏?
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

int main( void )
{
vector<int> ints;

for (int i=0; i<10; i++)
{
ints.push_back(i);
}

return 0;
}

上面的代码在linux下用ccmalloc工具检查出内存泄漏。



.--------------------------------------.
|================ ccmalloc-0.4.0 (C) 1997-2003 Armin Biere ================|
+--------------------------------------+
| executable = /home/temp/test |
| startup file = .ccmalloc |
| log file = stderr |
| start time = Thu Feb 28 09:14:37 2013 |
| operating system = Linux 2.4.20-8 i686 on willfardoc |
+--------------------------------------+
| only-count = 0 keep-deallocated-data = 0 |
| check-interval = 0 check-free-space = 0 |
| check-start = 0 file-info = 1 |
| chain-length = 0 additional-line = 1 |
| check-underwrites = 0 print-addresses = 0 |


| check-overwrites = 0 print-on-one-line = 0 |
| sort-by-wasted = 1 sort-by-size = 1 |
| # only-log-chain = 0 continue = 0 |
| # dont-log-chain = 0 statistics = 0 |
| debug = 0 library-chains = 0 |
| load-dynlibs = 0 align-8-byte = 0 |
| only-wasting-alloc= 1 |
`--------------------------------------'
.---------------.
|ccmalloc report|
=======================================================
| total # of| allocated | deallocated | garbage |
+-----------+-------------+-------------+-------------+
| bytes| 1624 | 0 | 1624 |
+-----------+-------------+-------------+-------------+
|allocations| 2 | 0 | 2 |
+-----------------------------------------------------+
| number of checks: 1 |
| number of counts: 2 |
| retrieving function names for addresses ... done. |
| reading file info from gdb ... done. |
| sorting by number of not reclaimed bytes ... done. |


| number of call chains: 1 |
| number of ignored call chains: 0 |
| number of reported call chains: 1 |
| number of internal call chains: 1 |
| number of library call chains: 0 |
=======================================================
|
*100.0% = 1624 Bytes of garbage allocated in 2 allocations
| |
| | 0x42015574 in <???>
| |
| | 0x080492a6 in <main>
| |
| | 0x08049391 in <std::vector<int, std::allocator<int> >::push_back(int const&)>
| |
| | 0x08049566 in <std::vector<int, std::allocator<int> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&)>
| |
| | 0x080497f5 in <std::_Vector_alloc_base<int, std::allocator<int>, true>::_M_allocate(unsigned)>
| |
| | 0x080499a4 in <std::__simple_alloc<int, std::__default_alloc_template<true, 0> >::allocate(unsigned)>
| |
| | 0x4009b2ac in <???>
| |
| | 0x4009b73d in <???>
| |
| | 0x4009b831 in <???>
| |
| | 0x400aee9e in <???>
| |
| `-----> 0x08056943 in <malloc>
| at src/wrapper.c:318
|
`------------------------------------------------------ vector?内存泄漏
------解决方案--------------------


估计是工具统计的时机不对。
[解决办法]
工具傻瓜化导致

读书人网 >C++

热点推荐