Help with dynamic structure arrays

I have made a program using structures, like this

1
2
3
4
5
6
7
typedef struct tagDATA{
char[128] Route;
char[64] Name;
char[100] Description;
char[128] Image;
GLuint ImageData;
}DATA;

and then placed them into another struct, like this

1
2
3
4
5
6
typedef struct tagMAIN
{
DATA * Data;
}MAIN

MAIN Main1;

Then i create them like this:

Main1.Data = new DATA[counter];

Until there everything works fine, however, there is a terrible problem, i need to be able to free the memory used by "Data", since when it recreates them when number of struct goes up and down, the memory used goes up, until leak and program crash

How i can free it?
I have tryied using delete[], delete, free(), realloc, well, please if someone can tell me how i can free that memory? or the correct way to use these functions to free it? its a program that works dinamically, so, in the same execution it will have more or less depending on the user, but isn't there any way to free that space?

It doesn't matters much if i need to clear the main structure, place it in a class or something, but please explain me how do i do for be able to use the memory, without ending up with a memory leak
Last edited on
delete[] Main1.Data; should work fine. Are there any problems with it?
Well, it doesn't seems to really free the memory, idk why, as i keep looping it, memory used by program (used task manager for see), doesn't goes down, instead of that, everytime structs are created again, the amount of memory used goes up
That might be a problem of your code structure rather than delete[].
Try running
1
2
3
4
5
6
7
int main(){
   while(true){
      int *i = new int[10];
      delete[] i;
   }
   return 0;
}
It kept looping until crashed by too many iterations, but memory didn't variate
I tested it multiple times, since something was wrong, when i use Delete, seems like it can affect other variables, like if its not deleting them on a controlled quantity

I wanna be able to free up the space... cause it keeps going on crash by resource leak
I have readed that if i don't allocate memory before creating pointer, it cannot be freed, in that case, how would i allocate it and then free it correctly? tryied but doesn't seems like something different happens xD

Ah, updated the structure im using, forgot, idk if the GLuint might affect this
Why are you posting this? We don't know your code, so we can't help you with it.
After searching a lot through my code, i found that probably the problem lies in the GLuint

If want my code... hmm, its quite long, rewrited it in the shortest way posible, since data for definitions and that are obtained from a file, and too long methods

I need to be able to free the memory used by that GLuint

Part of the code came from Nehe OpenGL tutorial

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <windows.h>										// Header File For Windows
#include <stdio.h>											// Header File For Standard Input / Output
#include <stdarg.h>											// Header File For Variable Argument Routines
#include <stdafx.h>											// Header File For Variable Argument Routines
#include <gl\gl.h>											// Header File For The OpenGL32 Library
#include <gl\glu.h>											// Header File For The GLu32 Library
#include <gl\glaux.h>										// Header File For The Glaux Library

using namespace System;

typedef struct tagMAPDATA
{
	char Route[128];
	char Name[20];
	char Image[128];
	char Description[100];
	GLuint ImageData;
}MAPDATA;
typedef struct tagMAP
{
	MAPDATA* MapData;
}MAP;

MAP Map1;

int LoadImage(char* image, int loopy)										// Load Bitmaps And Convert To Textures
{
        int Status=FALSE;									// Status Indicator
        AUX_RGBImageRec *RegSystemImage[1];					// Create Storage Space For The Textures
        memset(RegSystemImage,0,sizeof(void *)*1);			// Set The Pointer To NULL

        if ((RegSystemImage[0]=LoadBMP(image)))	// Load The Font
        {
		Status=TRUE;									// Set The Status To TRUE

		glGenTextures(1, &Map1.MapData[loopy].ImageData);					// Create The Texture

		// Typical Texture Generation Using Data From The Bitmap
		glBindTexture(GL_TEXTURE_2D, Map1.MapData[loopy].ImageData);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, RegSystemImage[0]->sizeX, RegSystemImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, RegSystemImage[0]->data);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	}

	if (RegSystemImage[0])									// If Texture Exists
	{
		if (RegSystemImage[0]->data)							// If Texture Image Exists
		{
			free(RegSystemImage[0]->data);					// Free The Texture Image Memory
		}

		free(RegSystemImage[0]);								// Free The Image Structure
	}

	return Status;											// Return The Status
}

int main(array<System::String ^> ^args)
{
	bool done = false;
    Console::WriteLine(L"Preparing for tests");
	while(!done)
	{
	Console::ReadLine();
	int limit = 10;
	int counter = 0;
	Map1.MapData = new MAPDATA[limit];
	while(counter < limit)
	{
		strcpy(Map1.MapData[counter].Description,"A simple but a little long lenght description, sorry by my bad english");
		strcpy(Map1.MapData[counter].Name,"Alpha Test is called");
		strcpy(Map1.MapData[counter].Image,"This is supposed to be a route like Images/Something/idk.bmp");
		strcpy(Map1.MapData[counter].Route,"This is supposed to be a route like Files/Something/idk.txt");
		Map1.MapData[counter].ImageData = 500;
		LoadImage(Map1.MapData[counter].Image,counter);
		counter++;
	}
	Console::ReadLine();
	delete[] Map1.MapData;
	counter = 0;
	}
    return 0;
}


and

1
2
3
4
5
6
7
8
9
10
11
12
13
class AUX_RGBImageRec {
   void convertBGRtoRGB();
 public:
   byte *data;
   DWORD sizeX;
   DWORD sizeY;
   bool NoErrors;
   AUX_RGBImageRec(): NoErrors(false), data(NULL) {};
   AUX_RGBImageRec(const char *FileName);
   ~AUX_RGBImageRec();
   bool loadFile(const char *FileName);
   friend AUX_RGBImageRec *auxDIBImageLoad(const char *FileName);
};
Topic archived. No new replies allowed.