Sign in to follow this  

OpenGL Advice re possible problem deleting dynamic memory when using OpenGL

Recommended Posts

davidr    122
Hi, I've together a very simple OpenGL app using GLUT on Linux that draws a few circles and bounces them around the screen. Everything works fine if I neglect to free the memory for the circles vertices using delete [] vertices when I close the app. If I DO do this, the app crashes with the following error message:
*** glibc detected *** ./OpenGL_Demo_3: double free or corruption (!prev): 0x080d01c0 ***
======= Backtrace: =========
======= Memory map: ========
08048000-0804b000 r-xp 00000000 08:11 32344880   /home/david/Development/OpenGL_Demo_3/OpenGL_Demo_3
0804b000-0804c000 rw-p 00002000 08:11 32344880   /home/david/Development/OpenGL_Demo_3/OpenGL_Demo_3
0804c000-08140000 rw-p 0804c000 00:00 0          [heap]
b6000000-b6021000 rw-p b6000000 00:00 0
b6021000-b6100000 ---p b6021000 00:00 0
b6139000-b613a000 rw-s 00000000 00:09 14319624   /SYSV00000000 (deleted)
b613a000-b613b000 rw-s 00000000 00:09 14286855   /SYSV00000000 (deleted)
b613b000-b633b000 rw-s 33a41000 00:0e 18307      /dev/nvidia0
b633b000-b633f000 rw-s 33a95000 00:0e 18307      /dev/nvidia0
b633f000-b643f000 rw-s 33bcb000 00:0e 18307      /dev/nvidia0
b643f000-b6440000 rw-s fdc06000 00:0e 18307      /dev/nvidia0
b6440000-b6480000 rw-s 33a37000 00:0e 18307      /dev/nvidia0
b6480000-b64a0000 rw-s 326a8000 00:0e 18307      /dev/nvidia0
b64a0000-b6507000 rw-p b64a0000 00:00 0
b6507000-b656b000 rw-p 00000000 00:0e 2836       /dev/zero
b656b000-b658e000 rw-p b656b000 00:00 0
b658e000-b65b0000 rw-s 00000000 00:09 0          /SYSV00000000 (deleted)
b65b0000-b6617000 rw-p b65b0000 00:00 0
b6617000-b661b000 r-xp 00000000 08:11 19039582   /usr/lib/
b661b000-b661c000 rw-p 00003000 08:11 19039582   /usr/lib/
b661c000-b661e000 r-xp 00000000 08:11 19039571   /usr/lib/
b661e000-b661f000 rw-p 00001000 08:11 19039571   /usr/lib/
b661f000-b663d000 r-xp 00000000 08:11 19039768   /usr/lib/
b663d000-b663f000 rw-p 0001e000 08:11 19039768   /usr/lib/
b663f000-b6640000 r-xp 00000000 08:11 19645643   /usr/lib/tls/
b6640000-b6641000 rw-p 00000000 08:11 19645643   /usr/lib/tls/
b6641000-b6642000 rw-p b6641000 00:00 0
b6642000-b7117000 r-xp 00000000 08:11 19039010   /usr/lib/
b7117000-b7153000 rwxp 00ad4000 08:11 19039010   /usr/lib/
b7153000-b7158000 rwxp b7153000 00:00 0
b7158000-b71da000 r-xp 00000000 08:11 19039506   /usr/lib/
b71da000-b71db000 rw-p 00081000 08:11 19039506   /usr/lib/
b71db000-b731f000 r-xp 00000000 08:11 22954955   /lib/tls/i686/cmov/
b731f000-b7320000 r--p 00143000 08:11 22954955   /lib/tls/i686/cmov/
b7320000-b7322000 rw-p 00144000 08:11 22954955   /lib/tls/i686/cmov/
b7322000-b7325000 rw-p b7322000 00:00 0
b7325000-b732f000 r-xp 00000000 08:11 22921283   /lib/
b732f000-b7330000 rw-p 0000a000 08:11 22921283   /lib/
b7330000-b7353000 r-xp 00000000 08:11 22954959   /lib/tls/i686/cmov/
b7353000-b7355000 rw-p 00023000 08:11 22954959   /lib/tls/i686/cmov/
b7355000-b743d000 r-xp 00000000 08:11 19040446   /usr/lib/
b743d000-b7440000 r--p 000e8000 08:11 19040446   /usr/lib/
b7440000-b7442000 rw-p 000eb000 08:11 19040446   /usr/lib/
b7442000-b7449000 rw-p b7442000 00:00 0
b7449000-b745d000 r-xp 00000000 08:11 22954969   /lib/tls/i686/cmov/
b745d000-b745f000 rw-p 00013000 08:11 22954969   /lib/tls/i686/cmov/
b745f000-b7461000 rw-p b745f000 00:00 0
b7461000-b7463000 r-xp 00000000 08:11 22954958   /lib/tls/i686/cmov/
b7463000-b7465000 rw-p 00001000 08:11 22954958   /lib/tls/i686/cmov/
b7465000-b746c000 r-xp 00000000 08:11 22954971   /lib/tls/i686/cmov/
b746c000-b746e000 rw-p 00006000 08:11 22954971   /lib/tls/i686/cmov/
b746e000-b7482000 r-xp 00000000 08:11 19040531   /usr/lib/
b7482000-b7483000 rw-p 00013000 08:11 19040531   /usr/lib/
b7483000-b75d6000 r-xp 00000000 08:11 19287450   /usr/local/Trolltech/Qt-4.3.3/lib/
b75d6000-b75dd000 rw-p 00152000 08:11 19287450   /usr/local/Trolltech/Qt-4.3.3/lib/ (core dumped)

At the start of the app, I do something like this:
Vertices = new Vectormath::Aos::Vector3[Vertex_Count];
// Fill Vertices with data
// Run app
// ...
// Cleanup - crash if I DO do this
delete [] Vertices;

I'm using Sony's vector math library, although I don't think its anything to do with the library as I experienced the same problem using my own Vertex structure. Does anybody have any suggestions? I've looked at many of the OpenGL Red Books sample apps, and I can't see that I've neglected anything. In my other programs that don't use OpenGL, creating/deleting dynamic memory works fine e.g. int* list = new int[100]; delete [] list; Thanks,

Share this post

Link to post
Share on other sites
haegarr    7372
From the error dump, "double free" normally occurs if, well, the memory is freed twice. Perhaps the compiler has integrated an automatic variable freing in the scope of the main routine. That points at a wrong type specification of "Variable"?! Or else your own clean-up code before that delete[] already frees the array, but I assume you've already excluded that possibility. Howeer, the snippet
  Vertices = new Vectormath::Aos::Vector3[Vertex_Count];
doesn't show any type of the variable "Vertices". To clarify, please post some more of the actual code.

Share this post

Link to post
Share on other sites
davidr    122

Here is the header file for Circle_Class:

#pragma once
#include <math.h>
#include <GL/gl.h>
#include <QtDebug>
#include <vectormath/scalar/cpp/vectormath_aos.h>
#include "Random_Library.h"
#include "Debug_Library.h"

class Circle_Class
Circle_Class(float in_Radius = 50.0);

float Radius(void) const;

void set_Position(const Vectormath::Aos::Vector3& in_Position);
const Vectormath::Aos::Vector3& Position(void) const;

void set_Direction(const Vectormath::Aos::Vector3& in_Direction);
const Vectormath::Aos::Vector3& Direction(void) const;

void set_Velocity(float in_Units_Per_Second);
float Velocity(void) const;

void Update(float in_Elapsed_Time_Seconds);

void Draw(void) const;
void Random_Direction(Vectormath::Aos::Vector3& out_Direction) const;

float m_Radius;
float m_Velocity;
int m_Vertex_Count;
float m_PI;
float m_Radians_Per_Deg;
Vectormath::Aos::Vector3* m_Vertices;
Vectormath::Aos::Vector3 m_Position;
Vectormath::Aos::Vector3 m_Direction;
GLfloat m_Color[4];

Here is the cpp file for Circle_Class

#include "Circle_Class.h"

Circle_Class::Circle_Class(float in_Radius):
m_Vertices = new Vectormath::Aos::Vector3[m_Vertex_Count];

float l_Angle_Deg = 360.0f / ((float) m_Vertex_Count);
for (int i = 0; i < m_Vertex_Count; ++i)
float q = ((float) i) * l_Angle_Deg;
q *= m_Radians_Per_Deg;

float x = m_Radius * cos(q);
float y = m_Radius * sin(q);



m_Color[0] = ((GLfloat) Random_Integer(0, 255)) / 255.f;
m_Color[1] = ((GLfloat) Random_Integer(0, 255)) / 255.f;
m_Color[2] = ((GLfloat) Random_Integer(0, 255)) / 255.f;
// Set alpha component to 1.0
m_Color[3] = 1.0f;

//Why does this create errors??
delete [] m_Vertices;

float Circle_Class::Radius(void) const
return m_Radius;

void Circle_Class::set_Position(const Vectormath::Aos::Vector3& in_Position)
m_Position = in_Position;

const Vectormath::Aos::Vector3& Circle_Class::Position(void) const
return m_Position;

void Circle_Class::set_Direction(const Vectormath::Aos::Vector3& in_Direction)
m_Direction = in_Direction;
m_Direction = Vectormath::Aos::normalize(m_Direction);

const Vectormath::Aos::Vector3& Circle_Class::Direction(void) const
return m_Direction;

void Circle_Class::set_Velocity(float in_Units_Per_Second)
m_Velocity = in_Units_Per_Second;

float Circle_Class::Velocity(void) const
return m_Velocity;

void Circle_Class::Update(float in_Elapsed_Time_Seconds)
Vectormath::Aos::Vector3 l_Vector_3(m_Direction);
l_Vector_3 *= (m_Velocity * in_Elapsed_Time_Seconds);
m_Position += l_Vector_3;

void Circle_Class::Draw(void) const

glColor3f(m_Color[0], m_Color[1], m_Color[2]);

for (int i = 0; i < m_Vertex_Count; ++i)
float x = m_Vertices[i].getX() + m_Position.getX();
float y = m_Vertices[i].getY() + m_Position.getY();

glVertex2f(x, y);

void Circle_Class::Random_Direction(Vectormath::Aos::Vector3& out_Direction) const
float l_Angle_Deg = Random_Integer(0, 359);
Vectormath::Aos::Matrix3 l_Matrix = Vectormath::Aos::Matrix3::rotationZ(l_Angle_Deg * m_Radians_Per_Deg);
Vectormath::Aos::Vector3 l_Vector_3(1.0f, 0.0f, 0.0f);
l_Vector_3 = l_Matrix * l_Vector_3;
out_Direction = Vectormath::Aos::normalize(l_Vector_3);

So, each Circle_Class has its own set of vertices. The memory is allocated when the Circle_Class is created, and freed when the circle is destroyed.


Share this post

Link to post
Share on other sites
davidr    122
OK - problem solved. I appended several Circles to a QList, and because I neglected to write a copy constructor for the Circle class, each Circles m_Vertices pointer pointed to the same array. Hence, the double free error.

[Edited by - davidr on March 24, 2008 3:35:02 AM]

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats =; gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
  • Popular Now