"failed to create bitmap, RGSSError"

Hey,

I recently got a bug report for my game that I'm very puzzled by. Was wondering if anyone had any insight into what kind of a problem would throw this "failed to create bitmap" error, which I've never seen before. The error thrown, using Hime's stacktrace, looks like this:


Message Log Test:267:in `initialize': failed to create bitmap, RGSSError
from Message Log Test:267:in `new'
from Message Log Test:267:in `create_log_contents'
from Message Log Test:386:in `refresh'
from Message Log Test:175:in `initialize'
from Message Log Test:522:in `new'
from Message Log Test:522:in `update'
from Scene_Base:14:in `main'
from SceneManager:23:in `run'
from Main:10:in `block in <main>
When I searched the "failed to create bitmap" error on the web, the only results seemed to be from a corrupted image, as the cache threw the error. In my case, however, there is no cached image being used. It just comes from this method:


Code:
  #--------------------------------------------------------------------------
  # Create Log Contents
  #--------------------------------------------------------------------------
  def create_log_contents(size)
    self.contents.dispose
   self.contents = Bitmap.new(width, [height - padding * 2, size * line_height].max)
  end

Could the size of the bitmap being created be an issue for someone on a weak computer? It really should just be blank at this point, so I can't see what else might cause it.
In this specific case, it is most likely a timing error - and those are difficult to find.

The error itself usually happens when the engine tries to access a picture or a sprite AFTER it had been deleted.
If it had happened to you yourself during development, then it would likely have meant you used some commands in the wrong order.

However if you had already playtested your game through before giving it to players, that means that the order was changed from what you had by a timing problem.
These usually happen if the commands (delete and access after) are in two different events and the timing randomly changed only for that player.

Please follow the link "Bughunting" in my signature, that gives more details on the background and mighthelp you finding the place where it went wrong for that player.
In this specific case, it is most likely a timing error - and those are difficult to find.

The error itself usually happens when the engine tries to access a picture or a sprite AFTER it had been deleted.
If it had happened to you yourself during development, then it would likely have meant you used some commands in the wrong order.

However if you had already playtested your game through before giving it to players, that means that the order was changed from what you had by a timing problem.
These usually happen if the commands (delete and access after) are in two different events and the timing randomly changed only for that player.

Please follow the link "Bughunting" in my signature, that gives more details on the background and mighthelp you finding the place where it went wrong for that player.
Hm, thank you. The game in question is early access, so not 100% finished by any means, but I've definitely playtested it quite extensively, and I did a lot of testing on this feature when implementing it (plus, the issue has only been reported by this specific user so far). I will have a look at the resources you recommended.

For slight context, this bug is in a text backlog feature, which just brings up a window showing recent messages on demand (mostly custom work). The only way to trigger it is by pressing a key. I tried spamming the key as fast as I could to see if that created a timing error, but it didn't for me. I have a pretty fast computer, though. Do you think there's any chance that, for someone with a worse computer, there might be enough time between pressing the button and the menu coming up for a second keypress to register? Especially since it does get slightly laggy when there are a large number of messages to show in the backlog.
yes and no.
lag due to low resources can make timing problematical, but even a halfway decent modern computer has enough resources to have no problems with most such cases.

I suggest asking for the typical computer values like RAM and CPU before automatically assuming it has to be lag.
I suspect the Bitmap never existed in the first place so it failed to dispose of it.
I suspect the Bitmap never existed in the first place so it failed to dispose of it.
Would that throw a failed to create error, though? I have a fair amount of debugging experience from my custom script work, and that would usually throw a NoMethod or NilClass error. Though if you've had similar experiences with disposing, I can add a line to check that.

Also, since I realize I didn't clarify it, line 267 was this one:
self.contents = Bitmap.new(width, [height - padding * 2, size * line_height].max)

I tried some further research with varied search terms, and it seemed like the two causes for this issue people had identified elsewhere were one of the parameters being 0 in create bitmap or the bitmap getting too big, so looking at being sure it can never evaluate with width or height zero (maybe just changing to Graphics.height, since it's a full window log).
Err, where's the link to the full script?
Err, where's the link to the full script?
I was mainly asking if anyone knew what kind of issue would throw the error type in question, so I hadn't posted it. It's kind of a mess to look at it since I took an old script and rewrote the parts that actually do stuff, and I'd rather not ask people to dig through my mess. If the issue persists, I'll clean it up and post it.
Concerning pressing a button and getting it polled twice or more, I can say that it rarely happens. In most cases, it's an issue with having a keyboard and a gamepad connected at the same time. Reconnecting the gamepad solves it. The only way this could become a little headache would be if you're just opening a new window but not creating a new scene. If you did create a new scene as usual, you shouldn't worry about it for it will ignore key presses for a little while.
Concerning pressing a button and getting it polled twice or more, I can say that it rarely happens. In most cases, it's an issue with having a keyboard and a gamepad connected at the same time. Reconnecting the gamepad solves it. The only way this could become a little headache would be if you're just opening a new window but not creating a new scene. If you did create a new scene as usual, you shouldn't worry about it for it will ignore key presses for a little while.
Good to know, thanks. Yeah, the person who had the issue got back to me a few minutes ago and said they only pressed it once anyway.

I think I actually just did find the issue. Going on the assumption that it could be from too much text being stored in the log causing too big a bitmap to be drawn, I went through and stuffed as much text as I possibly could into the log (since it goes by # of messages, some message boxes have a lot of text with word wrap) and got it to fire that way.

One thing I have no experience with is rescuing errors. Doing this shouldn't cause any horrible errors, right?

Code:
  def create_log_contents(size)
    begin
      self.contents.dispose
      self.contents = Bitmap.new(width, [height - padding * 2, size * line_height].max)
      $fallback = false
    rescue
      $fallback = true
      self.contents = Bitmap.new(width, height) 
      text = "Message log error. Please reduce the maximum number of\\br messages setting in the System menu or try again after more dialog."
      draw_text_ex(0, 0, text)
    end
  end
Take a look at my post in Ren_desuku's help thread. It's for RMXP but some principles are also valid for RMVX ACE.
There I showed Ren how to stuff several lines of text but it will only show a few of them at any given time.
You could get some ideas on how to add a similar feature in your backlog script to make sure it never grows too large again.
Take a look at my post in Ren_desuku's help thread. It's for RMXP but some principles are also valid for RMVX ACE.
There I showed Ren how to stuff several lines of text but it will only show a few of them at any given time.
You could get some ideas on how to add a similar feature in your backlog script to make sure it never grows too large again.
Hmm, not a bad approach if I'm interpreting it right. You'd redraw what's on the screen with different text when they scroll rather than making it all be drawn then letting you scroll, correct? I may look into something like that as a future long-term solution.

My implementation of drawing text is a little complicated (essentially I reconstructed draw_text_ex with a wordwrap script, went through it without drawing anything to measure the needed space, then went through it again to draw, all in the service of a log that keeps faces and text codes plus handles the oversized messages that word wrap allows for; see attached), so I'll use this stopgap for now to be sure no one has any more issues and then look at that at a later time.
Good error report. Invalid arguments is the most likely sinner.

Are you able to reproduce the error on your own?
If you can then you can write the arguments to the console
Ruby:
    self.contents.dispose
    w, h = width, [height - padding * 2, size * line_height].max
    print("Bitmap.new(#{w.inspect}, #{h.inspect})\n")
    self.contents = Bitmap.new(w, h)

Maybe one of them is 0, maybe negative. Maybe size or line_height got messed up somewhere and you end up trying to do something like Bitmap.new(640, 1000000)
Maybe you have a memory leak elsewhere and the tipping point ended up being in some otherwise fine code.

If you can't reproduce the error I recommend taking your fallback version add some code which logs the error + arguments in a text file. Player submitted logs can be tremendously helpful in debugging efforts.

Good hunting
*hugs*
- Zeriab
What can also cause this is an extended play session in a game with a memory leak bug. This is most commonly caused by sprites being created but not disposed, so every time their associated scene is accessed it ends up with X more sprites held in memory, where X is the number that haven't been disposed after creation, and it eventually reaches a point where so much memory is taken up that there's none left to create the bitmap again.
@Zeriab - I suspect it can actually get close to the 10k case naturally in some rare cases (for example, I have some books in game that have a lot of text within a few message boxes; these were what I used to reproduce the error, though I doubt anyone would be reading the log with 10+ copies of that in their backlog naturally, of course, but perhaps a smaller about of text could cause it on a weaker system, or in concert with the issue mentioned by Trihan.

@Trihan - Ah yes, I recall this issue now that you mention it, and the script that can be used to check for them. I'll investigate if any of those slipped back into the game since last time I ran the test for that, since I'm always adding/messing with things. I could trigger the bug with the oversized bitmap from a fresh load pretty quickly, but if there's something like that in the background I'm sure it could make it happen much more easily.
@Zeriab - I suspect it can actually get close to the 10k case naturally in some rare cases (for example, I have some books in game that have a lot of text within a few message boxes; these were what I used to reproduce the error, though I doubt anyone would be reading the log with 10+ copies of that in their backlog naturally, of course, but perhaps a smaller about of text could cause it on a weaker system, or in concert with the issue mentioned by Trihan.

@Trihan - Ah yes, I recall this issue now that you mention it, and the script that can be used to check for them. I'll investigate if any of those slipped back into the game since last time I ran the test for that, since I'm always adding/messing with things. I could trigger the bug with the oversized bitmap from a fresh load pretty quickly, but if there's something like that in the background I'm sure it could make it happen much more easily.
I actually had a pretty egregious example of it when investigating the issue for the developer of an upcoming commercial game. There were several memory leaks, but the worst was with the enemy HP icons. There's a script that adds skull icons to enemies, and each skull represents 20% HP. So 5 skulls per enemy per battle. There are 400ish enemies in the game. The skull icon sprites weren't being disposed.

So just imagine how many floating sprite instances there were in memory by the end of the game...you have no idea how much faster it ran once I eliminated the sprite leaks.
I actually had a pretty egregious example of it when investigating the issue for the developer of an upcoming commercial game. There were several memory leaks, but the worst was with the enemy HP icons. There's a script that adds skull icons to enemies, and each skull represents 20% HP. So 5 skulls per enemy per battle. There are 400ish enemies in the game. The skull icon sprites weren't being disposed.

So just imagine how many floating sprite instances there were in memory by the end of the game...you have no idea how much faster it ran once I eliminated the sprite leaks.

Oh gods XD Yeah, I can just imagine. I'll probably do a full runthrough of the recent content with Mithran's script in place sometime soon to see if I've managed to do anything like that, haha.
If your scene needs to load too many sprites or bitmaps, you could call Cache.clear at the end of your scene, like inside the terminate method.

Original thread: https://forums.rpgmakerweb.com/threads/failed-to-create-bitmap-rgsserror.162695/