Jump to content
  • Advertisement
Sign in to follow this  
davidr

OpenGL Advice re possible problem deleting dynamic memory when using OpenGL

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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
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.setX(x);
m_Vertices.setY(y);
m_Vertices.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.getX() + m_Position.getX();
float y = m_Vertices.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 this post


Link to post
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!