[英]Memory leak and valgrind errors in this small for loop?
我在這部分代碼中遇到了麻煩,該代碼在valgrind中生成錯誤。 當我注釋代碼並運行valgrind時,我沒有收到任何內存泄漏或錯誤,因此此循環應為原因:
///Print the top users
const char* str;
for (int i = 0; i < count; i++) {
if (FinalArray[i].Score == -1) {
break;
}
int id = UserGetID(user);
char* name = UserGetName(user);
int finalID = UserGetID(FinalArray[i].user);
char* finalName = UserGetName(FinalArray[i].user);
assert(finalName!= NULL && name !=NULL);
str = mtmSuggestFriends(id, name, finalID, finalName);
if (str == NULL) {
return MAIN_ALLOCATION_FAILED;
}
// fprintf(fileOutput, str);
}
在此循環之后,我只返回一個聲明成功的枚舉。
以下是Valgrind中的錯誤:
==8779== Use of uninitialised value of size 8
==8779== at 0x4037C2: UserGetName (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779== by 0x401FAC: SuggestFriends (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779== by 0x402E6D: executeUserCommand (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779== by 0x40281B: main (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779==
==8779== Use of uninitialised value of size 8
==8779== at 0x4037A0: UserGetID (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779== by 0x401FC8: SuggestFriends (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779== by 0x402E6D: executeUserCommand (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779== by 0x40281B: main (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779==
==8779== Invalid read of size 1
==8779== at 0x403F1A: mtmSuggestFriends (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779== by 0x401FEE: SuggestFriends (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779== by 0x402E6D: executeUserCommand (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779== by 0x40281B: main (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779== Address 0x9848B4458BB44589 is not stack'd, malloc'd or (recently) free'd
==8779==
==8779== Process terminating with default action of signal 11 (SIGSEGV)
==8779== General Protection Fault
==8779== at 0x403F1A: mtmSuggestFriends (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779== by 0x401FEE: SuggestFriends (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779== by 0x402E6D: executeUserCommand (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779== by 0x40281B: main (in /u1/023/mtm/ex2/RUN/mtm_isocial)
==8779==
==8779== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 4 from 1)
==8779== malloc/free: in use at exit: 1,250 bytes in 93 blocks.
==8779== malloc/free: 455 allocs, 362 frees, 10,081 bytes allocated.
==8779== For counts of detected errors, rerun with: -v
==8779== searching for pointers to 93 not-freed blocks.
==8779== checked 122,512 bytes.
==8779==
==8779== LEAK SUMMARY:
==8779== definitely lost: 0 bytes in 0 blocks.
==8779== possibly lost: 0 bytes in 0 blocks.
==8779== still reachable: 1,250 bytes in 93 blocks.
==8779== suppressed: 0 bytes in 0 blocks.
==8779== Reachable blocks (those to which a pointer was found) are not shown.
==8779== To see them, rerun with: --show-reachable=yes
函數ToStringUser返回const char *的malloc。因此,我不必擔心釋放它嗎?
知道為什么會這樣嗎?
我嘗試使用for中的此代碼釋放str,但我不斷收到相同的錯誤和相同數量的內存泄漏:
free((char*) str); OR free((void*) str);
這是User的結構以及getID和getName:
struct User_t {
char *Name;
int ID;
int Birth;
};
int UserGetID(User user) {
return user->ID;
}
char* UserGetName(User user) {
return user->Name;
}
在循環之前,我以此初始化一個新用戶:
User user = FindUserPointer(setUser, id);
使用的功能是這樣的:
static User FindUserPointer(Set users, int ID) {
assert(users!=NULL);
User tmpUser = UserCreate("temp", ID, 99);
if (tmpUser == NULL) {
return NULL;
}
SET_FOREACH(User,Itrator1,users) {
if (UserCompare(tmpUser, Itrator1) == 0) {
UserFree(tmpUser);
return Itrator1;
}
}
UserFree(tmpUser);
return NULL;
}
Valgrind並不是在抱怨泄漏-它是在抱怨您正在讀取未初始化的內存並取消引用無效的指針(無效的指針deref導致程序崩潰-至少在Valgrind下)。
我們需要查看UserGetID()
和UserGetName()
才能確定其中的錯誤(但是可能還不夠)。
根據您的評論mtmSuggestFriends
是您沒有源的目標文件,我猜測UsetGetID()
和/或UserGetName()
將無效的指針傳遞給mtmSuggestFriends
。
首先,您要傳入一個未分配的指針user
。 然后,從UserGetID()
調用的SuggestFriends()
函數將這個充滿隨機性的垃圾指針用作真實指針,從而導致無效讀取( SEGV
)
您可能會發現,設置“警告為錯誤”(gcc上的-Werr
)可能會向您顯示您在做不可預測的事情。
struct User_t {
char *Name;
int ID;
int Birth;
};
int UserGetID(User user) {
return user->ID;
}
... User
定義在哪里?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.