maximum of around 350 images per application??

Share your advanced PureBasic knowledge/code with the community.
Jan Vooijs
Enthusiast
Enthusiast
Posts: 196
Joined: Tue Sep 30, 2003 4:32 pm
Location: The Netherlands

maximum of around 350 images per application??

Post 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.
Life goes to Fast, Enjoy!!

PB 4 is to good to be true, wake up man it is NOT a dream THIS is a reality!!!

AMD Athlon on 1.75G, 1Gb ram, 160Gb HD, NVidia FX5200, NEC ND-3500AG DVD+RW and CD+RW, in a Qbic EO3702A and Win XP Pro SP2 (registered)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Post 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)
oldBear
Enthusiast
Enthusiast
Posts: 121
Joined: Tue Jul 05, 2005 2:42 pm
Location: berrypatch

Post 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.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: maximum of around 350 images per application??

Post 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.
Jan Vooijs
Enthusiast
Enthusiast
Posts: 196
Joined: Tue Sep 30, 2003 4:32 pm
Location: The Netherlands

Post 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.
Life goes to Fast, Enjoy!!

PB 4 is to good to be true, wake up man it is NOT a dream THIS is a reality!!!

AMD Athlon on 1.75G, 1Gb ram, 160Gb HD, NVidia FX5200, NEC ND-3500AG DVD+RW and CD+RW, in a Qbic EO3702A and Win XP Pro SP2 (registered)
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post 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 ;)
Jan Vooijs
Enthusiast
Enthusiast
Posts: 196
Joined: Tue Sep 30, 2003 4:32 pm
Location: The Netherlands

Post 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.
Life goes to Fast, Enjoy!!

PB 4 is to good to be true, wake up man it is NOT a dream THIS is a reality!!!

AMD Athlon on 1.75G, 1Gb ram, 160Gb HD, NVidia FX5200, NEC ND-3500AG DVD+RW and CD+RW, in a Qbic EO3702A and Win XP Pro SP2 (registered)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
mskuma
Enthusiast
Enthusiast
Posts: 573
Joined: Sat Dec 03, 2005 1:31 am
Location: Australia

Post 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.
Jan Vooijs
Enthusiast
Enthusiast
Posts: 196
Joined: Tue Sep 30, 2003 4:32 pm
Location: The Netherlands

Post 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.
Life goes to Fast, Enjoy!!

PB 4 is to good to be true, wake up man it is NOT a dream THIS is a reality!!!

AMD Athlon on 1.75G, 1Gb ram, 160Gb HD, NVidia FX5200, NEC ND-3500AG DVD+RW and CD+RW, in a Qbic EO3702A and Win XP Pro SP2 (registered)
Jan Vooijs
Enthusiast
Enthusiast
Posts: 196
Joined: Tue Sep 30, 2003 4:32 pm
Location: The Netherlands

Post 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.
Life goes to Fast, Enjoy!!

PB 4 is to good to be true, wake up man it is NOT a dream THIS is a reality!!!

AMD Athlon on 1.75G, 1Gb ram, 160Gb HD, NVidia FX5200, NEC ND-3500AG DVD+RW and CD+RW, in a Qbic EO3702A and Win XP Pro SP2 (registered)
Post Reply