Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
_WIDTH(_LOADIMAGE()) Ok?
#1
I recently saw some code that did this:

ImageWidth = _WIDTH(_LOADIMAGE("Image.png", 32))

Will this leave the image's contents in RAM with no way of freeing it?

-or-

Will the image be discarded since the handle was not assigned to a variable?

It's a neat trick and one that I could happen to use on a current project but not if the image is orphaned in RAM.
Reply
#2
If I were you, I'd assign a LONG variable to _LOADIMAGE result. To load an image only to get its width, it's actually not being "used" according to the Wiki description.

From:

https://qb64phoenix.com/qb64wiki/index.php/LOADIMAGE

Quote:It is important to free unused or discarded images with _FREEIMAGE to prevent CPU memory overflow errors.
Reply
#3
Running this program on Manjaro MATE:

Code: (Select All)
dim lh as long, i as long
dim apic$
apic$ = environ$("HOME") + "/Pictures/linux-distros-wikipedia.png"
for i = 1 to 10
    print i
    lh = _width(_loadimage(apic$, 32))
next
print "Press any key to get away..."
do : ke$ = inkey$ : loop until ke$ = ""
do : _limit 20 : ke$ = inkey$ : loop while ke$ = ""
system

For this picture converted by GIMP to PNG file, which is 4.5MB in size:

https://en.wikipedia.org/wiki/Linux_dist...0_2021.svg

Observe the screenshot:

[Image: memory-hog-width-loadimage.png]
Reply
#4
(01-13-2023, 02:07 PM)mnrvovrfc Wrote: If I were you, I'd assign a LONG variable to _LOADIMAGE result. To load an image only to get its width, it's actually not being "used" according to the Wiki description.

From:

https://qb64phoenix.com/qb64wiki/index.php/LOADIMAGE

Quote:It is important to free unused or discarded images with _FREEIMAGE to prevent CPU memory overflow errors.

Yep, I understand all of that. That's why I'm asking if something like the example is advisable or not.
Reply
#5
I have no idea how good the garbage collection / smart pointers / whatever is so there's bound to be someone better versed with the source code to answer this. That said, I would be more tempted to create a helper function if this kind of thing is needed often. Then you'd get the benefits of a simple call to a function that would do the loading and calculation and also clean up after itself.

And, with that, I haven't answered your question at all  Big Grin
RokCoder - dabbling in QB64pe for fun
Reply
#6
Clearly a bad idea IMO. Why load image without getting handle?

It's width is easy from handle.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#7
Can do it with _LoadFont sorta
Code: (Select All)
Screen _NewImage(800, 600, 32)
_Font _LoadFont("arial.ttf", 20)
Print "Hello World"
fh = _Font
Print fh
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#8
(01-13-2023, 01:45 PM)TerryRitchie Wrote: I recently saw some code that did this:

ImageWidth = _WIDTH(_LOADIMAGE("Image.png", 32))

Will this leave the image's contents in RAM with no way of freeing it?

-or-

Will the image be discarded since the handle was not assigned to a variable?

It's a neat trick and one that I could happen to use on a current project but not if the image is orphaned in RAM.

How is one going to _FREEIMAGE that?  As you suspect, chances are that's an image lost in RAM forevermore.

The only reason I say "chances are", is because image handles start at -10 and go downwards from there, so it's always possible some odd coder wrote something like:

FOR i = -10 TO -1000 STEP -1
  _FREEIMAGE i
NEXT
Reply
#9
Thanks for the replies guys. Yeah, I figured this was a bad idea, just wanted to get the opinions of others to make sure. I'm writing some code that scans directories for images and I thought this might be a simple method of getting widths and heights without actually having to load, get the info, then free each image.
Reply
#10
Quote:ImageWidth = _WIDTH(_LOADIMAGE("Image.png", 32))
about this I see another point of weakness, different from the RAM garbage coming because image.pgn lasts in RAM.

If in the _Loadimage action something goes wrong, what arrive to _WIDTH? 
Think about a wrong path, a failure to read the file, a wrong name (that file does not exist).
And what _WIDTH returns?

How to manage this kind of issue
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)