2012-05-15 12 views
-1

您好,我需要一些幫助,以下代碼和內存泄漏。從這項計劃內存泄漏在陣列上的功能

錯誤日誌顯示我真​​的encountring在我的程序以下泄漏:

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 

考慮到我的功能和倉儲類是沒有我做錯了什麼......

類的頭(滿):

#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!"); 
} 
} 
+1

您還沒有表現出相關的代碼,因此它不可能說... –

+0

還有什麼要告訴你:?| –

+0

看看每個堆棧跟蹤的頂部... –

回答

1

即使世界在這個函數delByID(部分除外)或本removeAssignment()

+0

但爲什麼,我的功能不正確,他們不應該工作嗎? –

+0

當您嘗試分配某些內容時,異常狀態表明出現錯誤,但程序可以按預期運行。請務必刪除()你的任務,因爲你的日誌說:89仍然可及的分配(S) – Joelmob

+0

謝謝....發現問題我沒有解構UIconsole中的一個指針。 –