Author Topic: _Freeimage question  (Read 3574 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • View Profile
    • 40wattstudio
_Freeimage question
« on: March 03, 2020, 09:36:41 pm »
I've been using _Loadimage quite a bit and from reading the Wiki it sounds like one should preferably use it in conjunction with _Freeimage to prevent memory errors.

_Freeimage seems pretty easy to implement, but how does one test to see that it's actually working?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: _Freeimage question
« Reply #1 on: March 03, 2020, 09:38:17 pm »
Easiest way is just to watch memory usage in Task Manager for your program.  Without it, you’ll have a memory leak and resources will increase endlessly until your program crashes.  (Depending on program flow, of course.  Loading an image once without freeing it won’t crash a program, but to load an image inside a loop repeatedly, definitely will.)
« Last Edit: March 03, 2020, 09:40:03 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • View Profile
    • 40wattstudio
Re: _Freeimage question
« Reply #2 on: March 03, 2020, 09:42:46 pm »
Ok that sounds pretty simple. Is there a way to monitor it in-program as well, like by printing to the screen a list of loaded and freed images?

Marked as best answer by 40wattstudio on March 05, 2020, 04:36:11 pm

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: _Freeimage question
« Reply #3 on: March 04, 2020, 01:05:09 am »
Ok that sounds pretty simple. Is there a way to monitor it in-program as well, like by printing to the screen a list of loaded and freed images?

You might be able to monitor the image handles.  Every image has an unique handle which we use to reference it in memory (Handle = _LOADIMAGE(...stuff)).

I *believe* that the first image handle usually starts around -11 and increases by -1 for each image loaded after that.  (I’m not at my PC currently, so I can’t test to be certain.)

So load one image, it’s handle value is usually -11.  The next image handle value is -12.  Then -13....

Free an image and those handles should be recycled when you load a new one.

First image loaded has a handle of -11.   Second is -12.  Third is -13....

Now, FREE the second image from memory (-12), LOAD a new image, and it *should* recycle that freed handle and have a value of -12...



So let’s say I’m loading up to 10 images in memory.  IF I’m loading and freeing them properly, as simple check of something like this would make an easy warning:

SUB ValidateHandle (Handle AS LONG)
    IF Handle < -100 THEN
        CLS
        PRINT “IMAGE MEMORY LEAK IN PROCESS!!”
        END
    END IF
END SUB

An endless loading and unloading of images would generate more than -100 handles, trip the IF, and report itself as an error for you.

**At least, I think it should work.  As I said above, I’m not at the PC currently for 100% testing.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!