Page 1 of 1
maximum of around 350 images per application??
Posted: Wed Jul 19, 2006 5:47 pm
by Jan Vooijs
Code updated for 5.20+
Hello,
I discovered something strange!
While developing a webcam snap program which copies an image from the clipboard that, there is a maximum of images to 'grab' after around 350 images the programs stops capturing!?!? (no error, no nothing)
Well the solution is to use FreeImage() after the image is 'used' (modified and written to disk in JPG format).
Now the program runs for more than 1500 images so far (every sec one 'shot').
See this snip-it of code:
Code: Select all
Procedure snapshot()
Protected Img, Res
Shared hWndC, Index.l, fName.s, CapMovie.l
ClearClipboard()
SendMessage_(hWndC, #WM_CAP_EDIT_COPY, 0, 0)
Delay( 1) ; extra since every x seconds it fails..
Img = GetClipboardImage( #PB_Any)
Res = SaveImage( Img, fName.s, #PB_ImagePlugin_JPEG, 10)
If Res = 0
Debug "OOPS, no file save.."
EndIf
FreeImage( Img) ; <<== Or else around 350 images the app stops capturing!!!
Debug "snapshot, ends"
EndProcedure
Jan V.
Posted: Wed Jul 19, 2006 6:33 pm
by netmaestro
This is happening because when you use #PB_Any, a new image# is being generated but because it's a new and different image# from all existing image#'s, the old one is not getting freed. Sure, your variable is updated to the new number, but the old number is still a valid image, taking up resources. So with #PB_Any you must free the last image before setting your variable to the new one or else there is a massive memory leak. None of this is necessary if you use hardcoded image#'s, as the old image is automatically freed.
Posted: Wed Jul 19, 2006 9:29 pm
by kenmo
Yeah, I think netmaestro is right. You're probably running low on memory if you have 300+ fullscreen, uncompressed images being stored. If you're not going to use the captured image again, why not free it immediately?
Edit: I just did a little calculating... if you're in 1024x768 32-bit mode, and you have 350 uncompressed screen captures, that's slightly over a gig of memory!
(1024*768*4)*350 / (1024^3)
Posted: Wed Jul 19, 2006 10:18 pm
by oldBear
Thanks for pointing this out.
I'm doing the same thing with #PB_Any and hadn't reached the limit in any session yet, but it was probably just a matter of time.
Thanks again.
Re: maximum of around 350 images per application??
Posted: Fri Jul 21, 2006 5:45 pm
by Rescator
Jan Vooijs wrote:Hello,
I discovered something strange!
While developing a webcam snap program which copies an image from the clipboard that, there is a maximum of images to 'grab' after around 350 images the programs stops capturing!?!? (no error, no nothing)
Well the solution is to use FreeImage() after the image is 'used' (modified and written to disk in JPG format).
Now the program runs for more than 1500 images so far (every sec one 'shot').
Or just use a constant like #Image_Grabbed instead of #PB_Any
and you can then grab millions with no resource issues at all.
Posted: Fri Jul 21, 2006 6:57 pm
by Jan Vooijs
Well,
To continue the 'saga' it is in the #PB_Any that is true, BUT there is something else, the program runs around 5.3Mb very slowly growing, it run 350sec and grew around 0.2Mb?!?
The memory on the computer is growing fast around 1Mb a second. After the 350 snapshots I used 314Mb of memory so not the aclamed 1Gb! These are bmp's from a webcam in 352x288 format so not soo big.
After the program locks and is 'killed' by me there is still memory lost!! Around 150Mb is still in use!!
Oops!!
Well i used #PB_Any because it was convenient. And because i wanted to do a 'strip' of images inside the program (like in those video editting programs). Must find an other way for this. Anyone a sugestion? An array or sprites?
Jan V.
Posted: Fri Jul 21, 2006 7:54 pm
by dracflamloc
Some of the memory you were not seeing used was actually most likely in swap so your computer wouldnt crash! In the old days your computer wouldn't have been running

Posted: Fri Jul 21, 2006 9:03 pm
by Jan Vooijs
dracflamloc wrote:Some of the memory you were not seeing used was actually most likely in swap so your computer wouldnt crash! In the old days your computer wouldn't have been running

True, but I think this is memory 'claimed' by my program but NOT given back because it crashed.
Jan V.
Posted: Thu Jul 27, 2006 6:55 pm
by Trond
Jan Vooijs wrote:dracflamloc wrote:Some of the memory you were not seeing used was actually most likely in swap so your computer wouldnt crash! In the old days your computer wouldn't have been running

True, but I think this is memory 'claimed' by my program but NOT given back because it crashed.
Jan V.
No, XP cleans very neatly up after crashed programs, in contrast to the Windows 9.x family. Your memory is not lost.
Posted: Fri Jul 28, 2006 1:14 pm
by mskuma
netmaestro wrote:This is happening because when you use #PB_Any, a new image# is being generated but because it's a new and different image# from all existing image#'s, the old one is not getting freed.
I wonder if the same memory leak problem could potentially exist when using #PB_Any for gadgets etc? That is, is it always wise to the Free.. series of functions when using #PB_Any for anything. Thanks.
Posted: Fri Jul 28, 2006 2:24 pm
by Jan Vooijs
Trond wrote:Jan Vooijs wrote:dracflamloc wrote:Some of the memory you were not seeing used was actually most likely in swap so your computer wouldnt crash! In the old days your computer wouldn't have been running

True, but I think this is memory 'claimed' by my program but NOT given back because it crashed.
Jan V.
No, XP cleans very neatly up after crashed programs, in contrast to the Windows 9.x family. Your memory is not lost.
I am afriad that you are wrong. Tested with my second 'wrong' webcam program. I made around 120k of memory (in chunks of 4k) on the heap when the program chrashed (really crashed) afterwards i lost around 98k of memory. Check with a program and found 98k UNCLAIMED memory! After a reboot that memory was gone. Tried it twice same result. Yes win xp is much better in protecting memory but if the program loses it's 'administation' than win xp can no longer claim it back. Although i found a (commercial) program who can.
Jan V.
Posted: Fri Jul 28, 2006 2:37 pm
by Jan Vooijs
mskuma wrote:netmaestro wrote:This is happening because when you use #PB_Any, a new image# is being generated but because it's a new and different image# from all existing image#'s, the old one is not getting freed.
I wonder if the same memory leak problem could potentially exist when using #PB_Any for gadgets etc? That is, is it always wise to the Free.. series of functions when using #PB_Any for anything. Thanks.
It can for every gadget where in a program where '#PB_Any' is used repeated over and over for the SAME gadget, but most for windows and some gadgets not all of them. And the original 'handle' is lost or better gone. it is no longer used inside the program.
Personnally i 'un use' now everytime i use a #PB_Any. Not because PB fails but because I sometimes trick PB and win xp. And i prever not using #PB_Any unless needed. I believe there is no bug but more a side effect. I think when using #PB_Any it was NOT meant to be used in counts of more than say 100. Seeing that other people have likewise problems like in opening files (but not closing them).
Jan V.