Freeglutians (and lurkers),

        I call you today with a problem. A problem with callbacks, Steve Baker's PUI, and an innocuous PUI example program named "complex.cpp" ( find it here: http://plib.sourceforge.net/dist/plib_examples-1.6.1.tar.gz ).

        This program simply crashes with an access violation when built with freeglut on Win2k with MSVC 6. The program ran fine on GLUT last I checked. Here's the problem:

        The file has an utterly normal GLUT initialization with PUI, which (abbreviated) looks like this:

int main ( int argc, char **argv )
{
  firsttime = TRUE;

  glutInitWindowPosition( 100,   0 ) ;
  glutInitWindowSize    ( 640, 480 ) ;
  glutInit              ( &argc, argv ) ;
  glutInitDisplayMode   ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ) ;
  main_window = glutCreateWindow      ( "Complex PUI Application"  ) ;
  glutDisplayFunc       ( displayfn ) ;
  glutKeyboardFunc      ( keyfn     ) ;
  glutSpecialFunc       ( specialfn ) ;
  glutMouseFunc         ( mousefn   ) ;
  glutMotionFunc        ( motionfn  ) ;

#ifdef VOODOO
  glutPassiveMotionFunc ( motionfn  ) ;
#endif

  glutIdleFunc          ( displayfn ) ;

  puInit () ;

#ifdef VOODOO
  puShowCursor () ;
#endif

{...}

  save_window = glutCreateWindow ( "Saving" ) ;
  glutDisplayFunc       ( savedisplayfn ) ;
  glutKeyboardFunc      ( keyfn     ) ;
  glutSpecialFunc       ( specialfn ) ;
  glutMouseFunc         ( mousefn   ) ;
  glutMotionFunc        ( motionfn  ) ;
  glutPassiveMotionFunc ( motionfn  ) ;
  glutIdleFunc          ( savedisplayfn ) ;
  glutReshapeFunc       ( savereshapefn ) ;
  glutHideWindow        () ;

{...}

  glutMainLoop () ;
  return 0 ;
}

        The window in question is save_window. save_window has two unique callbacks, savedisplayfn() and savereshapefn(). save_window is glutCreate'd in the main loop but is not initialized (had its GUI widgets built) until a menu item is clicked, executing the save_cb() function, which looks like this:

static void save_cb ( puObject * )
{
  static int save_count = 0 ;  // Number of buttons in file picker
  int w = 320, h = 270 ;
  glutSetWindow ( save_window ) ;
  glutShowWindow () ;
  glutReshapeWindow ( w, h ) ;
  glutPositionWindow ( ( 640 - w ) / 2, ( 480 - h ) / 2 ) ;
  if ( ++save_count > 2 ) save_count = 0 ;

  file_selector = new puFileSelector ( 0, 0, w, h, save_count, ".", "Pick Place To Save" ) ;
  file_selector -> setCallback ( pick_cb ) ;
  file_selector->setChildStyle ( PUCLASS_ONESHOT, PUSTYLE_BOXED ) ;
  file_selector->setChildBorderThickness ( PUCLASS_ONESHOT, 5 ) ;
  file_selector->setChildColour ( PUCLASS_SLIDER, 0, 0.5, 0.5, 0.5 ) ;
}

        At that point, file_selector becomes initialized with a new puFileSelector widget object. Before that, file_selector is NULL. Unfortunately, the savereshapefn() tries to resize file_selector as follows:

static void savereshapefn ( int w, int h )
{
  file_selector->setSize ( w, h ) ;
}

        Herein lies the problem. In GLUT this callback to resize the window apparently is not called until after the window has been created. Freeglut calls this callback almost immediately, before the window has been setup and file_selector set to a real object.  Even save_cb() itself calls glutReshapeWindow() (and hence savereshapefn()) before actually giving file_selector a value. GLUT must not execute callback functions until a window has been fully created, but how does it know?

Obviously a nullcheck in savereshapefn() would fix this, but this doesn't change the fact that we're breaking a former GLUT program.

        I don't know what to do to fix this.

        I've submitted this (and complex.cpp) as a bug report here: http://sourceforge.net/tracker/index.php?func=detail&aid=780477&group_id=1032&atid=101032

        By the way... Anyone who wants editor access to the freeglut webpage, email me. I'll get you setup!

        By the second way... Should we be using freeglut-bugs for these messages?      

-       James 'J.C.' Jones     
        Software Engineer
        Jacobs Sverdrup/TEAS
        Office: 850-729-6141