go-qml / qml

QML support for the Go language
Other
1.96k stars 189 forks source link

QML engine GCs resources after receiving reference #68

Closed quarnster closed 9 years ago

quarnster commented 10 years ago

Try running this test:

package qml_test

import (
    "gopkg.in/qml.v1"
    "testing"
    "time"
)

func init() {
    qml.SetupTesting()
}

type (
    Blah struct {
        A int
    }
    dummy2 struct {
        b Blah
    }

    dummy struct {
        d dummy2
    }
)

func (d *dummy2) B() Blah {
    return d.b
}

func (d *dummy) A() *dummy2 {
    return &d.d
}

func TestBlah(t *testing.T) {
    //  qml.Init(nil)

    f := func() error {
        e := qml.NewEngine()
        defer e.Destroy()
        var d dummy
        e.Context().SetVar("d", &d)
        c, err := e.LoadString("blah.qml", `
import QtQuick 2.0
Item {
    Timer {
        objectName: "ti";
        interval: 10
        running: true
        repeat: true
        onTriggered: {
            running = false;
            console.log(d);
            var a = d.a();
            console.log(a);
            var b = a.b(0);
            console.log(b.a);
            running = true;
        }
    }
}
`)
        if err != nil {
            return err
        }
        w := c.Create(nil)
        defer w.Destroy()
        o := w.ObjectByName("ti") //.ObjectByName("ti")
        defer o.Destroy()
        failed := false
        for i := 0; i < 30; i++ {
            if b, ok := o.Property("running").(bool); !b && ok {
                failed = true
                break
            }
            time.Sleep(time.Second)
        }
        if failed {
            t.FailNow()
        }
        return nil
    }
    err := f()
    if err != nil {
        t.Error(err)
    }
}

I'm receiving a long about 3 seconds worth of the expected output and then all of a sudden:

2014/05/26 12:45:42 blah.qml:13: dummy2(0x5f0c820)
2014/05/26 12:45:42 blah.qml:15: 0
2014/05/26 12:45:42 blah.qml:11: dummy(0x6d179f0)
2014/05/26 12:45:42 blah.qml:13: dummy2(0x5f0c820)
2014/05/26 12:45:42 blah.qml:15: 0
2014/05/26 12:45:42 blah.qml:11: dummy(0x6d179f0)
2014/05/26 12:45:42 blah.qml:13: dummy2(0x5f0c820)
2014/05/26 12:45:42 blah.qml:15: 0
2014/05/26 12:45:42 blah.qml:11: dummy(0x6d179f0)
2014/05/26 12:45:42 blah.qml:13: dummy2(0x5f0c820)
2014/05/26 12:45:42 blah.qml:15: 0
2014/05/26 12:45:42 blah.qml:11: dummy(0x6d179f0)
2014/05/26 12:45:42 blah.qml:13: dummy2(0x5f0c820)
2014/05/26 12:45:42 blah.qml:15: 0
2014/05/26 12:45:42 blah.qml:11: dummy(0x6d179f0)
2014/05/26 12:45:42 blah.qml:13: null
2014/05/26 12:45:42 blah.qml:14: file:////private/tmp/blah.qml:14: TypeError: Cannot call method 'b' of null
--- FAIL: TestBlah (3.01 seconds)
FAIL
exit status 1
FAIL    _/private/tmp   3.120s

If I don't fail immediately, it'll output the correct data again for a bit until it fails the next time.

Occurs in both v0 and v1

quarnster commented 10 years ago

Panicing variant when trying to use the return value of d.a as a function parameter:

package qml_test

import (
    "gopkg.in/qml.v1"
    "testing"
    "time"
)

func init() {
    qml.SetupTesting()
}

type (
    Blah struct {
        A int
    }
    dummy2 struct {
        b Blah
    }

    dummy struct {
        d dummy2
    }
)

func (d *dummy2) B() Blah {
    return d.b
}

func (d *dummy) C(*dummy2) {
}
func (d *dummy) A() *dummy2 {
    return &d.d
}

func TestBlah(t *testing.T) {
    //  qml.Init(nil)

    f := func() error {
        e := qml.NewEngine()
        defer e.Destroy()
        var d dummy
        e.Context().SetVar("d", &d)
        c, err := e.LoadString("blah.qml", `
import QtQuick 2.0
Item {
    Timer {
        objectName: "ti";
        interval: 10
        running: true
        repeat: true
        onTriggered: {
            running = false;
            console.log(d);
            var a = d.a();
            console.log(a);
            d.c(a);
            var b = a.b(0);
            console.log(b.a);
            running = true;
        }
    }
}
`)
        if err != nil {
            return err
        }
        w := c.Create(nil)
        defer w.Destroy()
        o := w.ObjectByName("ti") //.ObjectByName("ti")
        defer o.Destroy()
        failed := false
        for i := 0; i < 30; i++ {
            if b, ok := o.Property("running").(bool); !b && ok {
                failed = true
                break
            }
            time.Sleep(time.Second)
        }
        if failed {
            t.FailNow()
        }
        return nil
    }
    err := f()
    if err != nil {
        t.Error(err)
    }
}

Output:

2014/05/26 12:59:08 blah.qml:11: dummy(0x6e0ed60)
2014/05/26 12:59:08 blah.qml:13: dummy2(0x6837820)
2014/05/26 12:59:08 blah.qml:16: 0
2014/05/26 12:59:08 blah.qml:11: dummy(0x6e0ed60)
2014/05/26 12:59:08 blah.qml:13: dummy2(0x6837820)
2014/05/26 12:59:08 blah.qml:16: 0
2014/05/26 12:59:08 blah.qml:11: dummy(0x6e0ed60)
2014/05/26 12:59:08 blah.qml:13: dummy2(0x6837820)
2014/05/26 12:59:08 blah.qml:16: 0
2014/05/26 12:59:08 blah.qml:11: dummy(0x6e0ed60)
2014/05/26 12:59:08 blah.qml:13: null
panic: reflect: call of reflect.Value.Type on zero Value

goroutine 16 [running]:
runtime.panic(0x41a97c0, 0xc2080a4bc0)
    /Users/quarnster/code/3rdparty/go/src/pkg/runtime/panic.c:279 +0xf5
reflect.Value.Type(0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
    /Users/quarnster/code/3rdparty/go/src/pkg/reflect/value.go:1836 +0x92
gopkg.in/qml%2ev1.hookGoValueCallMethod(0x6d21e50, 0xc208030360, 0x7fff00000001, 0x7fff5fbfc650)
    /Users/quarnster/code/go/src/gopkg.in/qml.v1/bridge.go:506 +0x31b
gopkg.in/qml%2ev1._Cfunc_applicationExec(0x41b6b60)
    gopkg.in/qml.v1/_obj/_cgo_defun.c:71 +0x31
gopkg.in/qml%2ev1.Run(0x0, 0x422fa78, 0x0, 0x0)
    /Users/quarnster/code/go/src/gopkg.in/qml.v1/bridge.go:68 +0x1b1
gopkg.in/qml%2ev1.qmain()
    /Users/quarnster/code/go/src/gopkg.in/qml.v1/testing.go:17 +0x30

goroutine 19 [finalizer wait]:
runtime.park(0x4064ab0, 0x42d6bf8, 0x42caec9)
    /Users/quarnster/code/3rdparty/go/src/pkg/runtime/proc.c:1354 +0x89
runtime.parkunlock(0x42d6bf8, 0x42caec9)
    /Users/quarnster/code/3rdparty/go/src/pkg/runtime/proc.c:1370 +0x3b
runfinq()
    /Users/quarnster/code/3rdparty/go/src/pkg/runtime/mgc0.c:2624 +0xcf
runtime.goexit()
    /Users/quarnster/code/3rdparty/go/src/pkg/runtime/proc.c:1430

goroutine 17 [syscall]:
runtime.goexit()
    /Users/quarnster/code/3rdparty/go/src/pkg/runtime/proc.c:1430

goroutine 20 [chan receive]:
testing.RunTests(0x422fb10, 0x42c3300, 0x1, 0x1, 0x1)
    /Users/quarnster/code/3rdparty/go/src/pkg/testing/testing.go:504 +0x917
testing.Main(0x422fb10, 0x42c3300, 0x1, 0x1, 0x42de540, 0x0, 0x0, 0x42de540, 0x0, 0x0)
    /Users/quarnster/code/3rdparty/go/src/pkg/testing/testing.go:435 +0x87
main.main()
    _/private/tmp/_test/_testmain.go:47 +0x9c
gopkg.in/qml%2ev1.tmain()
    /Users/quarnster/code/go/src/gopkg.in/qml.v1/testing.go:20 +0x1a
gopkg.in/qml%2ev1.func·035(0x0, 0x0)
    /Users/quarnster/code/go/src/gopkg.in/qml.v1/testing.go:17 +0x2c
gopkg.in/qml%2ev1.func·002()
    /Users/quarnster/code/go/src/gopkg.in/qml.v1/bridge.go:65 +0x46
created by gopkg.in/qml%2ev1.Run
    /Users/quarnster/code/go/src/gopkg.in/qml.v1/bridge.go:67 +0x1aa

goroutine 21 [sleep]:
time.Sleep(0x3b9aca00)
    /Users/quarnster/code/3rdparty/go/src/pkg/runtime/time.goc:39 +0x31
_/private/tmp_test.func·001(0x0, 0x0)
    /private/tmp/blah_test.go:78 +0x2d8
_/private/tmp_test.TestBlah(0xc208068090)
    /private/tmp/blah_test.go:85 +0x4f
testing.tRunner(0xc208068090, 0x42c3300)
    /Users/quarnster/code/3rdparty/go/src/pkg/testing/testing.go:422 +0x8b
created by testing.RunTests
    /Users/quarnster/code/3rdparty/go/src/pkg/testing/testing.go:503 +0x8cf
exit status 2
FAIL    _/private/tmp   2.077s
niemeyer commented 10 years ago

The problem is the same described in this thread:

https://groups.google.com/d/msg/go-qml/h5gDOjyE8Yc/jyG9Pia2GaMJ

As explained there, the QML engine is destroying the old proxy object even though it was just handed a reference to it, which seems like bug, but I won't argue about that nor expect it to be timely fixed. Instead, I'm planning to simplify significantly the reference handling logic by making Go own all the values, and use its garbage collector to release resources on the QML side.

I'll keep you posted via this ticket.

quarnster commented 10 years ago

Got it, thanks, I'll keep my eyes and ears open :)

niemeyer commented 9 years ago

Sorry it's taking a while to get to this. I'll move it up in the agenda and will go back to it after the new gl package structure is working.

niemeyer commented 9 years ago

I'm back into this. Should have a fix shortly.

niemeyer commented 9 years ago

Okay, rather than changing too much right now, I'm going for a more conservative approach which I should have implemented long ago.

quarnster commented 9 years ago

Excellent! Appears to work fine from very minor testing, thanks! :)