Image gadget Problem

Windows specific forum
Prof
User
User
Posts: 20
Joined: Thu May 17, 2007 11:37 pm

Image gadget Problem

Post by Prof »

Hello everyone.

I would be gratgeful for a little help if someone has the time.

My problem is that I am designing a database and one of the fields I am using is an imagegadget to hold someones photo. The problem I have is that when I move from record to record, the new photo's load in OK but it eats loads of memory because I am having to use loadimage() and pass the handle to the imagegadget. I am only using 100x100 images but I am getting a severe memory leak every time I click to view the next record.

What would be really handy for me is a solution so that I can load an image straight into an image gadget (or something similar) without having to load the image into memory first.

Has anyone got a solution for this? Or is this a case of RTFM?

Thanks, Prof.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Are you using #PB_Any with LoadImage?

e.g. image = LoadImage(#PB_Any, ...)

or imageid = LoadImage(#image, ...)

:?:

A bit of code snippet would be of help.
BERESHEIT
Prof
User
User
Posts: 20
Joined: Thu May 17, 2007 11:37 pm

Post by Prof »

Sorry, here is a little code....

I am loading a default image in here first and creating the gadget...

Code: Select all

 Photo=LoadImage(#PB_Any, "DATA\student_photos\no_photo.bmp")
  Photo_Gadget=ImageGadget(#PB_Any, 350, 40, 150, 150, ImageID(Photo),#PB_Image_Border )
Here is where I am iterating through the records and loading in a new image....

Code: Select all

  Image_File$="DATA\Student_Photos\"+Str(Student_Record_Number)+".bmp"
    Result=FileSize(Image_File$)
    If Result=-1
       Image_File$="DATA\student_photos\no_photo.bmp"
    EndIf
    Photo=LoadImage(#PB_Any, Image_File$)
    SetGadgetState(Photo_Gadget, ImageID(Photo)) 
It works just fine but it eats loads of memory :/

Am I doing something wrong?

Prof.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

When you do imageid = LoadImage(0, ...) the last image that was in image# 0 is freed each time a new one is assigned to it. With #PB_Any, this doesn't happen. Each time you assign a new image# to the variable, it is a new and different image# generated by #PB_Any, and the last one is not freed.

To solve it, do this:

Code: Select all

If IsImage(Photo)
  FreeImage(Photo)
Endif
Photo = LoadImage(#PB_Any, ...)
and your memory leak is gone.
BERESHEIT
Prof
User
User
Posts: 20
Joined: Thu May 17, 2007 11:37 pm

Post by Prof »

netmaestro.....

Please slap me around the face with a lump of dog poo for being so stupid.

Your solution works a treat! Thanks!

I want to have your babies. Well, erm, maybe not.

Prof.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Don't feel too badly, the same thing happens to lots of people who are inexperienced with using #PB_Any. It's kind of a rite of passage on your way to mastering the language. You can tick this one off now and go on to the next trap. :)
BERESHEIT
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

One more thing I forgot to mention, you should initialize the Photo variable to -1 or something so that its initial value doesn't get shared with any existing images with static image#'s. For example, if you had an existing image 0 and the Photo variable started at 0, the first time you did the 'If IsImage' test, you would free image 0. Which presumably isn't a desired effect. If it starts at -1, it's safe to mix static image#'s with #PB_Any's.
BERESHEIT
Prof
User
User
Posts: 20
Joined: Thu May 17, 2007 11:37 pm

Post by Prof »

Thanks very much for your help on this issue. I am impressed with the level of help that you have given me. It is very much appreciated.

Regards, Prof.
Post Reply