Skip to content

Commit

Permalink
image: Fix crash when Image property is nil pointer. #18
Browse files Browse the repository at this point in the history
  • Loading branch information
roblillack committed Jun 29, 2024
1 parent da85091 commit 39f6476
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ui/image_cocoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ func (c *Image) draw() {

switch img := c.Image.(type) {
case *image.RGBA:
c.ref.SetImage(img)
if img != nil {
c.ref.SetImage(img)
}
default:
panic(fmt.Sprintf("unsupported image type: %T", img))
}
Expand Down
2 changes: 1 addition & 1 deletion ui/image_fltk.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (c *Image) handleEvent(e goFltk.Event) bool {
}

func (c *Image) draw() {
if c.ref == nil {
if c.ref == nil || isImageNil(c.Image) {
return
}

Expand Down
19 changes: 19 additions & 0 deletions ui/imgutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ui

import (
"image"
"reflect"
)

func isImageNil(i image.Image) bool {
if i == nil {
return true
}

switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(i).IsNil()
}

return false
}

0 comments on commit 39f6476

Please sign in to comment.