Page 1 of 1

Severe Memory Leak Problems

Posted: Sun Jul 26, 2015 12:57 am
by ThatOneGuy
I have been playing around with images and files in LiveCode and have noticed some extremely severe memory leaks.

It seems as though LiveCode likes to create new versions of variables when changing scope and then quickly lose the pointers to the previous versions, making them impossible to delete or free up in any way, even after returning to the previous scope.

One attempt to edit images by changing the imagedata of the image stored in a secondary buffer causes about 300 MB of memory leakage on each test run or about 1 MB per edit of the secondary buffer. This constitutes an exact duplicate of the secondary buffer being made and the previous version being subsequently lost in memory every time a function is called to edit it.

Placing the code below in the stack script of a stack with a 640x400 image named "image" will cause the aforementioned memory leak when running "test".

Code: Select all

local imgWidth
local imgHeight
local pixelData
local theWriteBuffer

on setupImage
   put the width of img "image" into imgHeight
   put the height of img "image" into imgWidth
   put the imagedata of img "image" into theWriteBuffer
end setupImage

on test
   setupImage
   setDrawColor(255, 255, 0, 0)
   repeat with x = 1 to 100
      drawPixel( x,100)
   end repeat
   setDrawColor(255, 0, 255, 0)
   repeat with x = 1 to 100
      drawPixel( x,110)  
   end repeat
   setDrawColor(255, 0, 0, 255)
   repeat with x = 1 to 100
      drawPixel( x,120)   
   end repeat
   writeBuffer
end test

on setDrawColor colorData
   put numtochar(item 4 of colorData) & numtochar(item 3 of colorData) & numtochar(item 2 of colorData) & numtochar(item 1 of colorData) into pixelData
end setDrawColor

on drawPixel theLoc
   local thePixel
   put (item 1 of theLoc * 4) + ((item 2 of theLoc) * imgWidth * 4) into thePixel
   put pixelData into char thePixel to (thePixel + 3) of theWriteBuffer
end drawPixel

on writeBuffer
   set the imagedata of img "image" to theWriteBuffer
end writeBuffer

Re: Severe Memory Leak Problems

Posted: Sun Jul 26, 2015 1:12 am
by FourthWorld
Confirmed here on Ubuntu 14.04 with LC 7.1dp1.

Please submit this to the bug queue so they can address it
http://quality.runrev.com/

Leaks tend to get a very high priority there, so with any luck they may be able to fix this before dp2.

Thanks -

Re: Severe Memory Leak Problems

Posted: Sun Jul 26, 2015 8:51 am
by ThatOneGuy

Re: Severe Memory Leak Problems

Posted: Mon Jul 27, 2015 2:52 pm
by FourthWorld
Thanks for submitting that - its status is "In Progress" already, so it looks like this will be resolved shortly.

Memory leaks and crashers are rare in LiveCode, because those tend to get high priority once a good recipe is submitted for it. Thanks for taking the time to post that good recipe.

Re: Severe Memory Leak Problems

Posted: Tue Jul 28, 2015 12:51 am
by mwieder
Yeah, I reported similar leaks in libpango in 15617 and attached a valgrind dump. Peter reproduced the leaks there as well, so I'm hopeful that a fix is on the way.
And it looks like a simple change https://github.com/runrev/livecode/pull/2572/files.