On 28.04.2010, at 08:09, Jean-Pierre Lefebvre wrote:
> I will provide you with more details about my observation, and with this
> picture in a couple of days, as soon as my deadline will be over.
I'd also be interested whether my test program crashes with your image
and window dimensions. Please test by changing the #defines for WIDTH
and HEIGHT as well as the image name and report here if you can
reproduce the crash. If you can't, then the problem you are seeing is
probably elsewhere in your code (and we don't have a problem in FLTK).
> However, maybe it is a "newbie" question (and I apologize for that), but in
> your previous post, you told me that my code "is likely to produce memory
> leaks for the primary image and maybe the copied image". Could you please
> tell me what is wrong in this code, or what should I do to avoid this problem
> ?
I wrote "likely" because this depends on how you allocate and/or reuse
your windows and images. If it's only a one-shot
create-and-show-and-finish-the-program, then you don't need to bother.
Else: see below.
> I give you below the declaration in the *.h, and the code in the *.cpp files.
> * == *.h file ===============================================
>
> class LocalfswToolWindow {
> public:
> LocalfswToolWindow( int deltaFontSize );
> ~LocalfswToolWindow();
This is an empty (default) destructor. Did you define it elsewhere? See
below for more info what would probably be needed.
> void show();
> void hide();
>
> Fl_Window *win;
> int width;
> int height;
> bool morfeo_install_dir;
> Fl_PNG_Image *Tool_FSW;
> Fl_Box *Tool_FSW_Box;
> };
Note that the pointers are not initialized. However, your constructor
seems to do it (and sets NULL values in some cases).
> * ============================================================
> * == *.cpp file =============================================
>
> LocalfswToolWindow::LocalfswToolWindow( int deltaFontSize )
> {
> // --- Size of the window
> width = 14 * (BB + WB);
> height = 10 * (BB + WB);
>
> win = new paletteWindow( width, height,
> CTX::instance()->nonModalWindows ?
> false : true, "Local FSW Tool");
> win->box(GMSH_WINDOW_BOX);
>
> if( getenv( "MORFEO_INSTALL_DIR" ) != NULL ){
> morfeo_install_dir = true;
>
> // --- Path to the picture
> string picture_path = getenv( "MORFEO_INSTALL_DIR" );
> picture_path += "/Figures/Tool_Description.png";
>
> fl_register_images();
> Tool_FSW = new Fl_PNG_Image( picture_path.c_str() );
Here you allocate a new object, the pointer is stored in your class. The
Fl_PNG_Image will never be deleted. That's what I called the "primary
image".
> Tool_FSW_Box = new Fl_Box(0,0, width, height);
> Tool_FSW_Box->box(FL_FLAT_BOX);
> Tool_FSW_Box->image(Tool_FSW->copy(width, height));
Tool_FSW->copy(width, height) allocates another image. The pointer is
stored in the Fl_Box widget. This image will never be deleted either.
That's what I called the "copied image".
> win->position( CTX::instance()->solverPosition[-1],
> CTX::instance()->solverPosition[0] );
> }
> else{
> morfeo_install_dir = false;
> Msg::Warning("The environment variable 'MORFEO_INSTALL_DIR' is not
> defined.");
> Msg::Warning("The windows describing the heat source types and the tool
> geometry are not displayed.");
> Msg::Warning("Please, refer to the Documentation in order to define this
> environment variable.");
> FlGui::instance()->messages->show();
> Tool_FSW = NULL;
> Tool_FSW_Box = NULL;
> }
>
> win->end();
> }
> * ============================================================
>
> Thanks a lot in advance for your help,
>
> Jean-Pierre
In your case it seems like this destructor would help:
LocalfswToolWindow::~LocalfswToolWindow() {
if (Tool_FSW) delete Tool_FSW;
if (Tool_FSW_Box && Tool_FSW_Box->image())
delete Tool_FSW_Box->image();
}
That might not be complete yet, because you don't delete the window as
well. Do you hide it, or does it stay open all the time? But, as I wrote
before: if you don't reuse the class during the program's execution
(i.e. if there is only one instance of your class), don't bother,
because the system will clean everything on exit.
DISCLAIMER: Code untested, just to give you an idea what to look for.
HTH
Albrecht
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk