CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #1
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    130

    [RESOLVED] Segmentation fault after declaring class member.

    If i use the class below (stack allocation) i get a segmentation fault.
    If i remove or move the int a to public then it works file.
    If i keep it under private and heap allocate an instance of this class, i don't get segmentation fault the program is running but its messed up (weird unexpected behavior is happening)!

    Lastly, if i create a new private keyword under public it works file:
    Code:
    private:
    	GLFWwindow *window;
    
    public:
    	unsigned int width, height;
    
    private:
    	int a;
    So i believe it has to do something with the order i declare members inside a class.

    First time after years of programming i encountered something like this. I hope someone of you know what's happening


    Header File:
    Code:
    #ifndef ENGIN_CORE_H
    #define ENGIN_CORE_H
    #include <glad/glad.h>
    #include <GLFW/glfw3.h>
    #include <iostream>
    
    namespace engine
    {
    	class Core
    	{
    		private:
    			GLFWwindow *window;
    
    		private:
    			int a;
    
    		public:
    			unsigned int width, height;
    
    		public:
    			Core(std::string title, unsigned int width, unsigned int height);
    			~Core();
    			void MainLoop();
    	};
    }
    
    #endif

    Implementation:
    Code:
    #include <Core.h>
    #include <Engine.h>
    
    
    
    //On Window Resize.
    void OnWindowResize(GLFWwindow *window, int width, int height);
    
    
    
    //Constructor.
    engine::Core::Core(std::string title, unsigned int width, unsigned int height)
    {
    
    	this->width   = width;
       	this->height = height;
    
    	/* Initialize the library */
        if (!glfwInit())
            throw GLFWerror("GLFW could not initialize.");
        
        //OpenGL 3.3 Core.
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    
        /* Create a windowed mode window and its OpenGL context */
        window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
        if (!window)
        {
            glfwTerminate();
            throw GLFWerror("GLFW could not create a window");
        }
    
    
        //Set GLFW user pointer.
    	glfwSetWindowUserPointer(window, (void *)this);
    
        /* Make the window's context current */
        glfwMakeContextCurrent(window);
    
        //Initialize glad.
        if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
        {
        	glfwTerminate();
            throw GLADerror("GLAD could not initialize.");
        }
    
        //Set the viewport.
        GLCall(glViewport(0, 0, this->width, this->height));
    
        //Enable depth testing.
        GLCall(glEnable(GL_DEPTH_TEST));
    
        //On Window Resize.
        glfwSetFramebufferSizeCallback(window, OnWindowResize);
    }
    
    
    
    //Deconstructor.
    engine::Core::~Core()
    {
    
    }
    
    
    
    //Main Loop.
    void engine::Core::MainLoop()
    {
    
        Renderer *renderer = new Renderer();
        Shader *program = new Shader("Shaders/basic_vs.glsl", "Shaders/basic_fs.glsl");
        Texture *texture = new Texture("Resources/Images/container.png", true);
    
            /* Loop until the user closes the window */
            while (!glfwWindowShouldClose(window))
            {
                /* Render here */
                GLCall(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
    
                renderer->RenderArrays(program, texture, this);
    
                /*
                if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
                {
                    if (!this->full_screen)
                    {
                        glfwSetWindowMonitor(window, glfwGetPrimaryMonitor(), 0, 0, width, height, 60);
                        this->full_screen = true;
                    }
    
                    else
                    {
                        glfwSetWindowMonitor(window, NULL, 0, 0, width, height, 60);
                        this->full_screen = false;
                    }
                }*/
    
                /* Swap front and back buffers */
                glfwSwapBuffers(window);
    
                /* Poll for and process events */
                glfwPollEvents();
            }
    
    
            delete renderer;
            delete program;
            delete texture;
    
    
            //Terminate GLFW.
            glfwTerminate();
    }
    
    
    
    
    //On Window Resize.
    void OnWindowResize(GLFWwindow *window, int width, int height)
    {
    	engine::Core *core = (engine::Core *)glfwGetWindowUserPointer(window);
    
    	core->width = width;
    	core->height = height;
    
    	GLCall(glViewport(0, 0, width, height));
    }
    Last edited by babaliaris; July 18th, 2018 at 12:51 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured