memory management

hi below is my little test program for windows:
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <windows.h>
#include <gl/gl.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <vector>
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
void DisableOpenGL(HWND, HDC, HGLRC);

class block
{
public:
float x;
float y;
block(float,float);
~block();
};

block::block (float a, float b) {
  x= a;
  y= b;
}
block::~block () {
 // delete &x;
  //delete &y;
}
 std::vector<block> block_array;
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;
    HWND hwnd;
    HDC hDC;
    HGLRC hRC;
    MSG msg;
    BOOL bQuit = FALSE;
    float theta = 0.0f;

    /* register window class */
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_OWNDC;
    wcex.lpfnWndProc = WindowProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = "GLSample";
    wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;


    if (!RegisterClassEx(&wcex))
        return 0;

    /* create main window */
    hwnd = CreateWindowEx(0,
                          "GLSample",
                          "OpenGL Sample",
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          800,
                          600,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hwnd, nCmdShow);

    /* enable OpenGL for the window */
    EnableOpenGL(hwnd, &hDC, &hRC);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    glClearColor(0, 0, 0, 0);
    glOrtho (0,800,600 , 0, 0, 1);
    glDisable(GL_DEPTH_TEST);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.375, 0.375, 0);
    glEnable(GL_BLEND);
    glEnable(GL_TEXTURE_2D);
   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
   srand(1);



   for (int x=0;x<800;x=x+80)
    {
        for (int y=0;y<600;y=y+80)
        {
        block *b=new block(x,y);
        block_array.push_back(*b);
        }
    }

/*   float x=0;
            float y=0;
            int block_iter=0;
            for (x=40;x<800;x=x+80)
            {
            for (y=30;y<600;y=y+60)
            {
               block_array[block_iter].x=x;
               block_array[block_iter].y=y;
               block_iter++;
            }
            }
  */
    /* program main loop */
    while (!bQuit)
    {
        /* check for messages */
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            /* handle or dispatch messages */
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
        else
        {
            /* OpenGL animation code goes here */

            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            //glPushMatrix();
            //glRotatef(theta, 0.0f, 0.0f, 1.0f);

    for (std::vector<block>::iterator it = block_array.begin(); it!=block_array.end(); ++it)
            {
             glLoadIdentity();
    glTranslatef(0.375, 0.375, 0);

    glTranslatef(it->x,it->y,0);
   //glRotatef(block_array[block_iter].x,0,0,1);

            glBegin(GL_QUADS);

                glColor3f(.3f, .3f, .3);
                   glVertex2f(-40,-30);
                glColor3f(.60f, .60f, .60f);
                glVertex2f(40, -30);
                glColor3f(0.9f, 0.9f, 0.9f);
                glVertex2f(40,30);
                glColor3f(0.60f, 0.60f, 0.60f);
                glVertex2f(-40,30);

            glEnd();

            }


            //?//glPopMatrix();

            SwapBuffers(hDC);

            theta += 1.0f;
            Sleep (1);

    }}

    /* shutdown OpenGL */
    DisableOpenGL(hwnd, hDC, hRC);

    /* destroy the window explicitly */
    DestroyWindow(hwnd);

    return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_CLOSE:
            PostQuitMessage(0);
        break;

        case WM_DESTROY:
            return 0;

        case WM_KEYDOWN:
        {
            switch (wParam)
            {
                case VK_ESCAPE:
                    PostQuitMessage(0);
                case 0x41:

                for (std::vector<block>::iterator it = block_array.begin(); it!=block_array.end(); ++it)
                {
                delete &it;
                }
                std::cout << "pressed a " << block_array.size() << std::endl;
                block_array.clear();

                  //  block_array.clear;//'=block_array[10].y+20.0;
                  //  printf("%f\n",block_array[10].y);
                case 0x51:
                for (int x=0;x<800;x=x+80)
                {
                    for (int y=0;y<600;y=y+80)
                    {
                    block *b= new block(x,y);
                    block_array.push_back(*b);
                    }
                }
                break;
            }
        }
        break;

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

    return 0;
}

void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
{
    PIXELFORMATDESCRIPTOR pfd;

    int iFormat;

    /* get the device context (DC) */
    *hDC = GetDC(hwnd);

    /* set the pixel format for the DC */
    ZeroMemory(&pfd, sizeof(pfd));

    pfd.nSize = sizeof(pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW |
                  PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;

    iFormat = ChoosePixelFormat(*hDC, &pfd);

    SetPixelFormat(*hDC, iFormat, &pfd);

    /* create and enable the render context (RC) */
    *hRC = wglCreateContext(*hDC);

    wglMakeCurrent(*hDC, *hRC);
}

void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(hRC);
    ReleaseDC(hwnd, hDC);
}



it has a memory leak
when I press a I am trying to get rid of all instances of block and clear the vector 'block_array'
strangely the memory footprint (in task manager) goes up by about 4k whenever i press 'a'

when i press 'q' i create more instances of block
when i press 'a' the vector shrinks to a size of 80 but the memory footprint of the program stays the same
closed account (D80DSL3A)
There is no break; for case 0x41 so execution falls through to the 0x51 case where more blocks are allocated.
thanks that made the array go down to zero and all the blocks disapeared off the screen as they should.

The memory footprint (in task manager) still doesnt go back down to its original value though.

If I press 'q' about 10-20 times I can get the memory footprint to go up to 5.5MB and when I press 'a' it stays the same
little bump
You have massive memory leaks, and you are deleting stuff you shouldn't be.

Your vector does not contain pointer, it contains full objects. When you add an object to the vector, the vector contains a COPY of that object.

Since you have full objects in your vector, you do not need to use new/delete at all. You only need to new/delete if you have a vector full of pointers. Since that's not the case here, you don't need to worry about it.

1
2
3
4
                for (std::vector<block>::iterator it = block_array.begin(); it!=block_array.end(); ++it)
                {
                delete &it;
                }


This code is wrong because you're deleting an iterator (not the block). Not only that, but this iterator was not allocated with new, so this is totally broken.

Generally speaking, you don't use the & operator with delete. If you find yourself doing delete &something; then you're probably deleting something you shouldn't be deleting.

In your case, since the vector does not contain pointers, you can get rid of this entire loop.

1
2
3
4
5
6
7
8
                for (int x=0;x<800;x=x+80)
                {
                    for (int y=0;y<600;y=y+80)
                    {
                    block *b= new block(x,y);
                    block_array.push_back(*b);
                    }
                }


This is the source of your giant memory leak.

What you're doing is making a new block with new, then COPYING that block to another block in the vector.

However the block you allocated with new here is NEVER DELETED.

Again, since the vector has the full block and not a pointer, you don't need new at all:

1
2
3
4
5
6
7
 // bad
block *b= new block(x,y);
block_array.push_back(*b);


 // good
block_array.push_back( block(x,y) );
ok I replaced
1
2
block *b= new block(x,y);
block_array.push_back(*b);

with
 
block_array.push_back( block(x,y) )


I got rid of all new and delete commands


the memory footprint is still staying the same when i clear the vector

the memory footprint is still staying the same when i clear the vector


It's possible vector is hanging onto the memory in anticipation of the vector growing again later. The only way to free it for sure would be to destroy the vector (happens automatically when the vector goes out of scope).

To my knowledge, there's no other way to explicitly free the buffer vector has allocated.
Topic archived. No new replies allowed.