Hi
I'm working on a program that uses Gtkmm for the GUI under windows. The program also uses Gtkglextmm for creating a opengl capable window and GLEW for getting a OpenGL 3 context.
Things work well until I try to create a shader. The program creates an ifstream, loads the file, compiles it into a gl program. Then the program calls close() on the ifstream. Then the ifstream variable goes out of scope. When the ifstream variable goes out of scope it crashes in the deconstructor.
I think I've removed most except for what is causing the problem. But first the error message:
Code:
Unhandled exception at 0x0f738a7f (msvcp100d.dll) in FlowerModelerGtkmm.exe: 0xC0000005: Access violation reading location 0x00000004.
And the visual studio 2012 debugger brings me to this line in: istream
Code:
virtual __CLR_OR_THIS_CALL ~basic_istream()
{ // destroy the object
}
And now here is the code that is causing the problem:
Main.cpp
Code:
#include <gl\glew.h>
#include <gtkmm\main.h>
#include <gtkglext\gtkglmm.h>
#include <iostream>
#include "ControlsWindow.h"
#include "OrganEditorWindow.h"
#include "DiagramEditorWindow.h"
#include "ModelViewportWindow.h"
#include "Flower.h"
#include "GTKGLUtil.h"
int main(int argc, char* argv[])
{
Gtk::Main kit(argc, argv);
Gtk::GL::init(argc, argv);
Glib::RefPtr<Gdk::GL::Config> glconfig;
glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB | Gdk::GL::MODE_DEPTH | Gdk::GL::MODE_DOUBLE);
if(!glconfig)
{
std::cout<<"Unable to make a double buffered context"<<std::endl;
glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB | Gdk::GL::MODE_DEPTH);
if(!glconfig)
{
std::cout<<"Unable to create single buffered context."<<std::endl;
std::exit(1);
}
}
GLConfigUtil::examine_gl_attrib(glconfig);
Flower flower;
OrganEditorWindow organEditor(glconfig, flower);
organEditor.show();
if(glewInit() != GLEW_OK)
{
std::cout<<"Failed to initialize glew"<<std::endl;
}
int major, minor;
Gdk::GL::query_version(major, minor);
std::cout<<"Opengl extension version: "<<major<<"."<<minor<<std::endl;
std::cout<<"Opengl Version: "<<glGetString(GL_VERSION)<<std::endl;
Shader *shader = new Shader();
shader->Init("textVertShader.glsl", "textFragShader.glsl"); //<-- code crahses in here
kit.run();
return 0;
}
And Shader.cpp
Code:
bool Shader::Init(std::string vShaderFile, std::string fShaderFile)
{
{
std::ifstream fin;
fin.open(vShaderFile);
if(!fin.is_open())
{
std::cout<<"Unable to open the vertex shader source file: "<<vShaderFile<<std::endl;
return false;
}
{
std::string vShaderSource((std::istreambuf_iterator<char>(fin)), std::istreambuf_iterator<char>());
vShaderId = CompileShader(GL_VERTEX_SHADER, vShaderSource);
checkError("Init->vShader");
}
fin.clear();
fin.close();
}// <-- This is where the program crashes.
{
std::ifstream fin;
fin.open(fShaderFile);
if(!fin.is_open())
{
std::cout<<"Unable to open the fragment shader source file: "<<fShaderFile<<std::endl;
return false;
}
{
std::string fShaderSource((std::istreambuf_iterator<char>(fin)), std::istreambuf_iterator<char>());
this->fShaderId = CompileShader(GL_FRAGMENT_SHADER, fShaderSource);
checkError("Init->fShader");
}
}
{
CreateProgram(this->vShaderId, this->fShaderId);
checkError("Init->CreateProgram");
}
if(this->progId > 0)
{
return true;
}
return false;
}
I've tried playing around with the scope of the ifstream variable and using close and not using close. It always crashes when going out of scope.
I'm at a loss. This code worked before I started converting it to Gtkmm, and currently works in other programs (one using freeglut and another allegro). This is also my first attempt at using Gtkmm/Gtk+.
Any help is appreciated.