Author Topic: Invalid handle error on Screen command after 5 minutes  (Read 7381 times)

0 Members and 1 Guest are viewing this topic.

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Invalid handle error on Screen command after 5 minutes
« on: May 28, 2021, 03:37:03 pm »
My program works for about 5 minutes, and then it dies with
Quote
Unhandled Error #258
Line: 7164 (in main module)
Invalid handle
Continue?

where line 7164 is:
Code: QB64: [Select]
  1.     Screen imgScreen&

I've looked through the code and it isn't doing a _FREEIMAGE or otherwise resetting the image handle (that I can see), so I am not sure what's causing this.

I'm attaching my program, in case anyone would like to take a look or try running it.

To reproduce the error, just choose option 3 or 4 (animation test) and wait. You can move around the screen with the arrow keys and if you get tired of waiting press ESC to exit.

Any help or advice much appreciated!

Screen shot:
  [ You are not allowed to view this attachment ]  

Error message after it runs for 5 minutes:
  [ You are not allowed to view this attachment ]  

The code and tile sheets it uses:
  [ You are not allowed to view this attachment ]  

« Last Edit: May 28, 2021, 03:40:17 pm by madscijr »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Invalid handle error on Screen command after 5 minutes
« Reply #1 on: May 28, 2021, 03:43:35 pm »
I'd recommend doing an _ECHO to a console window periodically and see what the image handle's value is.
Shuwatch!

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Invalid handle error on Screen command after 5 minutes
« Reply #2 on: May 28, 2021, 04:20:15 pm »
Just for fun, I tried running your program with the development build v1.51 5e05664 and error occurs at line 7080 for me (also 28 warnings of unused variables) using menu option 3.

EDIT  and the error came up immediately!
« Last Edit: May 28, 2021, 04:22:42 pm by Richard »

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: Invalid handle error on Screen command after 5 minutes
« Reply #3 on: May 28, 2021, 04:22:45 pm »
I've been looking at the code for half an hour, but I can't see through it. (why don't you use more arrays?)
« Last Edit: May 28, 2021, 04:24:30 pm by MasterGy »

FellippeHeitor

  • Guest
Re: Invalid handle error on Screen command after 5 minutes
« Reply #4 on: May 28, 2021, 04:31:04 pm »
I didn't dive deep enough to understand why you'd need to set SCREEN so often, but just in case try this:

Code: QB64: [Select]
  1. IF imgScreen& < -1 THEN SCREEN imgScreen&

Could it be you're after _DEST instead?

FellippeHeitor

  • Guest
Re: Invalid handle error on Screen command after 5 minutes
« Reply #5 on: May 28, 2021, 04:32:08 pm »
Or even:
Code: QB64: [Select]
  1. IF imgScreen& < -1 THEN SCREEN imgScreen& ELSE EXIT SUB

FellippeHeitor

  • Guest
Re: Invalid handle error on Screen command after 5 minutes
« Reply #6 on: May 28, 2021, 04:35:59 pm »
Digging a bit further:

Code: QB64: [Select]
  1. imgScreen& = _COPYIMAGE(imgBackground&, 32)

That's generating several copies of imgBackground&, without ever freeing the previous iteration. Your program is leaking memory, like a lot. Check Task Manager while it's running.

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: Invalid handle error on Screen command after 5 minutes
« Reply #7 on: May 28, 2021, 05:21:33 pm »
I'd recommend doing an _ECHO to a console window periodically and see what the image handle's value is.
Not a bad idea, thanks.
Dumb question though, how do you open a second console window and _ECHO to it?
I've never done anything like that in QB, but I can see that would be very helpful while debugging.
Thanks again.

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: Invalid handle error on Screen command after 5 minutes
« Reply #8 on: May 28, 2021, 05:28:24 pm »
Just for fun, I tried running your program with the development build v1.51 5e05664 and error occurs at line 7080 for me (also 28 warnings of unused variables) using menu option 3.

EDIT  and the error came up immediately!

I wonder what that's all about... did anything in the language change with that version?
I'm just using this version
Quote
QB64 Version 1.5
Stable Release
From git 3043116


Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: Invalid handle error on Screen command after 5 minutes
« Reply #9 on: May 28, 2021, 05:31:04 pm »
I've been looking at the code for half an hour, but I can't see through it. (why don't you use more arrays?)

Apologies for the ugly code, this is very much a work in progress.
I'm using arrays, is there anything in particular that isn't using arrays but should?
I am very much a novice programmer (even though I've been programming for close to 40 years in one form or another!)
Thanks...

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: Invalid handle error on Screen command after 5 minutes
« Reply #10 on: May 28, 2021, 05:35:55 pm »
Digging a bit further:

Code: QB64: [Select]
  1. imgScreen& = _COPYIMAGE(imgBackground&, 32)

That's generating several copies of imgBackground&, without ever freeing the previous iteration. Your program is leaking memory, like a lot. Check Task Manager while it's running.

Aha! I didn't realize that _COPYIMAGE was actually making a whole new copy,
I thought it was just copying the contents of the source image to the destination image
if it's already instantiated (and would only create a new copy if not).
But we know what assuming does!
What if I put something like
Code: QB64: [Select]
  1. IF imgScreen& < -1 THEN _FREEIMAGE imgScreen&
before that line?

Thank you for the heads up.

FellippeHeitor

  • Guest
Re: Invalid handle error on Screen command after 5 minutes
« Reply #11 on: May 28, 2021, 05:42:50 pm »
I thought of suggesting you to do that call to _freeimage before making the copy, but the problem is exactly the line you're getting an error with now: SCREEN imgScreen& will lock the image as the current display image and will error if you try to free it.

Consider having a canvas image onto which you put all your layers, instead of setting SCREEN over and over.

It's usually 1- set screen once; 2- clear and reuse in a loop; 3- set a new SCREEN only if changes are made to size (if $resize is being used).

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: Invalid handle error on Screen command after 5 minutes
« Reply #12 on: May 28, 2021, 05:51:33 pm »
I thought of suggesting you to do that call to _freeimage before making the copy, but the problem is exactly the line you're getting an error with now: SCREEN imgScreen& will lock the image as the current display image and will error if you try to free it.

I think I have a lot to learn!

I tried
Code: QB64: [Select]
  1. IF imgScreen&< -1 THEN _FREEIMAGE imgScreen&
  2. imgScreen& = _CopyImage(imgBackground&, 32)
  3.  

and it failed, because it locked the image like you said.

This code doesn't blow up
Code: QB64: [Select]
  1. IF imgScreen&< -1 THEN
  2.         SCREEN imgBackground&
  3.         _Dest imgBackground&
  4.         _FREEIMAGE imgScreen&
  5. imgScreen& = _CopyImage(imgBackground&, 32)
  6. _Dest imgScreen&
  7.  

but it looks awfully clunky.

Quote
Consider having a canvas image onto which you put all your layers, instead of setting SCREEN over and over.

It's usually 1- set screen once; 2- clear and reuse in a loop; 3- set a new SCREEN only if changes are made to size (if $resize is being used).

I am so clueless. What's a canvas image? I thought they were all just images?
The reason I set SCREEN above, is to simply unlock the imgScreen& so I can _FREEIMAGE it, so when I copy the layers onto it, there aren't all those copies.
I am really confused.

Would you maybe be able to post a simple example or point me to one?

Thanks again

UPDATE:
Maybe I am using _CopyImage when I really need to be using _PutImage ... When I get back on the computer I will look that up. Thank you all for being so kind and patient with one so thick headed.

UPDATE #2:
Yep, _PutImage was what I should have been using. Thanks to OldMoses for explaining!
« Last Edit: May 29, 2021, 12:06:05 am by madscijr »

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: Invalid handle error on Screen command after 5 minutes
« Reply #13 on: May 28, 2021, 06:03:01 pm »
Your program is leaking memory, like a lot. Check Task Manager while it's running.

Well, at least it isn't crashing after putting in the _FREEIMAGE code.
The amount of memory it's taking up is holding steady at 106 MB in Task Manger.
Thanks for your help!

I still don't understand the difference between a canvas and an image, or how to copy something to the screen without doing _FREEIMAGE on the screen's image handle first (so it doesn't make a new copy) and temporarily changing SCREEN to a different image handle (so it isn't locked up when I do the _FREEIMAGE), but at least the prog isn't leaking memory and crashing anymore.
« Last Edit: May 28, 2021, 06:05:04 pm by madscijr »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Invalid handle error on Screen command after 5 minutes
« Reply #14 on: May 28, 2021, 06:09:27 pm »
Not a bad idea, thanks.
Dumb question though, how do you open a second console window and _ECHO to it?
I've never done anything like that in QB, but I can see that would be very helpful while debugging.
Thanks again.

To make a console in QB64 you need to use the metacommand $CONSOLE. You toggle it ON or OFF with _CONSOLE ON or _CONSOLE OFF. _ECHO allows you to output to the window. So _ECHO "Window Handle: "; STR$(imgScreen&)
Shuwatch!