您好我需要一些关于以下代码和内存泄漏的帮助。
此程序的错误日志显示我在程序中遇到以下泄漏:
Dr. Memory version 1.4.6 build 2 built on Mar 7 2012 10:14:04
Application cmdline: ""D:\c++\Begin\L_6-8\Debug\L_6-8.exe""
Recorded 62 suppression(s) from default C:\Program Files (x86)\Dr. Memory/bin/suppress-default.txt
Error #1: UNADDRESSABLE ACCESS: reading 0x0077f5a0-0x0077f5a4 4 byte(s)
# 0 Assignment::getID() [..//Assignment.h:26]
# 1 AssignmentRepository::searchById() [D:\c++\Begin\L_6-8\Debug/../AssignmentRepository.cpp:10]
# 2 AssignmentRepository::delByID() [D:\c++\Begin\L_6-8\Debug/../AssignmentRepository.cpp:30]
# 3 Catalog::removeAssignment() [D:\c++\Begin\L_6-8\Debug/../Catalog.cpp:38]
# 4 _fu63___ZSt4cout [D:\c++\Begin\L_6-8\Debug/../UIconsole.cpp:148]
# 5 UIconsole::runUI() [D:\c++\Begin\L_6-8\Debug/../UIconsole.cpp:24]
# 6 Application::run() [D:\c++\Begin\L_6-8\Debug/../App.cpp:48]
# 7 main [D:\c++\Begin\L_6-8\Debug/../App.cpp:25]
Note: @0:00:21.491 in thread 6388
Note: refers to 1 byte(s) beyond last valid byte in prior malloc
Note: prev lower malloc: 0x0077f590-0x0077f5a0
Note: instruction: mov (%eax) -> %eax
Error #2: POSSIBLE LEAK 100 direct bytes 0x0077f5c8-0x0077f62c + 0 indirect bytes
# 0 libstdc++-6.dll!_cxa_allocate_exception
# 1 AssignmentRepository::delByID() [D:\c++\Begin\L_6-8\Debug/../AssignmentRepository.cpp:36]
# 2 Catalog::removeAssignment() [D:\c++\Begin\L_6-8\Debug/../Catalog.cpp:38]
# 3 _fu63___ZSt4cout [D:\c++\Begin\L_6-8\Debug/../UIconsole.cpp:148]
# 4 UIconsole::runUI() [D:\c++\Begin\L_6-8\Debug/../UIconsole.cpp:24]
# 5 Application::run() [D:\c++\Begin\L_6-8\Debug/../App.cpp:48]
# 6 main [D:\c++\Begin\L_6-8\Debug/../App.cpp:25]
DUPLICATE ERROR COUNTS:
SUPPRESSIONS USED:
ERRORS FOUND:
1 unique, 1 total unaddressable access(es)
0 unique, 0 total uninitialized access(es)
0 unique, 0 total invalid heap argument(s)
0 unique, 0 total warning(s)
0 unique, 0 total, 0 byte(s) of leak(s)
1 unique, 1 total, 100 byte(s) of possible leak(s)
ERRORS IGNORED:
89 still-reachable allocation(s)
(re-run with "-show_reachable" for details)
Details: C:\Users\Warzaru\AppData\Roaming/Dr. Memory/DrMemory-L_6-8.exe.9752.000/results.txt
考虑到我的功能和Repository类,我做错了什么......
班级标题(完整):
#ifndef ASSIGNMENTREPOSITORY_H_
#define ASSIGNMENTREPOSITORY_H_
#include "Assignment.h"
#include <vector>
class AssignmentRepository{
private:
vector <Assignment> assignments;
public:
vector <Assignment> getAll();
void save(Assignment);
void delByID(int);
void editAssignment(Assignment);
int searchById(int);
void printAllAssignments();
Assignment *getAssignment(int i);
~AssignmentRepository();
};
#endif /* ASSIGNMENTREPOSITORY_H_
班级cpp(部分):
int AssignmentRepository::searchById(int a){
for(unsigned i=0; i<assignments.size(); i++){
if(a == assignments[i].getID()){
return i;
}
}
return 0;
}
void AssignmentRepository::delByID(int i){
int check;
check = searchById(i);
if(check != 0){
assignments.erase(assignments.begin()+check);
}
else{
throw RepoException("ID does not exist in the database!");
}
}
答案 0 :(得分:1)
此函数delByID()或此removeAssignment()
中存在一些异常