FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

segfault whith DerelictGLU.unload();

 
Post new topic   Reply to topic     Forum Index -> Derelict
View previous topic :: View next topic  
Author Message
bioinfornatics



Joined: 22 Jun 2010
Posts: 90

PostPosted: Wed Sep 15, 2010 12:57 am    Post subject: segfault whith DerelictGLU.unload(); Reply with quote

when i do
Code:
private import derelict.sdl.sdl;
private import derelict.opengl.gl;
private import derelict.opengl.glu;
private import tango.stdc.stringz;
private import tango.io.Stdout;
private import derelict.sdl.sdl:    SDL_Event, SDL_PollEvent, SDL_QUIT;

class Display{
    // horizontal and vertical screen resolution
    private uint height;
    private uint width;
    // number of bits per pixel used for display. 24 => true color
    private uint bitsPerPixel;
    // field of view => the angle our camera will see vertically
    private float fov;
    // distance of the near clipping plane
    private float nearPlane;
    // distance of the far clipping plane
    private float farPlane;
    /**
     * Setup some basic OpenGL parameters
     */
    private void setupGL(){
        // switch to the projection mode matrix
        glMatrixMode(GL_PROJECTION);
        // load the identity matrix for projection
        glLoadIdentity();
        // setup a perspective projection matrix
        gluPerspective(fov, cast(float)height / width, nearPlane, farPlane);
        // switch back to the modelview transformation matrix
        glMatrixMode(GL_MODELVIEW);
        // load the identity matrix for modelview
        glLoadIdentity();
    }
    /**
     * Library initializer
     */
    private void init(){
        // initialize SDL, GL and GLU Derelict modules
        DerelictSDL.load();
        DerelictGL.load();
        DerelictGLU.load();
        // initialize SDL's VIDEO module
        SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
        // enable double-buffering
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
        // create our OpenGL window
        SDL_SetVideoMode(height, width, bitsPerPixel, SDL_OPENGL);
        SDL_WM_SetCaption(toStringz("D is the best"), null);
        setupGL();
    }
    /**
     * be nice and release all resources
     */
    private void cleanup(){
        // tell SDL to quit
        SDL_Quit();
        // release GL, GLU and SDL's shared libs
        DerelictGLU.unload();
        DerelictGL.unload();
        DerelictSDL.unload();
    }
    /**
     * Constructor
     */
    public this(){
        height      = 800;
        width       = 600;
        bitsPerPixel= 24;
        fov         = 90;
        nearPlane   = 0.1f;
        farPlane    = 100.f;
        init();
    }
    public ~this(){
        cleanup();
    }
    public void clear(){
        glClear(GL_COLOR_BUFFER_BIT);
    }
    public void drawGLFrame(){
        glBegin(GL_TRIANGLES);
            glColor3f (1,  0,  0);
            glVertex3f(-1, -1, -2);
            glColor3f (0,  1,  0);
            glVertex3f(1, -1, -2);
            glColor3f (0,  0,  1);
            glVertex3f(0,  1, -2);
        glEnd();
        // swap the buffers, making our backbuffer the visible one
        SDL_GL_SwapBuffers();
    }

    public bool event(){
        bool isRunning = true;
        SDL_Event event;
        // handle all SDL events that we might've received in this loop iteration
        while(SDL_PollEvent(&event)){
            switch(event.type){
                // user has clicked on the window's close button
                case SDL_QUIT:
                    isRunning = false;
                    break;
                // by default, we do nothing => break from the switch
                default:
                    break;
            }
            Stdout.formatln("event.type: {}",event.type);
        }
        return isRunning;
    }
}

void main(){
    bool    isRunning   = true;
    Display display     = new Display();
    while(isRunning){
        isRunning = display.event();
        // clear the screen. by default it clears to black
        display.clear();
        //display graphic
        display.drawGLFrame();
    }
}

programm segfault when enter in display destructor call cleanup private function
when try do DerelictGLU.unload();
Back to top
View user's profile Send private message
aldacron



Joined: 05 May 2004
Posts: 1322
Location: Seoul, South Korea

PostPosted: Wed Sep 15, 2010 5:30 am    Post subject: Reply with quote

I suggest you read up on D's destructors. In short, if you aren't deleting objects explicitly, then you cannot rely on them ever being called or, if they are, on the order in which they are called. Basically, don't rely on destructors for cleanup of objects.

I'm guessing that the problem here is that when the destructor on the Display object is run, the DerelictGLU object has already been cleaned up. That's just a guess, though. Someone would need to verify that with a debugger.

Three suggestions.

1) Don't call cleanup from your destructor. Call it manually before the program exits. Either that, or explicitly delete the display object. Which ever way you go, I suggest you do it in a scope(exit) block.

2) If you do use a scope(exit) block as suggested in 1, then it is possible for SDL_Quit not to have been loaded, as the code in the scope(exit) block will be called even when an exception is thrown. So you'll need to make sure the function isn't null before calling it:

Code:

if(SDL_Quit !is null) SDL_Quit();


If you don't use the scope(exit) for cleanup, you probably don't need to worry about this.

3) Remove the calls to Derelict*.unload. Those aren't necessary, as they are automatically called by Derelict via static module destructors on program exit. They are publicly exposed in case you need to do hotswapping at runtime. For normal use, they can be completely ignored.
_________________
The One With D | The One With Aldacron | D Bits
Back to top
View user's profile Send private message Send e-mail
bioinfornatics



Joined: 22 Jun 2010
Posts: 90

PostPosted: Wed Sep 15, 2010 6:57 am    Post subject: Reply with quote

Big thanks for this nice explanation Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Derelict All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group