# OpenGL Advice re possible problem deleting dynamic memory when using OpenGL

## Recommended Posts

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: =========
/lib/tls/i686/cmov/libc.so.6[0xb7244d65]
/lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb7248800]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb740cd81]
/usr/lib/libstdc++.so.6(_ZdaPv+0x1d)[0xb740cddd]
./OpenGL_Demo_3[0x804979a]
/usr/lib/libglut.so.3(glutMainLoop+0x9f)[0xb7e907ff]
./OpenGL_Demo_3[0x80495b0]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb71f1050]
./OpenGL_Demo_3(__gxx_personality_v0+0xdd)[0x8049111]
======= 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/libXdmcp.so.6.0.0
b661b000-b661c000 rw-p 00003000 08:11 19039582   /usr/lib/libXdmcp.so.6.0.0
b661c000-b661e000 r-xp 00000000 08:11 19039571   /usr/lib/libXau.so.6.0.0
b661e000-b661f000 rw-p 00001000 08:11 19039571   /usr/lib/libXau.so.6.0.0
b661f000-b663d000 r-xp 00000000 08:11 19039768   /usr/lib/libexpat.so.1.0.0
b663d000-b663f000 rw-p 0001e000 08:11 19039768   /usr/lib/libexpat.so.1.0.0
b663f000-b6640000 r-xp 00000000 08:11 19645643   /usr/lib/tls/libnvidia-tls.so.169.09
b6640000-b6641000 rw-p 00000000 08:11 19645643   /usr/lib/tls/libnvidia-tls.so.169.09
b6641000-b6642000 rw-p b6641000 00:00 0
b6642000-b7117000 r-xp 00000000 08:11 19039010   /usr/lib/libGLcore.so.169.09
b7117000-b7153000 rwxp 00ad4000 08:11 19039010   /usr/lib/libGLcore.so.169.09
b7153000-b7158000 rwxp b7153000 00:00 0
b7158000-b71da000 r-xp 00000000 08:11 19039506   /usr/lib/libGLU.so.1.3.070001
b71da000-b71db000 rw-p 00081000 08:11 19039506   /usr/lib/libGLU.so.1.3.070001
b71db000-b731f000 r-xp 00000000 08:11 22954955   /lib/tls/i686/cmov/libc-2.6.1.so
b731f000-b7320000 r--p 00143000 08:11 22954955   /lib/tls/i686/cmov/libc-2.6.1.so
b7320000-b7322000 rw-p 00144000 08:11 22954955   /lib/tls/i686/cmov/libc-2.6.1.so
b7322000-b7325000 rw-p b7322000 00:00 0
b7325000-b732f000 r-xp 00000000 08:11 22921283   /lib/libgcc_s.so.1
b732f000-b7330000 rw-p 0000a000 08:11 22921283   /lib/libgcc_s.so.1
b7330000-b7353000 r-xp 00000000 08:11 22954959   /lib/tls/i686/cmov/libm-2.6.1.so
b7353000-b7355000 rw-p 00023000 08:11 22954959   /lib/tls/i686/cmov/libm-2.6.1.so
b7355000-b743d000 r-xp 00000000 08:11 19040446   /usr/lib/libstdc++.so.6.0.9
b743d000-b7440000 r--p 000e8000 08:11 19040446   /usr/lib/libstdc++.so.6.0.9
b7440000-b7442000 rw-p 000eb000 08:11 19040446   /usr/lib/libstdc++.so.6.0.9
b7442000-b7449000 rw-p b7442000 00:00 0
b7449000-b745d000 r-xp 00000000 08:11 22954969   /lib/tls/i686/cmov/libpthread-2.6.1.so
b745d000-b745f000 rw-p 00013000 08:11 22954969   /lib/tls/i686/cmov/libpthread-2.6.1.so
b745f000-b7461000 rw-p b745f000 00:00 0
b7461000-b7463000 r-xp 00000000 08:11 22954958   /lib/tls/i686/cmov/libdl-2.6.1.so
b7463000-b7465000 rw-p 00001000 08:11 22954958   /lib/tls/i686/cmov/libdl-2.6.1.so
b7465000-b746c000 r-xp 00000000 08:11 22954971   /lib/tls/i686/cmov/librt-2.6.1.so
b746c000-b746e000 rw-p 00006000 08:11 22954971   /lib/tls/i686/cmov/librt-2.6.1.so
b746e000-b7482000 r-xp 00000000 08:11 19040531   /usr/lib/libz.so.1.2.3.3
b7482000-b7483000 rw-p 00013000 08:11 19040531   /usr/lib/libz.so.1.2.3.3
b7483000-b75d6000 r-xp 00000000 08:11 19287450   /usr/local/Trolltech/Qt-4.3.3/lib/libQtCore.so.4.3.3
b75d6000-b75dd000 rw-p 00152000 08:11 19287450   /usr/local/Trolltech/Qt-4.3.3/lib/libQtCore.so.4.3Aborted (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 on other sites
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 on other sites
Hi,

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{public:	Circle_Class(float in_Radius = 50.0);	~Circle_Class(void);	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;private:	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_Radius(in_Radius),	m_Velocity(0.0f),	m_Vertex_Count(128),	m_PI(3.141593f),	m_Radians_Per_Deg(m_PI/180.0f),	m_Vertices(0){	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_Vertices[i].setX(x);		m_Vertices[i].setY(y);		m_Vertices[i].setZ(0.0);	}	Random_Direction(m_Direction);	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;}Circle_Class::~Circle_Class(void){	//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]);		glBegin(GL_POLYGON);	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);	}	glEnd();}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.

Thanks

##### Share on other sites
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]

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627767
• Total Posts
2978989
• ### Similar Content

• Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
So, here's what the plan is so far as far as loading goes:
Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!

• I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks

• A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

-What I'm using:
C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.
-Questions
Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.

• 11
• 10
• 10
• 23
• 14