计算机信息:
我是使用C ++进行编程的初学者,两个月来我一直在写一个脚本来使用此算法生成2D随机纹理:
http://web.archive.org/web/20080228054410/http://www.davebollinger.com/works/pixelspaceships/
我使用的是OpenGL 3(带有GLEW和GLFW 3)来显示结果,Magick ++用来构建PNG纹理,而FastNoise则是在每次执行程序时使结果不同。
有了Valgrind,我遇到了这两次内存泄漏(以及许多其他非常相似的泄漏):
==2525== 10 bytes in 1 blocks are still reachable in loss record 11 of 1,389
==2525== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2525== by 0x707E489: strdup (strdup.c:42)
==2525== by 0x8026186: ??? (in /usr/lib/x86_64-linux-gnu/libglapi.so.0.0.0)
==2525== by 0x8025F4D: _glapi_get_proc_address (in /usr/lib/x86_64-linux-gnu/libglapi.so.0.0.0)
==2525== by 0x50D8982: glXGetProcAddress (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x4E88FDC: glewInit (in /usr/lib/x86_64-linux-gnu/libGLEW.so.1.13.0)
==2525== by 0x426809: MyWindow::CreateContext(char*) (MyWindow.cpp:40)
==2525== by 0x4266C7: MyWindow::MyWindow(char*) (MyWindow.cpp:5)
==2525== by 0x428AF5: main (main.cpp:11)
==2525== 24 bytes in 1 blocks are still reachable in loss record 200 of 1,389
==2525== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2525== by 0x10956743: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x109FE1DA: nouveau_drm_screen_create (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x105654E5: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x108C2AC0: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x108BDFDF: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x5104232: ??? (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x50DBD03: ??? (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x50D7240: glXQueryVersion (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x445C54: _glfwInitGLX (in /home/pablo/WorkSpacePerso/CppProjet/Infinite_loop_OpenGL/bin/main)
==2525== by 0x441B3F: _glfwPlatformCreateWindow (in /home/pablo/WorkSpacePerso/CppProjet/Infinite_loop_OpenGL/bin/main)
==2525== by 0x438C14: glfwCreateWindow (in /home/pablo/WorkSpacePerso/CppProjet/Infinite_loop_OpenGL/bin/main)
MyWindow.cpp:
#include "MyWindow.h"
MyWindow::MyWindow(char* argv){
CreateContext(argv);
}
void MyWindow::CreateContext(char* argv){
Magick::InitializeMagick(argv);
glfwInit();
//set OpenGl version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
//window non resizable
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
//get screen size
const GLFWvidmode* screen = glfwGetVideoMode(glfwGetPrimaryMonitor());
m_Width = screen->width;
m_Height = screen->height;
//create window
m_Window = glfwCreateWindow(m_Width, m_Height, "My Window", nullptr, nullptr);
//hide cursor
glfwSetInputMode(m_Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
//Context
glfwMakeContextCurrent(m_Window);
glewExperimental = GL_TRUE;
glewInit();
//Allow transparency for textures
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
MyWindow::~MyWindow(){
Magick::TerminateMagick();
glfwDestroyWindow(m_Window);
glfwTerminate();
}
MyWindow.h:
#ifndef GLEW_STATIC
#define GLEW_STATIC
#endif
#ifndef MyWindow_H_Included
#define MyWindow_H_Included
//Include
#include <GL/glew.h>
#include <GLFW/glfw3.h>
class MyWindow {
private:
GLFWwindow* m_Window;
unsigned int m_Width;
unsigned int m_Height;
void CreateContext(char* argv);
public:
MyWindow(char* argv);
~MyWindow();
};
#endif
编译:
g++ -I./lib/FastNoise -Wall -g -std=c++11 -I./include `Magick++-config --cxxflags --cppflags --ldflags --libs` -lGLEW -lglfw3 -lGL -lsfml-system -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXinerama -lX11 -lXcursor -c -o obj/MyWindow.o src/MyWindow.cpp
我知道这只是“仍然可以到达”的内存泄漏,这不是我可以遇到的更大问题,但是在此链接上,作者说释放它们“通常是合理的”: http://valgrind.org/docs/manual/faq.html#faq.deflost
我不知道自己在MyWindow.cpp中做错了什么,以便glewInit()和glfwInit()导致这些错误。它们可能是由以下错误生成的:
==2525== ERROR SUMMARY: 6 errors from 2 contexts (suppressed: 0 from 0)
==2525==
==2525== 1 errors in context 1 of 2:
==2525== Syscall param ioctl(generic) points to uninitialised byte(s)
==2525== at 0x70EFF47: ioctl (syscall-template.S:84)
==2525== by 0x90A3477: drmIoctl (in /usr/lib/x86_64-linux-gnu/libdrm.so.2.4.0)
==2525== by 0x90A624E: drmCommandWriteRead (in /usr/lib/x86_64-linux-gnu/libdrm.so.2.4.0)
==2525== by 0x11578DA0: nouveau_object_mthd (in /usr/lib/x86_64-linux-gnu/libdrm_nouveau.so.2.0.0)
==2525== by 0x11579535: nouveau_device_new (in /usr/lib/x86_64-linux-gnu/libdrm_nouveau.so.2.0.0)
==2525== by 0x109FE120: nouveau_drm_screen_create (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x105654E5: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x108C2AC0: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x108BDFDF: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x5104232: ??? (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x50DBD03: ??? (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x50D7240: glXQueryVersion (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== Address 0x102ef182 is 2 bytes inside a block of size 136 alloc'd
==2525== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2525== by 0x11578D65: nouveau_object_mthd (in /usr/lib/x86_64-linux-gnu/libdrm_nouveau.so.2.0.0)
==2525== by 0x11579535: nouveau_device_new (in /usr/lib/x86_64-linux-gnu/libdrm_nouveau.so.2.0.0)
==2525== by 0x109FE120: nouveau_drm_screen_create (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x105654E5: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x108C2AC0: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x108BDFDF: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x5104232: ??? (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x50DBD03: ??? (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x50D7240: glXQueryVersion (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x445C54: _glfwInitGLX (in /home/pablo/WorkSpacePerso/CppProjet/Infinite_loop_OpenGL/bin/main)
==2525== by 0x441B3F: _glfwPlatformCreateWindow (in /home/pablo/WorkSpacePerso/CppProjet/Infinite_loop_OpenGL/bin/main)
==2525== Uninitialised value was created by a heap allocation
==2525== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2525== by 0x11578D65: nouveau_object_mthd (in /usr/lib/x86_64-linux-gnu/libdrm_nouveau.so.2.0.0)
==2525== by 0x11579535: nouveau_device_new (in /usr/lib/x86_64-linux-gnu/libdrm_nouveau.so.2.0.0)
==2525== by 0x109FE120: nouveau_drm_screen_create (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x105654E5: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x108C2AC0: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x108BDFDF: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x5104232: ??? (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x50DBD03: ??? (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x50D7240: glXQueryVersion (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x445C54: _glfwInitGLX (in /home/pablo/WorkSpacePerso/CppProjet/Infinite_loop_OpenGL/bin/main)
==2525== by 0x441B3F: _glfwPlatformCreateWindow (in /home/pablo/WorkSpacePerso/CppProjet/Infinite_loop_OpenGL/bin/main)
==2525==
==2525== 5 errors in context 2 of 2:
==2525== Syscall param ioctl(generic) points to uninitialised byte(s)
==2525== at 0x70EFF47: ioctl (syscall-template.S:84)
==2525== by 0x90A3477: drmIoctl (in /usr/lib/x86_64-linux-gnu/libdrm.so.2.4.0)
==2525== by 0x90A624E: drmCommandWriteRead (in /usr/lib/x86_64-linux-gnu/libdrm.so.2.4.0)
==2525== by 0x11578A88: ??? (in /usr/lib/x86_64-linux-gnu/libdrm_nouveau.so.2.0.0)
==2525== by 0x1157951A: nouveau_device_new (in /usr/lib/x86_64-linux-gnu/libdrm_nouveau.so.2.0.0)
==2525== by 0x109FE120: nouveau_drm_screen_create (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x105654E5: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x108C2AC0: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x108BDFDF: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x5104232: ??? (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x50DBD03: ??? (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x50D7240: glXQueryVersion (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== Address 0x102ef0f2 is 2 bytes inside a block of size 72 alloc'd
==2525== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2525== by 0x11578A33: ??? (in /usr/lib/x86_64-linux-gnu/libdrm_nouveau.so.2.0.0)
==2525== by 0x1157951A: nouveau_device_new (in /usr/lib/x86_64-linux-gnu/libdrm_nouveau.so.2.0.0)
==2525== by 0x109FE120: nouveau_drm_screen_create (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x105654E5: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x108C2AC0: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x108BDFDF: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x5104232: ??? (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x50DBD03: ??? (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x50D7240: glXQueryVersion (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x445C54: _glfwInitGLX (in /home/pablo/WorkSpacePerso/CppProjet/Infinite_loop_OpenGL/bin/main)
==2525== by 0x441B3F: _glfwPlatformCreateWindow (in /home/pablo/WorkSpacePerso/CppProjet/Infinite_loop_OpenGL/bin/main)
==2525== Uninitialised value was created by a heap allocation
==2525== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2525== by 0x11578A33: ??? (in /usr/lib/x86_64-linux-gnu/libdrm_nouveau.so.2.0.0)
==2525== by 0x1157951A: nouveau_device_new (in /usr/lib/x86_64-linux-gnu/libdrm_nouveau.so.2.0.0)
==2525== by 0x109FE120: nouveau_drm_screen_create (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x105654E5: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x108C2AC0: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x108BDFDF: ??? (in /usr/lib/x86_64-linux-gnu/dri/nouveau_dri.so)
==2525== by 0x5104232: ??? (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x50DBD03: ??? (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x50D7240: glXQueryVersion (in /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0)
==2525== by 0x445C54: _glfwInitGLX (in /home/pablo/WorkSpacePerso/CppProjet/Infinite_loop_OpenGL/bin/main)
==2525== by 0x441B3F: _glfwPlatformCreateWindow (in /home/pablo/WorkSpacePerso/CppProjet/Infinite_loop_OpenGL/bin/main)
或者这是其他东西吗?
如果需要更多详细信息,可以找到命令的结果: “ valgrind -v --show-leak-kinds = all --leak-check = full --track-origins =是bin / main” 此处: