Game crashing but it didn't before (SOLVED)

Hello,

sorry for the title but I don’t know how to name my problem in a better way :slight_smile:. Basically what happened is that I had my prototype of a game working on PC and on my Android phone., then I made some changes to the code and it started crashing at a very specific point.

How to reproduce:

  • Launch the game
  • Click “Start”
  • Die (you can control the spaceship with your finger, just wait for the enemy ships to come from the top and get hit 4 times by their projectiles)
  • On “Game Over” screen click “Continue”
  • Click “Start” again
  • CRASH!

I thought it was some of my code doing it so I rerolled my code to the last version when it worked and the crash still happens at the exact same point. I tried to bundle on two different computers using Defold version 1.2.171. I don’t know what is causing the error because the crash dump is incomprehensible to me. If any of you have any suggestion I would be really grateful.

Here is the crash error message:

ERROR:CRASH: CALL STACK:

 0 0x7FF6769DA790 _snscanf_s <unknown>:0
 1 0x369A70 alcCreateContext <unknown>:0
 2 0x7FFB5E99FD00 UnhandledExceptionFilter <unknown>:0
 3 0x7FFB619C2F80 memset <unknown>:0
 4 0x7FFB619AC5C0 __C_specific_handler <unknown>:0
 5 0x7FFB619C10B0 __chkstk <unknown>:0
 6 0x7FFB61989E70 RtlRaiseException <unknown>:0
 7 0x7FFB619BFE10 KiUserExceptionDispatcher <unknown>:0
 8 0x7FF6769817E0 printf <unknown>:0
 9 0x7FF676980000 Ordinal0 <unknown>:0
10 0x7FF6769817E0 printf <unknown>:0
11 0x7FF6769C52F0 _snprintf <unknown>:0
12 0x7FF6769DA790 _snscanf_s <unknown>:0
13 0x7FF676A07F40 dmHashReverseSafe64 <unknown>:0
14 0x7FF676A07F40 dmHashReverseSafe64 <unknown>:0
15 0x7FF676A07F40 dmHashReverseSafe64 <unknown>:0
16 0x7FF676A07F40 dmHashReverseSafe64 <unknown>:0
17 0x7FF676ABBFE0 Particle_Hash <unknown>:0
18 0x7FF676A07F40 dmHashReverseSafe64 <unknown>:0
19 0x7FF676A07F40 dmHashReverseSafe64 <unknown>:0
20 0x7FF676A07F40 dmHashReverseSafe64 <unknown>:0
21 0x7FF676A07F40 dmHashReverseSafe64 <unknown>:0
22 0x7FF676A07F40 dmHashReverseSafe64 <unknown>:0
23 0x7FF6769B7C40 sprintf <unknown>:0
24 0x7FF676A07F40 dmHashReverseSafe64 <unknown>:0
25 0x7FF676A07F40 dmHashReverseSafe64 <unknown>:0
26 0x7FF6769B7C40 sprintf <unknown>:0
27 0x7FF676C07F10 LZ4MaxCompressedSize <unknown>:0
28 0x7FFB61027BC0 BaseThreadInitThunk <unknown>:0
29 0x7FFB6198CE30 RtlUserThreadStart <unknown>:0

I have a repository which crashes for me here: https://bitbucket.org/VojtechKlos/defold-crash-repo/src/master/

Edit: The last working version before it started crashing is in the Master branch. The develop branch contains the code after which it started crashing.

Note: This is my first ever game I am very new to game programming and LUA so excuse my code :wink:

Thanks to anyone who would have any suggestion!

1 Like

Looks like it’s happening somewhere in the init function of the powerup.gui_script, if that helps.

A print at the top of the function will print before the crash. A print at the end of it will not.

Still looking…I would guess it has something to do with globals somewhere, but I really have no idea.

comment out

table.remove(globals.powerups.available, rand1)

and

table.remove(globals.powerups.available, rand2)

and it won’t crash. Don’t ask me why :joy:

2 Likes

That did it! Thank you!

Weird thing is I haven’t touched globals or the powerups script at all in last couple of iterations… Weird… But anyway thank you imensely!

Edit: I did in fact touched the globals but nothing serious. In fact it was working as it was before.

It seems to crash in the pcg_boundedrand_r() function:

(from lldb):

(lldb) bt
* thread #1, name = 'engine_main', queue = 'com.apple.main-thread', stop reason = EXC_ARITHMETIC (code=EXC_I386_DIV, subcode=0x0)
  * frame #0: 0x00000001000015c6 dmengine`::pcg32_boundedrand_r(rng=0x000000010064c0f0, bound=0) at pcg_basic.c:95:33
    frame #1: 0x0000000100001b74 dmengine`range(L=0x000000010af1e380) at pcgrandom.cpp:71:11
    frame #2: 0x0000000100211dd5 dmengine`lj_BC_FUNCC + 68
    frame #3: 0x0000000100228cd2 dmengine`lua_pcall + 146
    frame #4: 0x00000001001ed399 dmengine`dmScript::PCallInternal(L=0x000000010af1e380, nargs=1, nresult=-1, in_error_handler=0) at script.cpp:1251:22
...

And, by monkey patching the rnd.range function, it seems it gets a weird range “[1, 0]”. Since the extension is using unsigned ints, I’m guessing something goes bad in that function.

local old_fn = rnd.range

local function new_range(a, b)
	print("MAWE range:", a, b)
	return old_fn(a, b)
end
rnd.range = new_range

Let’s ping @selimanac, and maybe he can take a look at this?

2 Likes

So, by making sure you don’t pass in an invalid range seems to help.
You can do that in the monkey patched function:

local old_fn = rnd.range
local function new_range(a, b)
	if b < a then
		b = a
	end
	return old_fn(a, b)
end
rnd.range = new_range
1 Like

I didn’t realize rnd.range was a thing, tbh.

If you switch to math.random, you get a Lua error instead of a crash that seems to indicate your table is empty (so range 1, 0)

if you switch to math.random(1,2) everything works as it should

It’s a thing if you use the library defold-random by @selimanac:

Yes, having Lua errors from this module would be a good improvement.

I guess that this problem is partially on my end since I am not checking if the array is empty before taking its length and placing it in the range method. But as Mathias said it would be nice to have an actual Lua error instead of a crash.

OK, I’m going to add this.

2 Likes

Yeah. I generally try to wrap any reference to a dynamic table that uses the length with

If #table >= 1 then
   Whatever
End

I can’t remember if #table returns 0 or nil if it’s empty. Could use ~= 0 or ~= nil instead, depending.

Lua error message is added. You can check it out on master or v1.2.2

5 Likes

It returns 0 if the table is empty

1 Like

Thanks I will check it out later today! Thank you everyone for your help!

1 Like

BTW it works well. Didn’t have time to respond earlier.

2 Likes