gpt4 book ai didi

c++ - 我有 1 处内存泄漏,无法释放

转载 作者:行者123 更新时间:2023-11-30 19:51:38 25 4
gpt4 key购买 nike

    #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 退出。该程序显示我有内存泄漏。检测到内存泄漏!转储对象 ->{69} 位于 0x00EF9880 的正常 block ,长度为 264 字节。数据:<> 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);
}
}

关于c++ - 我有 1 处内存泄漏,无法释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39691954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com