[英]I have 1 memory leak that I cannot free
#define _CRT_SECURE_NO_WARNINGS
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
#include < crtdbg.h >
#define _CRTDBG_MAP_ALLOC
enum {
RUNNING = 1
};
struct Point {
int x, y;
};
struct Line {
Point start;
Point end;
};
struct GraphicElement {
enum {
SIZE = 256
};
unsigned int numLines; //number of lines
Line * pLines; //plines points to start and end
char name[SIZE];
};
typedef struct {
unsigned int numGraphicElements;
GraphicElement * pElements; //the head points to pLines
}
VectorGraphic;
void InitVectorGraphic(VectorGraphic * );
void AddGraphicElement(VectorGraphic * );
void CleanUpVectorGraphic(VectorGraphic * );
VectorGraphic Image;
int main() {
char response;
InitVectorGraphic( & Image);
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
while (RUNNING) {
printf("\nPlease select an option:\n");
printf("1. Add a Graphic Element\n");
printf("2. List the Graphic Elements\n");
printf("q. Quit\n");
printf("CHOICE: ");
fflush(stdin);
scanf("%c", & response);
switch (response) {
case '1':
AddGraphicElement( & Image);
break;
case '2':
ReportVectorGraphic( & Image);
break;
case 'q':
CleanUpVectorGraphic( & Image);
return 0;
default:
printf("Please enter a valid option\n");
}
printf("\n");
}
}
/*initialize the vectors, allocate memory*/
void InitVectorGraphic(VectorGraphic * pImage) { //addres of pImage is passed in
pImage - > pElements = (GraphicElement * ) malloc(sizeof(GraphicElement)); //pImage is now the addess of image
pImage - > numGraphicElements = 0;
}
/*add values into the vectors list.*/
void AddGraphicElement(VectorGraphic * pImage) {
struct GraphicElement * pElements;
struct GraphicElement * pSecond;
struct Point point;
struct Line * line;
unsigned int numLines;
char name[256];
int x;
int y;
int i = 0;
int count = 0;
//allocate memory
line = (struct Line * ) malloc(20 * sizeof(Line)); //allocate memory for line, in order to accsess Line struct values
pElements = (GraphicElement * ) malloc(20 * sizeof(GraphicElement));
printf("Please enter the name of the new GraphicElement(<256 characters): ");
scanf("%s", name);
strcpy(pElements - > name, name); //copy the elements name
printf("How many lines are there in the new GraphicElement? ");
scanf("%u", & numLines);
pElements - > numLines = numLines; //pass the number of lines indicated
pImage - > pElements = pElements; //pass the number of lines indicated
pImage - > pElements[pImage - > numGraphicElements] = * pElements; //pass the elements into pImage
pImage - > numGraphicElements++; //number of elements
一旦我按 q 退出。 程序顯示我有內存泄漏。 檢測到內存泄漏! 在 0x00EF9880 處轉儲對象 ->{69} 普通塊,264 字節長。 數據:<> CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 對象轉儲完成
/*clear everything, no memory leaks*/
void CleanUpVectorGraphic(VectorGraphic * pImage) {
/* free all Lines pointers */
for (int i = 0; i < pImage - > pElements - > numLines; i++) {
free(pImage - > pElements[i].pLines);
}
pImage - > pElements - > numLines = 0;
pImage - > numGraphicElements = 0;
if (pImage != NULL) {
free(pImage - > pElements);
}
}
基本上我的程序會提示用戶輸入元素名稱和元素數量。 之后,我為 start(x,y) 和 end(x,y) 輸入 int 值。 之后我按 q 退出並退出程序。 我已經通過調試器,但沒有弄清楚內存泄漏在哪里。 我嘗試了很多不同的策略,但都失敗了。 我怎樣才能釋放這個內存泄漏?
注意:另外,如果我添加第二個元素,內存泄漏會重復。
從內存塊的大小來看,它看起來像一個指向您嘗試釋放的 GraphicElement 的指針沒有指向任何東西
在使用、取消引用或釋放它們之前始終檢查原始指針:
void CleanUpVectorGraphic(VectorGraphic * pImage) {
//always check input
if(!pImage) {
return;
}
/* free all Lines pointers */
for (int i = 0; i < pImage->pElements->numLines; i++) {
if(pImage->pElements && pImage->pElements[i] && pImage->pElements[i].pLines) {
free(pImage->pElements[i].pLines);
}
}
pImage->pElements->numLines = 0;
pImage->numGraphicElements = 0;
if (pImage != NULL) {
free(pImage->pElements);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.