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

Thread: [RESOLVED] Segmentation fault after declaring class member.

  1. #1
    Join Date
    Jul 2017
    Posts
    15

    [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.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,195

    Re: Segmentation fault after declaring class member.

    From Wikipedia:
    In computing, a segmentation fault (often shortened to segfault) or access violation is a fault, or failure condition, raised by hardware with memory protection, notifying an operating system (OS) the software has attempted to access a restricted area of memory (a memory access violation).
    Did you try to debug to find out the place where this "Segmentation fault" occurs and by what conditions?
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2017
    Posts
    15

    Re: Segmentation fault after declaring class member.

    Well I know what a segfault is. No I did not debugged it, but still I can clearly see that the problem comes from the order I'm declaring the variables. I will try to debug it though using gdb cause my computer is to slow to run an ide.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,195

    Re: Segmentation fault after declaring class member.

    Quote Originally Posted by babaliaris View Post
    ... I can clearly see that the problem comes from the order I'm declaring the variables.
    It usually means you are trying to access something being outside the borders of memory you may use.
    Victor Nijegorodov

  5. #5
    Join Date
    Jul 2017
    Posts
    15

    Re: Segmentation fault after declaring class member.

    Lol i found the problem and you won't believe me! I don't even know what it was causing it. It was the compilation. I'm using Make to compile my program. I did a make clean to remove all the object files that was generating my executable and the i compiled the program from scratch and now it works! Maybe there is something wrong with my makefile, and it wasn't updating some of the objects and it was linking with old ones. Do you have any experience with that? Can you take a look at my makefile and see if there is something wrong?

    Code:
    #HOW TO ADD YOUR OWN SOURCES:
    #----------------------------------------------------
    # see comment 1)
    
    
    
    #Executable name (change this to whatever you want).
    NAME     = engine
    
    #Some other macros.
    CC                         = g++
    CFLAGS                = -Wall -g
    OBJECT_DIR         = objects/
    SRC_DIR               = src/
    HEADERS_DIR     = headers/
    GLAD_DIR            = Libraries/glad/
    STB_IMAGE_DIR  = Libraries/stb_image/
    
    #Libs to link with.
    LIBS = -lglfw -lGL -ldl
    
    #Include directories.
    INCLUDE = -ILibraries/GLFW/include -I$(GLAD_DIR)include -ILibraries/GLM -ILibraries -I$(HEADERS_DIR)
    
    #Object files.
    OBJECTS = $(OBJECT_DIR)main.o $(OBJECT_DIR)glad.o $(OBJECT_DIR)stb_image.o $(OBJECT_DIR)GLCall.o\
    				    $(OBJECT_DIR)Shader.o $(OBJECT_DIR)Exceptions.o $(OBJECT_DIR)VertexArray.o $(OBJECT_DIR)VertexBuffer.o\
    					$(OBJECT_DIR)VertexAttributes.o  $(OBJECT_DIR)IndexBuffer.o $(OBJECT_DIR)Core.o $(OBJECT_DIR)Renderer.o\
    					$(OBJECT_DIR)Texture.o
    
    
    
    #ALL
    all: $(NAME)
    	$(info Done building $(NAME)!)
    
    
    #Generate the Executable.
    $(NAME):  $(OBJECTS)
    	$(info Building the executable) @$(CC) -o $(NAME) $(OBJECTS) $(LIBS)
    
    
    
    # 1) To compile your own files copy and paste the main.cpp (two lines) and replace
    #      main with your source file name. MAKE SURE you put .c or .cpp files under src/
    #                                                                  and .h files under headers/
    
    # Example:
    #-------------
    #$(OBJECT_DIR)my_src.o:  $(SRC_DIR)my_src.cpp
    #	$(CC) $(CFLAGS) $(INCLUDE) -c $(SRC_DIR)my_src.cpp -o $(OBJECT_DIR)my_src.o
    
    #AFTER THAT copy and paste $(OBJECT_DIR)my_src.o into the OBJECTS macro.
    
    #==========================Compilation Goes Here==========================#
    
    #Compile main.cpp
    $(OBJECT_DIR)main.o:  $(SRC_DIR)main.cpp
    	$(info Compiling main.cpp) @$(CC) $(CFLAGS) $(INCLUDE) -c $(SRC_DIR)main.cpp -o $(OBJECT_DIR)main.o
    
    
    #Compile glad.c
    $(OBJECT_DIR)glad.o:  $(GLAD_DIR)src/glad.c
    	$(info Compiling glad.c) @$(CC) $(CFLAGS) $(INCLUDE) -c $(GLAD_DIR)src/glad.c -o $(OBJECT_DIR)glad.o
    
    
    #Compile stb_image.c
    $(OBJECT_DIR)stb_image.o: $(STB_IMAGE_DIR)stb_image.c $(STB_IMAGE_DIR)stb_image.h
    	$(info Compiling stb_image.c) @$(CC) $(CFLAGS) $(INCLUDE) -c $(STB_IMAGE_DIR)stb_image.c -o $(OBJECT_DIR)stb_image.o
    
    
    #Compile GLCall.cpp
    $(OBJECT_DIR)GLCall.o:  $(SRC_DIR)GLCall.cpp $(HEADERS_DIR)GLCall.h
    	$(info Compiling GLCall.cpp) @$(CC) $(CFLAGS) $(INCLUDE) -c $(SRC_DIR)GLCall.cpp -o $(OBJECT_DIR)GLCall.o
    
    
    #Compile Shader.cpp
    $(OBJECT_DIR)Shader.o:  $(SRC_DIR)Shader.cpp $(HEADERS_DIR)Shader.h
    	$(info Compiling Shader.cpp) @$(CC) $(CFLAGS) $(INCLUDE) -c $(SRC_DIR)Shader.cpp -o $(OBJECT_DIR)Shader.o
    
    
    #Compile Exceptions.cpp
    $(OBJECT_DIR)Exceptions.o:  $(SRC_DIR)Exceptions.cpp $(HEADERS_DIR)Exceptions.h
    	$(info Compiling Exceptions.cpp) @$(CC) $(CFLAGS) $(INCLUDE) -c $(SRC_DIR)Exceptions.cpp -o $(OBJECT_DIR)Exceptions.o
    
    
    #Compile VertexArray.cpp
    $(OBJECT_DIR)VertexArray.o:  $(SRC_DIR)VertexArray.cpp $(HEADERS_DIR)VertexArray.h
    	$(info Compiling VertexArray.cpp) @$(CC) $(CFLAGS) $(INCLUDE) -c $(SRC_DIR)VertexArray.cpp -o $(OBJECT_DIR)VertexArray.o
    
    
    #Compile VertexBuffer.cpp
    $(OBJECT_DIR)VertexBuffer.o:  $(SRC_DIR)VertexBuffer.cpp $(HEADERS_DIR)VertexBuffer.h
    	$(info Compiling VertexBuffer.cpp) @$(CC) $(CFLAGS) $(INCLUDE) -c $(SRC_DIR)VertexBuffer.cpp -o $(OBJECT_DIR)VertexBuffer.o
    
    
    #Compile VertexAttributes.cpp
    $(OBJECT_DIR)VertexAttributes.o:  $(SRC_DIR)VertexAttributes.cpp $(HEADERS_DIR)VertexAttributes.h
    	$(info Compiling VertexAttributes.cpp) @$(CC) $(CFLAGS) $(INCLUDE) -c $(SRC_DIR)VertexAttributes.cpp -o $(OBJECT_DIR)VertexAttributes.o
    
    
    #Compile IndexBuffer.cpp
    $(OBJECT_DIR)IndexBuffer.o:  $(SRC_DIR)IndexBuffer.cpp $(HEADERS_DIR)IndexBuffer.h
    	$(info Compiling IndexBuffer.cpp) @$(CC) $(CFLAGS) $(INCLUDE) -c $(SRC_DIR)IndexBuffer.cpp -o $(OBJECT_DIR)IndexBuffer.o
    
    
    #Compile Core.cpp
    $(OBJECT_DIR)Core.o:  $(SRC_DIR)Core.cpp $(HEADERS_DIR)Core.h
    	$(info Compiling Core.cpp) @$(CC) $(CFLAGS) $(INCLUDE) -c $(SRC_DIR)Core.cpp -o $(OBJECT_DIR)Core.o
    
    
    #Compile Renderer.cpp
    $(OBJECT_DIR)Renderer.o:  $(SRC_DIR)Renderer.cpp $(HEADERS_DIR)Renderer.h
    	$(info Compiling Renderer.cpp) @$(CC) $(CFLAGS) $(INCLUDE) -c $(SRC_DIR)Renderer.cpp -o $(OBJECT_DIR)Renderer.o
    
    
    #Compile Texture.cpp
    $(OBJECT_DIR)Texture.o:  $(SRC_DIR)Texture.cpp $(HEADERS_DIR)Texture.h
    	$(info Compiling Texture.cpp) @$(CC) $(CFLAGS) $(INCLUDE) -c $(SRC_DIR)Texture.cpp -o $(OBJECT_DIR)Texture.o
    
    
    
    
    #==========================Compilation Goes Here==========================#
    
    #Clean.
    clean:
    	$(info Cleaning Up!) @rm $(NAME) $(OBJECT_DIR)*.o

  6. #6
    Join Date
    Jul 2017
    Posts
    15

    Re: Segmentation fault after declaring class member.

    I found the answer! If you see my Core.cpp includes Engine.h which includes all of my custom header files. Since in the makefile i'm instructing dependencies only for Core.cpp and Core.h, every time i was updating another header file (which i was including in my Core.cpp) the Core.cpp was not getting compiled. Probably i must update my makefile and insert all the .h file dependencies.

    Well look what you can learn from bugs!! This is a very good knowledge which i didn't know since i'm knew in linux and make. I used to work with visual studio that does all of this stuff automatically for you and you don't need to know how compilation and linking actually works (at least the advanced stuff, you should know the basics of compilation and linking when you are programming in c++).

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,195

    Re: [RESOLVED] Segmentation fault after declaring class member.

    Well I don't use Make , only the IDE compile/link tools.
    Anywere I'm glad you have (almost!) solved your problem!
    Victor Nijegorodov

  8. #8
    Join Date
    Jul 2017
    Posts
    15

    Re: [RESOLVED] Segmentation fault after declaring class member.

    At least now I can respect these tools, especial Microsoft's visual studio.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,195

    Re: [RESOLVED] Segmentation fault after declaring class member.

    Quote Originally Posted by babaliaris View Post
    At least now I can respect these tools, especial Microsoft's visual studio.
    Victor Nijegorodov

  10. #10
    Join Date
    Aug 2002
    Posts
    681

    Re: [RESOLVED] Segmentation fault after declaring class member.

    Hi,
    Linux does have a lot of different IDEs: Anjuta, KDevelop, Code::Blocks, CodeLite...

    Thank you.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)