FLTK logo

[fltk.general] Re: Newbie: question about destruction of FLTK ojbects

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Library      Forums      Links      Apps     Login 
[  All Forums  |  Back to fltk.general  ]
 
[ Previous Message ] [ New Message | Reply ] [ Next Message ]

Re: Newbie: question about destruction of FLTK ojbects S ET 13:42 Sep 27  
  I got to deleting the main window in a handler, may be it's not the optimal way though:

// ----------------------------------------------------------------------------------------------
static int OnMainWindowEvent(Fl_Widget * w, void * p)
{
  int ev = Fl::event();
 
  switch (ev)
  {
    case FL_CLOSE:
      delete w;
      return 0; // 0 or 1 ?
    default:
      std::cout << "Main window event: " << ev << std::endl;
      break;
  }
 
  return 1;
}
// ----------------------------------------------------------------------------------------------

I am happy to read the many more options you have listed;

and to note the reactivity of the forum.

So many more subtleties to discover yet.

Thank you all and regards.

P.S: I'm not that unfamiliar to C++, just coming from other worlds
Le dimanche 27 septembre 2026 à 13:36:20 UTC+2, Daniel G. a écrit :
Hi,

in C++ if you create objects with the keyword 'new' you need to delete them yourself with 'delete'.

Please learn about RAII and Smart Pointers. Modern concepts which help you avoiding this problem.

nmse...@gmail.com schrieb am Sonntag, 27. September 2026 um 12:44:07 UTC+2:
Hello,

This is my very first step with FLTK and there are certainly many basic things I'm missing about this library.

My concern here is about object destruction.

The following mockup is created with FLUID. I have a added a destructor that is not called at all when the double window closes, with an exit code of 0.
// ---------------------------------------------------------------------------------------------------------------------
// cat blank.h
// generated by Fast Light User Interface Designer (fluid) version 1.0405

#ifndef blank_h
#define blank_h
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>

class UserInterface {
public:
  Fl_Double_Window* make_window();

  // Testing destructor, not added by FLUID.
  virtual ~UserInterface();
};
#endif

// ---------------------------------------------------------------------------------------------------------------------
// cat blank.cpp
// generated by Fast Light User Interface Designer (fluid) version 1.0405

#include "blank.h"

Fl_Double_Window* UserInterface::make_window() {
  Fl_Double_Window* w;
  { Fl_Double_Window* o = new Fl_Double_Window(480, 320);
    w = o; (void)w;
    o->user_data((void*)(this));
    o->end();
  } // Fl_Double_Window* o
  fl_mkdir("/tmp/blank", S_IRUSR | S_IWUSR);
  return w;
}

#include <iostream>
UserInterface::~UserInterface()
{
  // Does not get called!
  std::cout << __FUNCTION__ << std::endl;
  fl_rmdir("/tmp/blank");
}

// ---------------------------------------------------------------------------------------------------------------------
// cat main.cpp
#include "blank.h"

int main(int argc, char * argv[])
{
  UserInterface * ui = new UserInterface();
  Fl_Double_Window * cw = ui->make_window();
 
  cw->show();
  return Fl::run();
}

On application exit, nothing is printed in stdout and the created directory persists. I would have expected the contrary.

I wish to know what's going on, how far is it normal and what should I consider to resolve this.

Using fltk 1.4.5-2 on Arch Linux.

Thank you for any guidance.

--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/fltkgeneral/4236fd34-f8eb-4809-9864-a94e8d19f15dn%40googlegroups.com.
[ Direct Link to Message ]
 
     
[ Previous Message ] [ New Message | Reply ] [ Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2026 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.