Thread: Runtime crash on ifstream destructor.

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    40

    Runtime crash on ifstream destructor.

    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.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    What happens if you remove all that reading and just open and close the file? Does that still crash?

    I wasn't aware that ifstream.open takes an std::string as parameter. Is that some kind of implicit conversion or do you use a standard library that has it implemented?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nvoigt
    I wasn't aware that ifstream.open takes an std::string as parameter. Is that some kind of implicit conversion or do you use a standard library that has it implemented?
    C++11 change.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Oh, ok, cool
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    40
    Hi

    Thanks for the replies. I found the problem: it is a visual studio 2010 compiler bug. I had set it to use the 2010 compiler since some of the libraries I was using were compiled with 2010. Here is a link explaining the problem Stuff: Visual C++ 2010 std::istringstream crash in destructor. By switching to visual studio 2012 and recompiling the dependent libraries it fixed the problem (for me at least).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does __attribute__ ((destructor)) run on crash?
    By Alexander Edgar in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2012, 11:25 PM
  2. Destructor crash
    By Abbygail in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2009, 07:31 PM
  3. Crash in destructor
    By vaibhavs17 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2009, 11:10 AM
  4. Runtime formation and execution at runtime
    By Soham in forum C Programming
    Replies: 17
    Last Post: 08-27-2008, 08:45 AM
  5. destructor
    By bharathbangera in forum C++ Programming
    Replies: 5
    Last Post: 05-18-2003, 12:31 PM