Sprite Trails in Windows 64 but not GTK2 lazarus / 31-LCL ---------------------------------------------------------------------- Windsurfer · 2019-01-21 11:30 I run a local programming mixed ability group. I use Linux Mint 19.1 and they use Windows 7, 8, 10. We are looking at the basics of sprites, using the LCL. (I know we should use BGRABitmap, but some of the group have never coded before, and they already have many new concepts to deal with.) In Linux Mint 19.1 Cinnamon with GTK2 everything works correctly. In Windows the sprites leave trails and heaptrace shows a memory leak. I have tested with fpc 3.04 and both Laz 1.8 fixes and Laz 2.0 fixes in Linux and 1.8 fixes in Windows The problems seem to occur at the imgBuffer.Assign(imgBackGround) statement in the following code. It seems that the assignment of the standard background to the buffer does not work properly when the procedure is triggered by a timer. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.MoveSprite;beginIn Linux Mint 19.1 everything works correctly.  // Movement of sprite by keyboard: UP/DOWN/LEFT/RIGHT arrows advance smooth version  if (GetKeyState(VK_LEFT) < 0) then    if (iSpLeft >= 10) then      iSpLeft := iSpLeft - 10;   if (GetKeyState(VK_RIGHT) < 0) then    if (iSpLeft <= Image1.Clientwidth - iSpwidth - 10) then    iSpLeft := iSpLeft + 10;   if (GetKeyState(VK_UP) < 0) then    if (iSpTop >= 10) then      iSpTop := iSpTop - 10;   if (GetKeyState(VK_DOWN) < 0) then    if (iSpTop <= Image1.ClientHeight - iSpHeight - 10) then      iSpTop := iSpTop + 10;   imgBuffer.Assign(imgBackground);   // Write Background to Buffer  Imagelist1.Draw(imgBuffer.Canvas, iSpLeft, iSpTop, 0, True);  // Draw Sprite to Buffer.  Image1.Picture.Assign(imgBuffer); // Paint new image in Buffer to screenend;       If anyone has the time could they please check the attached program? I suspect an error in the LCL, but it needs an independent check before I file a bug report. ---------------------------------------------------------------------- Handoko · 2019-01-21 12:09 I tested your Sprite3, I got SIGSEGV error when closing the program. It happened because you need to put Timer1.Enabled := False on Form.OnClose event. Other than that, everything seems to run correctly. Tested on Ubuntu 18.10 Lazarus 1.8.4 GTK 2. I saw no image inside the ImageList, so I drew the stickman for testing. ---------------------------------------------------------------------- Windsurfer · 2019-01-21 13:54 Thanks Handoko, I was hoping you might look at it. I have read your recent posts on sprites, and intend to pick up on the speed control that you and wp have added to the 2.0 release examples. I would never have thought of disabling the timer, although it now seems obvious. I'm not sure what happened to item 0 in the imagelist, so thanks for adding one. I'll now ask my cohorts / students to check it on Windows. ---------------------------------------------------------------------- Handoko · 2019-01-21 14:35 I cross compiled to win32 and tested it using WINE. Yes, I can see the issue you mentioned. I'm going to go away from my computer, I will be back to check the issue later. ---------------------------------------------------------------------- Windsurfer · 2019-01-21 14:38 Thanks. I now have no errors or memory leak in Windows. I will check for units to include in the uses clause. ---------------------------------------------------------------------- marcov · 2019-01-21 14:38 Afaik this is normal and happens with Delphi too, since you don't redraw the background. Maybe GTK does this automatically, but that is not an universal principle. ---------------------------------------------------------------------- Windsurfer · 2019-01-21 17:28 marcov, That is what the code is intended to do. imgBackGround never changes. Before every move imgBackground is first assigned to the imgBuffer and then the sprite is added to imgBuffer. The altered imgBuffer is then assigned to Image1. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  imgBuffer.Assign(imgBackground);   // Write Background to Buffer  Imagelist1.Draw(imgBuffer.Canvas, iSpLeft, iSpTop, 0, True);  // Draw Sprite to Buffer.  Image1.Picture.Assign(imgBuffer); // Paint new image in Buffer to screen It looks as though in Windows the line of code to assign the background to the buffer only works the first time. From then on they seem to be the same image, rather than the buffer being a copy. EDIT I'll try copying the background to another image to see if that is so. ---------------------------------------------------------------------- Handoko · 2019-01-21 17:49 Your code works if you change this imgBuffer.Assign(imgBackground); to this:   imgBuffer.Canvas.CopyRect(Rect(0,0,imgBuffer.Width, imgBuffer.Height),     imgBackGround.Canvas, Rect(0,0,imgBackGround.Width, imgBackGround.Height)); Tested on WINE win32. Maybe it is a bug, but I suggest we should test it on Delphi first. Because I checked the documentation of TBitmap.Assign on both FPC and Delphi, they're different and it sounds suspicious to me: https://lazarus- ccr.sourceforge.io/docs/lcl/graphics/tcustombitmap.assign.html http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpU pdate2/EN/html/delphivclwin32/Graphics_TBitmap_Assign.html Delphi version says, it copies the bitmap image contained in Source to the bitmap object. The FPC version is a bit different: If the BitMap data are already in use, the data are shared; if not, a new BitMap is produced. So in FPC, it may not actually copy the data. Could it be the cause of the issue? You know, if you modify (in this case do painting) on the shared data (imgBuffer), the original image (imgBackground) also get painted. That's why the trails won't be deleted. But why it doesn't happen on GTK? So if you follow the documentation of FPC version, the bug is on the GTK. ---------------------------------------------------------------------- Windsurfer · 2019-01-21 19:02 I did try something like that in Linux, but it did not work, probably because I had an error. It just occurred to me that changing Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Image1.Picture.Assign(imgBuffer); // Paint new image in Buffer to screento Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Image1.Picture.Assign(imgBackground); // Paint new image in Buffer to screenshould show if they are both the same image rather than separate copies. If you could test it in Windows, I would be grateful. I have terrible problems with permissions in Windows 10. If Wine works, I'll install it for testing. ---------------------------------------------------------------------- marcov · 2019-01-22 10:10 Afaik it is not a bug, but winapi. TPaintbox was created partially to make this more uniform over platforms. ---------------------------------------------------------------------- Windsurfer · 2019-01-22 19:52 Thanks marcov. Knowing it is an API issue prevents further searching. Our group met this morning.  It allowed me to introduce compiler directives to them, and I showed them the solution below. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$ifdef mswindows}  imgBuffer.Canvas.CopyRect(Rect(0,0,imgBuffer.Width, imgBuffer.Height),    imgBackGround.Canvas, Rect(0,0,imgBackGround.Width, imgBackGround.Height));{$else}imgBuffer.Assign(imgBackground);{$endif} It may be that one is faster than the other, but we did not investigate that. Handoku, would this be worth including in your sprite example code. It might help someone else to avoid the wrong path.