TTrayIcon fails when changing icon for the second time lazarus / 39-Graphics ---------------------------------------------------------------------- TyneBridges · 2013-04-19 10:02 I've been trying to use TTrayIcon to load several different icons, depending on a value fed to the timer process. Code: [Select]procedure TLCIForm.DrawSTIcon(ProportionRemaining: Byte); Var OurIcon: String; begin     Case ProportionRemaining of           90..100: OurIcon:= 'i1';  // Full           70..89: OurIcon:= 'i2';           50..69: OurIcon:= 'i3';           25..49: OurIcon:= 'i4';           0..24: OurIcon:= 'i5';  // Empty      end; // case      NewIcon:= OurIcon;      If LastIcon <> NewIcon then      begin           ShowMessage('About to change icon to ' + OurIcon);           TI.Icon.Handle:= LoadIcon(hInstance, MakeIntResource(OurIcon));           If TI.Icon.Handle = 0 then ShowMessage('Error loading icon ' + OurIcon);           TI.Hint:= 'Time remaining indicated by black bar (' + IntToStr(ProportionRemaining) + ')';           //TI.PopUpMenu:= MyPopUpMenu;           TI.Show;           //ShowMessage(IntToStr(ProportionRemaining));           LastIcon:= NewIcon;      end; end;  The first two icons are loaded correctly but I get an External: SIGSEGV error on the third. I have debugging switched on, but all I can see is that the error occurs in the following bit of code in icon.inc (TCustomIcon) Code: [Select]procedure TSharedIcon.Clear; var   n: Integer; begin   for n := 0 to FImages.Count - 1 do     TObject(FImages[n]).Free; end; I've added watches for n and FImages.Count and have debug info switched on but the debugger is not helping - it just shows "No symbol in current context", even when I'm using "Step into" to go through the lines above one by one. I've tried to research whether I need to free resources to allow several different icons to load, but an answer on this link suggests not. http://stackoverflow.com/questions/15556817/is-a-memory-leak-possible- with-loadicon Once again, I'm stumped. Is this a bug in the component, or just something I'm doing wrong? Has anyone any advice on how to trace the problem (as the debugger doesn't seem to be working for me)? ---------------------------------------------------------------------- taazz · 2013-04-19 10:28 try using ctrl+alt+S to open the call stack window and see which line in your code is raising the problem after that you need to follow the flow and see why the error occurs my crystal ball says that it has something to do with .free calling a delete somewhere down the line and since the for top value is only evaluated once when the code enters the loop it probably overshoots the remaining objects. It is probably a bug and it should be reported but keep in mind that crystal balls do not always tell the truth. ---------------------------------------------------------------------- TyneBridges · 2013-04-19 10:47 Quote from: taazz on April 19, 2013, 12:28:44 pmtry using ctrl+alt+S to open the call stack window and see which line in your code is raising the problem after that you need to follow the flow and see why the error occurs my crystal ball says that it has something to do with .free calling a delete somewhere down the line and since the for top value is only evaluated once when the code enters the loop it probably overshoots the remaining objects. It is probably a bug and it should be reported but keep in mind that crystal balls do not always tell the truth. That gives me the following, with the highlight on index 5. #0 ?? at :0 #1 SYSTEM_TOBJECT_$__FREE at :0 #2 TSHAREDICON__CLEAR() at .\include\icon.inc:172 #3 TCUSTOMICON__SETHANDLE(1575959, ) at .\include\icon.inc:1082 #4 TICON__SETICONHANDLE(1575959, ) at .\include\icon.inc:1347 #5 TLCIFORM__DRAWSTICON(68, ) at lc.pas:72 #6 TLCIFORM__SESSIONTIMERTIMER(0x3208d8, ) at lc.pas:127 #7 TCUSTOMTIMER__DOONTIMER() at customtimer.pas:181 #8 TCUSTOMTIMER__TIMER() at customtimer.pas:156 #9 TIMERCALLBACKPROC(0, 275, 27401, 12411548) at win32callback.inc:2791 #10 USER32!OffsetRect at :0 #11 ?? at :0 Line 72 of lc.pas is Code: [Select]TI.Icon.Handle:= LoadIcon(hInstance, MakeIntResource(OurIcon)); Does that mean anything to you? I don't know which variable it's referring to, as the only parameter is the icon name which I know is initialised as I currently display it in a message box. ---------------------------------------------------------------------- TyneBridges · 2013-04-19 14:57 In my earlier thread (where I untidily had two different topics) ChrisF wrote: QuoteAnyway, there is a problem with LoadIcon, LodCursor and LoadBitMap: see http://bugs.freepascal.org/view.php?id=24222. So, you can load them by explicitly casting the windows unit. For instance: Code: [Select] hIcon:=Windows.LoadIcon(MainInstance,'MyICONName01'); Helpful advice, and it sounded as if Chris had the solution. However, when I amended my code to Code: [Select]TI.Icon.Handle:= Windows.LoadIcon(hInstance, MakeIntResource(OurIcon));, clicked BUILD and ran the program again, it didn't seem to make any difference at all - the same error occurred in exactly the same place. Edit: As an experiment, I tried amending the line to "Graphics.LoadIcon...". The code still compiled and started to run, but the error occurred earlier - on the second icon (the first change). From that I surmise that the Windows function was the one being called all along and, clearly, there is still a problem somewhere. ---------------------------------------------------------------------- ChrisF · 2013-04-19 17:17 I've got the same problem with TTrayIcon; don't really know why... A bug, I guess. Just in case, here is a quick-coded sample (adapted from my own programs) for implementing SysTray icons, just using the windows API. First, you'll have to put your icon files in the program directory before the compilation, or to manually modify the main.rc file to point to the right icon files. ---------------------------------------------------------------------- TyneBridges · 2013-04-20 09:52 Thanks, Chris. I'm accessing the forum from a tablet at the moment but will try that out as soon as I get back to my main PC. ---------------------------------------------------------------------- TyneBridges · 2013-04-20 17:30 Your example was great, Chris. It seems to work fine for all my icons, so I'm currently working it into my application. Thanks again.