#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退出。该程序显示我有内存泄漏。 检测到内存泄漏! 转储对象 - &gt; {69}普通块在0x00EF9880,264字节长。 数据:&lt;&gt; 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退出并退出程序。我已经通过调试器而没有弄清楚这个内存泄漏的位置。我尝试了很多不同的策略,但我失败了。我怎样才能解除内存泄漏?
注意:如果我添加第二个元素,则内存泄漏会重复。
答案 0 :(得分:-1)
从内存块的大小看,它看起来像一个指向你试图释放的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);
}
}