Segmentation fault

Hi all!
So i´m trying to make an aplication here using fbo
but in this line:

Object* fbo = new FBO(m);

CodeBlocks send me a segmentaion fault error
How can i resolve this?

this is the class where all is rendering on the screen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "sg3d.h"


#include <SDL/SDL.h>
#include "gl_util.h"
#include "immediate.h"
#include "DisplayList.h"
#include "vertex_buffer_object.h"
#include "loader.h"
#include "Texture.h"
#include "FBO.h"

#include <iostream>

namespace topicos
{
SG3D::SG3D()
{
    _end = false;
    _rotation = 0;
    start();
}

SG3D::~SG3D()
{
    for (unsigned i = 0; i < _sceneObject.size(); ++i)
    {
        delete _sceneObject[i];
    }
}

void SG3D::start()
{
    unsigned w, h;
    w = 800;
    h = 600;
    _sdl.videoMode(w, h);
    GLUtil::initGL();
    GLUtil::setView(w, h);
    GLUtil::initGlew();
    initTexture();
    initObjects();
    // initLight();
    // initShader();
    mainLoop();


}
void SG3D::initTexture()
{
    ///drawing a simple cube with texture
    text = new Texture("data/test.png");
    text->genTexture();

}
/**
* Inicia aqui os diferentes objetos
**/
void SG3D::initObjects()
{


    std::cout << "loading obj" << std::endl;
    Mesh m = Loader::loadObj("build/obj/cavalo1.obj");
    std::cout << "\t\t\tend." << std::endl;

    std::cout << "creating FBO mode" << std::endl;
    Object* fbo = new FBO(m);
    fbo->translate(Vec3(8, 0, 0));
    fbo->color(Vec3(0, 1, 0));
    fbo->rotate(-90, Vec3(1, 0, 0));
    _sceneObject.push_back(fbo);
    std::cout << "\t\t\tend." << std::endl;


}
void SG3D::mainLoop()
{
    SDL_Event e;
    while(!_end)
    {
        while(SDL_PollEvent(&e))
        {
            switch(e.type)
            {
            case SDL_QUIT:
                _end = true;
                break;
            case SDL_KEYDOWN:
                switch(e.key.keysym.sym)
                {
                case SDLK_ESCAPE:
                    _end = true;
                    break;
                } //END keydow check
                break;
            default:
                break;
            } //END type check
        }//END while poll
        update();
        draw();
    }//END while end
    _sdl.quit();
}

/**
 * Funcao responsavel por atualizacoes.
 *
 **/
void SG3D::update()
{
    //Esse update eh para bonito. Nao seria legal ficar fazendo esse tipo de verificacao dentro do update.
    if (_sceneObject.size() == 0)
    {
        std::cout << "Voce nao pode acessar o elemento zero. O tamanho da sua lista eh zero" << std::endl;
    }
    else
    {
        Object* obj = _sceneObject[0]; //Pegando o elemento zero (Erro caso não tenhamos objetos)
        obj->rotate((_rotation++), Vec3(1, 0, 0)); //Rodando o elemento zero.
    }
}


/**
 * Funcao responsavel por chamar a lista de objetos que serao desenhados.
 *
 **/
void SG3D::draw()
{
    GLUtil::resetDraw();


    text->TexturedCube();
    for (std::vector<Object*>::iterator it = _sceneObject.begin(); it != _sceneObject.end(); ++it)
    {
        (*it)->draw();
    }

    _sdl.swap();
}

}


And this is the FBO class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <GL/glew.h>
#include "FBO.h"
#include "mesh.h"
#include "Vertex_buffer_object.h"
#include "loader.h"
#include <iostream>

FBO::FBO(const Mesh& m): Object(m)
{
    this->initTex();
    this->initFBO();
    this->mesh = m;
}

FBO::~FBO()
{
    //dtor
}
void FBO::initTex()
{
    Texture* tex = new Texture("data/test.png");
    textId = tex->genTexture();

}
void FBO::initFBO()
{
    glGenFramebuffers(1, &_fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
    // attach a texture to FBO color attachment point
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textId, 0);

    checkFramebufferStatus();

}
void FBO::draw()
{
    // set the rendering destination to FBO
    glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
    // clear buffer
    glClearColor(1, 1, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    std::cout << "creating VBO mode" << std::endl;
    Object* vbo = new Vertex_buffer_object(this->mesh);
    vbo->translate(Vec3(8, 0, 0));
    vbo->color(Vec3(0, 1, 0));
    vbo->rotate(-90, Vec3(1, 0, 0));
    vbo->draw();
    std::cout << "\t\t\tend." << std::endl;

    // back to normal window-system-provided framebuffer
    glBindFramebuffer(GL_FRAMEBUFFER, 0); // unbind
}
void FBO::checkFramebufferStatus()
{
    // check FBO status
    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    switch(status)
    {
    case GL_FRAMEBUFFER_COMPLETE:
        std::cout << "Framebuffer complete." << std::endl;
        break;
    case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
        std::cout << "[ERROR] Framebuffer incomplete: Attachment is NOT complete." << std::endl;
        break;

    case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
        std::cout << "[ERROR] Framebuffer incomplete: No image is attached to FBO." << std::endl;
        break;

    case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
        std::cout << "[ERROR] Framebuffer incomplete: Draw buffer." << std::endl;
        break;

    case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
        std::cout << "[ERROR] Framebuffer incomplete: Read buffer." << std::endl;
        break;

    case GL_FRAMEBUFFER_UNSUPPORTED:
        std::cout << "[ERROR] Framebuffer incomplete: Unsupported by FBO implementation." << std::endl;
        break;

    default:
        std::cout << "[ERROR] Framebuffer incomplete: Unknown error." << std::endl;
        break;
    }
}


Thank you very much!
'FBO' inherits from 'Object'. So logically a variable of type 'Object' is too small to hold a variable of type 'FBO'. Why did you try to do it this way?

Not sure where I was going with that. A pointer is only ever a certain size.
Last edited on
yes 'FBO' inherits from 'Object' so FBO is a Object,Object can hold fbo object logically.
I'm doing this because Object is an abstract class and i need to override the draw function
Last edited on
That's crap logic, but ignore what I wrote before anyway.

What you did has nothing to do with overriding a member function. A call to a member function of a pointer to a base class will use the most derived version of the function being called. So by declaring that FBO has it's own 'draw()' function, you've already overridden it. http://en.cppreference.com/w/cpp/language/virtual
yes i know so what is causing my problem?
The heap is probably corrupted before you create the new FBO. Can you run the program with heap checking?

BTW FBO::InitTex() appears to leak the tex object.
Topic archived. No new replies allowed.