• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Jens Eckervogt
      Hello guys, 
      Please tell me! 
      How do I know? Why does wavefront not show for me?
      I already checked I have non errors yet.
      using OpenTK; using System; using System.Collections.Generic; using System.IO; namespace Tutorial_08.net.sourceskyboxer { public class WaveFrontLoader { private static List<Vector3> vertices; private static List<Vector2> textures; private static List<Vector3> normals; private static List<int> indices; private static float[] verticesArray; private static float[] normalsArray; private static float[] texturesArray; private static int[] indicesArray; private static string[] lines; public static RawModel LoadObjModel(string filename, Loader loader) { if (!File.Exists("Contents/" + filename + ".obj")) { throw new FileNotFoundException("Error: wavefront file doesn't exist path: " + filename + ".png"); } vertices = new List<Vector3>(); textures = new List<Vector2>(); normals = new List<Vector3>(); indices = new List<int>(); lines = File.ReadAllLines("Contents/" + filename + ".obj"); try { foreach (string line in lines) { if (line == "" || line.StartsWith("#")) continue; string[] token = line.Split(' '); switch(token[0]) { case ("o"): string o = token[1]; break; case "v": Vector3 vertex = new Vector3(float.Parse(token[1]), float.Parse(token[2]), float.Parse(token[3])); vertices.Add(vertex); break; case "vn": Vector3 normal = new Vector3(float.Parse(token[1]), float.Parse(token[2]), float.Parse(token[3])); normals.Add(normal); break; case "vt": Vector2 texture = new Vector2(float.Parse(token[1]), float.Parse(token[2])); textures.Add(texture); break; case "f": texturesArray = new float[vertices.Count * 2]; normalsArray = new float[vertices.Count * 3]; verticesArray = new float[vertices.Count * 3]; indicesArray = new int[indices.Count]; int vertexPointer = 0; foreach (Vector3 vex in vertices) { verticesArray[vertexPointer++] = vex.X; verticesArray[vertexPointer++] = vex.Y; verticesArray[vertexPointer++] = vex.Z; } for (int i = 0; i < indices.Count; i++) { indicesArray[i] = indices[i]; } break; } } } catch (FileNotFoundException f) { throw new FileNotFoundException($"OBJ file not found: {f.FileName}", f); } catch (ArgumentException ae) { throw new ArgumentException("OBJ file is damaged", ae); } return loader.loadToVAO(verticesArray, texturesArray, indicesArray); } } } And It have tried other method but it can't show for me.  I am mad now. Because any OpenTK developers won't help me.
      Please help me how do I fix.

      And my download (mega.nz) should it is original but I tried no success...
      - Add blend source and png file here I have tried tried,.....  
      PS: Why is our community not active? I wait very longer. Stop to lie me!
      Thanks !
    • By codelyoko373
      I wasn't sure if this would be the right place for a topic like this so sorry if it isn't.
      I'm currently working on a project for Uni using FreeGLUT to make a simple solar system simulation. I've got to the point where I've implemented all the planets and have used a Scene Graph to link them all together. The issue I'm having with now though is basically the planets and moons orbit correctly at their own orbit speeds.
      I'm not really experienced with using matrices for stuff like this so It's likely why I can't figure out how exactly to get it working. This is where I'm applying the transformation matrices, as well as pushing and popping them. This is within the Render function that every planet including the sun and moons will have and run.
      if (tag != "Sun") { glRotatef(orbitAngle, orbitRotation.X, orbitRotation.Y, orbitRotation.Z); } glPushMatrix(); glTranslatef(position.X, position.Y, position.Z); glRotatef(rotationAngle, rotation.X, rotation.Y, rotation.Z); glScalef(scale.X, scale.Y, scale.Z); glDrawElements(GL_TRIANGLES, mesh->indiceCount, GL_UNSIGNED_SHORT, mesh->indices); if (tag != "Sun") { glPopMatrix(); } The "If(tag != "Sun")" parts are my attempts are getting the planets to orbit correctly though it likely isn't the way I'm meant to be doing it. So I was wondering if someone would be able to help me? As I really don't have an idea on what I would do to get it working. Using the if statement is truthfully the closest I've got to it working but there are still weird effects like the planets orbiting faster then they should depending on the number of planets actually be updated/rendered.
    • By Jens Eckervogt
      Hello everyone, 
      I have problem with texture
      using System; using OpenTK; using OpenTK.Input; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL4; using System.Drawing; using System.Reflection; namespace Tutorial_05 { class Game : GameWindow { private static int WIDTH = 1200; private static int HEIGHT = 720; private static KeyboardState keyState; private int vaoID; private int vboID; private int iboID; private Vector3[] vertices = { new Vector3(-0.5f, 0.5f, 0.0f), // V0 new Vector3(-0.5f, -0.5f, 0.0f), // V1 new Vector3(0.5f, -0.5f, 0.0f), // V2 new Vector3(0.5f, 0.5f, 0.0f) // V3 }; private Vector2[] texcoords = { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) }; private int[] indices = { 0, 1, 3, 3, 1, 2 }; private string vertsrc = @"#version 450 core in vec3 position; in vec2 textureCoords; out vec2 pass_textureCoords; void main(void) { gl_Position = vec4(position, 1.0); pass_textureCoords = textureCoords; }"; private string fragsrc = @"#version 450 core in vec2 pass_textureCoords; out vec4 out_color; uniform sampler2D textureSampler; void main(void) { out_color = texture(textureSampler, pass_textureCoords); }"; private int programID; private int vertexShaderID; private int fragmentShaderID; private int textureID; private Bitmap texsrc; public Game() : base(WIDTH, HEIGHT, GraphicsMode.Default, "Tutorial 05 - Texturing", GameWindowFlags.Default, DisplayDevice.Default, 4, 5, GraphicsContextFlags.Default) { } protected override void OnLoad(EventArgs e) { base.OnLoad(e); CursorVisible = true; GL.GenVertexArrays(1, out vaoID); GL.BindVertexArray(vaoID); GL.GenBuffers(1, out vboID); GL.BindBuffer(BufferTarget.ArrayBuffer, vboID); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * Vector3.SizeInBytes), vertices, BufferUsageHint.StaticDraw); GL.GenBuffers(1, out iboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, iboID); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(int)), indices, BufferUsageHint.StaticDraw); vertexShaderID = GL.CreateShader(ShaderType.VertexShader); GL.ShaderSource(vertexShaderID, vertsrc); GL.CompileShader(vertexShaderID); fragmentShaderID = GL.CreateShader(ShaderType.FragmentShader); GL.ShaderSource(fragmentShaderID, fragsrc); GL.CompileShader(fragmentShaderID); programID = GL.CreateProgram(); GL.AttachShader(programID, vertexShaderID); GL.AttachShader(programID, fragmentShaderID); GL.LinkProgram(programID); // Loading texture from embedded resource texsrc = new Bitmap(Assembly.GetEntryAssembly().GetManifestResourceStream("Tutorial_05.example.png")); textureID = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, textureID); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, texsrc.Width, texsrc.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); System.Drawing.Imaging.BitmapData bitmap_data = texsrc.LockBits(new Rectangle(0, 0, texsrc.Width, texsrc.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, texsrc.Width, texsrc.Height, PixelFormat.Bgra, PixelType.UnsignedByte, bitmap_data.Scan0); texsrc.UnlockBits(bitmap_data); GL.Enable(EnableCap.Texture2D); GL.BufferData(BufferTarget.TextureBuffer, (IntPtr)(texcoords.Length * Vector2.SizeInBytes), texcoords, BufferUsageHint.StaticDraw); GL.BindAttribLocation(programID, 0, "position"); GL.BindAttribLocation(programID, 1, "textureCoords"); } protected override void OnResize(EventArgs e) { base.OnResize(e); GL.Viewport(0, 0, ClientRectangle.Width, ClientRectangle.Height); } protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); keyState = Keyboard.GetState(); if (keyState.IsKeyDown(Key.Escape)) { Exit(); } } protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); // Prepare for background GL.Clear(ClearBufferMask.ColorBufferBit); GL.ClearColor(Color4.Red); // Draw traingles GL.EnableVertexAttribArray(0); GL.EnableVertexAttribArray(1); GL.BindVertexArray(vaoID); GL.UseProgram(programID); GL.BindBuffer(BufferTarget.ArrayBuffer, vboID); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture3D, textureID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, iboID); GL.DrawElements(BeginMode.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0); GL.DisableVertexAttribArray(0); GL.DisableVertexAttribArray(1); SwapBuffers(); } protected override void OnClosed(EventArgs e) { base.OnClosed(e); GL.DeleteVertexArray(vaoID); GL.DeleteBuffer(vboID); } } } I can not remember where do I add GL.Uniform2();
    • By Jens Eckervogt
      Hello everyone
      For @80bserver8 nice job - I have found Google search. How did you port from Javascript WebGL to C# OpenTK.?
      I have been searched Google but it shows f***ing Unity 3D. I really want know how do I understand I want start with OpenTK But I want know where is porting of Javascript and C#?
    • By mike44
      I draw in a OpenGL framebuffer. All is fine but it eats FPS (frames per second), hence I wonder if I could execute the framebuffer drawing only every 5-10th loop or so?
      Many thanks
  • Advertisement
  • Advertisement
Sign in to follow this  

OpenGL Advice re possible problem deleting dynamic memory when using OpenGL

This topic is 3678 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: =========
======= 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.
b7482000-b7483000 rw-p 00013000 08:11 19040531   /usr/lib/libz.so.
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
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

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.getX() + m_Position.getX();
float y = m_Vertices.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
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