Gopherjs: Help understanding crash -- Uncaught Error: runtime error: invalid memory address or nil pointer dereference

Created on 16 Aug 2016  路  3Comments  路  Source: gopherjs/gopherjs

I am getting this crash.
However, instance.go:247 couldn't possibly hit a nil pointer (see below).
How can I further troubleshoot how is gopherjs hitting a bad pointer from my Golang code?

Uncaught Error: runtime error: invalid memory address or nil pointer dereference
$callDeferred @ negoc.js:1405
$panic @ negoc.js:1444
$throwRuntimeError @ runtime.go:27
$throwNilPointerError @ negoc.js:29
target.(anonymous function) @ negoc.js:883
createInstance @ instance.go:247
dispatch @ dispatch.go:88
handleWebsocket @ websocket.go:132
fun @ negoc.js:1466
$goroutine @ negoc.js:1464
$runScheduled @ negoc.js:1505

See below that instance.go:247 can't possibly hit a nil pointer:

237 mod := shader.findModel(modelName)
238 if mod == nil {
239     log(fmt.Sprintf("createInstance: id=%s program=%s model=%s not found", id, programName, modelName))
240     mod = newModel(shader, modelName, gameInfo.gl, objURL, f, u, gameInfo.assetPath, gameInfo.textureTable, repeat, gameInfo.materialLib, gameInfo.extensionUintIndexEnabled)
241     if mod == nil {
242         log(fmt.Sprintf("createInstance: id=%s program=%s failure creating model=%s", id, programName, modelName))
243         return
244     }
245 }
246
247 inst := mod.findInstance(id)

Please advise.

question

All 3 comments

I have added debug code right before the crash as below:

245 var trueNil model
246 log(fmt.Sprintf("createInstance: id=%s program=%s model=%s newModel=%v newModelIsNil=%v (trueNil=%v trueNilIsNil=%v)", id, programName, modelName, mod, mod == nil, trueNil, trueNil == nil))
247
248 inst := mod.findInstance(id)

The debug code at line 246 above issued the following message:

negoc: createInstance: id=m:4 program=p:simpleTexturizer model=o:old_house newModel=<nil> newModelIsNil=false (trueNil=<nil> trueNilIsNil=true)

How is it possible that 'mod' is printed as 'nil' while 'mod==nil' returned false?

The nil pointer dereference is not happening on instance.go:247, it's happening _inside_ of mod.findInstance(id).

Look at the middle line:

$throwNilPointerError @ negoc.js:29
target.(anonymous function) @ negoc.js:883
createInstance @ instance.go:247

But it looks like it's happening because mod - the receiver value - is nil.

How is it possible that 'mod' is printed as 'nil' while 'mod==nil' returned false?

What type is model? If it's an interface, then it might be a nil pointer to struct that implements the interface. E.g., see https://play.golang.org/p/TJJ2EOgxIX.

Try printing its type, like:

fmt.Printf("mod value = %v, mod type = %T\n", mod, mod)

What type is model? If it's an interface, then it might be a nil pointer to struct that implements the interface. E.g., see https://play.golang.org/p/TJJ2EOgxIX

That was it.
model is an interface type.
Thanks for the clarification, and sorry for the noise.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

canning-duck picture canning-duck  路  3Comments

hackwaly picture hackwaly  路  4Comments

DapperDodo picture DapperDodo  路  6Comments

j7b picture j7b  路  4Comments

marwan-at-work picture marwan-at-work  路  4Comments