Like Tree3Likes
  • 3 Post By Adamkromm

Runtime crash on ifstream destructor.

This is a discussion on Runtime crash on ifstream destructor. within the C++ Programming forums, part of the General Programming Boards category; Hi I'm working on a program that uses Gtkmm for the GUI under windows. The program also uses Gtkglextmm for ...

  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,139
    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
    21,772
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,139
    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).
    Salem, laserlight and Codeplug like this.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21