Giter Site home page Giter Site logo

Comments (5)

ying32 avatar ying32 commented on August 23, 2024

我修改了下,这样写才对,有可能被GC

package main

import (
	"unsafe"

	"github.com/ying32/govcl/vcl"
)

type Person struct {
	Name string
}

func main() {
	// 这里已经是指针了
	p1 := &Person{}
	p1.Name = "Tom"

	strList := vcl.NewStringList()
        // 要记得添加内存释放的
	defer strList.Free()
	// 这里直接使用
	strList.AddObject("item1", vcl.ObjectFromInst(uintptr(unsafe.Pointer(p1))))

	// 这里要获取指针的实例地址
	item1 := (*Person)(unsafe.Pointer(strList.Objects(0).Instance()))
	println("Name = " + item1.Name)

}

from govcl.

ismlsmile avatar ismlsmile commented on August 23, 2024

前面我的测试代码有个笔误:
strList.AddObject("item1", vcl.ObjectFromInst(uintptr(unsafe.Pointer(&p1))))

后面取了指针的指针,所以怎么试都崩溃,用上面你改的示例可以运行。

但拿到事件回调函数中获取就不行了,应该是被gc了。可能需要类似ListView和TreeView那样的改造。

from govcl.

ying32 avatar ying32 commented on August 23, 2024

你检出下dev分支的代码,然后这样写个看看

package main

import (
	"unsafe"
	"github.com/ying32/govcl/vcl"
)

type Person struct {
	Name string
}

func main() {
	// 这里已经是指针了
	p1 := &Person{}
	p1.Name = "Tom"

	strList := vcl.NewStringList()
        // 要记得添加内存释放的
	defer strList.Free()
	// 这里直接使用
	strList.AddObject("item1", vcl.ObjectFromUnsafePointer(unsafe.Pointer(p1)))

	item1 := (*Person)(strList.Objects(0).UnsafeAddr())
	println("Name = " + item1.Name)

}

from govcl.

ismlsmile avatar ismlsmile commented on August 23, 2024

checkout了dev分支测试了一下,上面的代码运行OK。在事件回调中测试也OK,完美!

from govcl.

ying32 avatar ying32 commented on August 23, 2024

哈哈,遇到GC上面的代码也不知道会有不会啥意外的

from govcl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.