ฉันมีปัญหากับส่วนเล็กๆ ของโค้ดที่สร้างข้อผิดพลาดใน 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 ส่งคืน malloc ของ const char*.. ดังนั้นฉันไม่ควรกังวลเกี่ยวกับการปล่อยมันใช่ไหม
มีความคิดใด ๆ ว่าทำไมสิ่งนี้ถึงเกิดขึ้น?
ฉันพยายามปล่อย str ด้วยโค้ดนี้ใน for แต่ฉันได้รับข้อผิดพลาดเดิมและหน่วยความจำรั่วในปริมาณเท่าเดิม:
free((char*) str); OR free((void*) str);
นี่คือโครงสร้างของผู้ใช้และ 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;
}
user
ใน for loop คืออะไร และโปรแกรมมี segfaults ในฟังก์ชันmtmSuggestFriends
ดังนั้นจึงอาจมีประโยชน์หากจะดู - person huon   schedule 04.05.2012free()
;) โปรดทราบว่าfree()
จะไม่เปลี่ยนค่าของอาร์กิวเมนต์ถึงแม้ว่ามันจะไม่สำคัญก็ตาม - person Philip   schedule 04.05.2012Invalid read of size 1
+SIGSEGV
หมายถึง) คุณควรแก้ไขก่อนที่คุณจะกังวลมากเกินไปเกี่ยวกับfree
ing หน่วยความจำ - person huon   schedule 04.05.2012const
พอยน์เตอร์เพื่อเก็บผลลัพธ์ของmalloc()
มันไม่สมเหตุสมผลเลย เนื่องจากคุณจะต้องเปลี่ยน (เช่นfree()
) ตัวชี้ในภายหลัง - person Philip   schedule 04.05.2012