Scott A Thisse wrote:
> On Wed, 2009-12-09 at 01:12 -0800, Albrecht Schlosser wrote:
>>> Example code:
>>> static void draw_png_icon(Fl_Color c) {
>>> fl_color(c);
>>> static Fl_PNG_Image *i = new Fl_PNG_Image("icon.png");
>> Note: you wrote "Example code", and so I assume that this is not
>> your real code, but if it is: the above statement would create
>> a new image (and load it from disk) for each call to your draw
>> function. This should read:
>>
>> static Fl_PNG_Image *i = 0;
>> if (!i) i = new Fl_PNG_Image("icon.png");
>
> Really? Am I missing something obvious? I thought that static variable
> initialization occurs only once, regardless of the RHS expression.
It does, and that's the reason you want it initialized to zero only
once,
so that the line below it executes once (allocates the image once)
no matter how many times the function is called. Very important.
If that variable wasn't static, i would be zeroed on each call
to the function, and the image would be re-allocated each time,
leaking memory on each call..
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk