2019-06-11 12:38:22 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.wow.st/gmp/nswrap/examples/gc/ns"
|
|
|
|
)
|
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
func releaseX(x int) func(ns.MyClass, ns.MyClassSupermethods) {
|
|
|
|
return func(self ns.MyClass, super ns.MyClassSupermethods) {
|
2019-06-11 12:38:22 -04:00
|
|
|
//fmt.Printf("--release %d\n", x)
|
|
|
|
super.Release() // comment out for leak
|
|
|
|
}
|
|
|
|
}
|
2019-06-18 09:15:46 -04:00
|
|
|
|
2019-06-11 12:38:22 -04:00
|
|
|
func memtest1() {
|
|
|
|
fmt.Println("memtest1 started")
|
|
|
|
for {
|
2020-06-24 13:03:53 -04:00
|
|
|
arr := make([]*ns.MyClass, 1000)
|
2019-06-11 12:38:22 -04:00
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
// Alloc methods set a finalizer that causes the Go GC to
|
|
|
|
// Release these objects.
|
|
|
|
arr[i] = ns.MyClassAlloc()
|
|
|
|
arr[i].ReleaseCallback(releaseX(i))
|
|
|
|
|
|
|
|
// You can still manually retain objects, but that will cause
|
|
|
|
// them to stick around after their Go pointers are collected.
|
|
|
|
//arr[i].Retain() // uncomment for leak
|
|
|
|
}
|
|
|
|
// Manually run the Go GC at every loop iteration. May not be needed
|
|
|
|
// in a real program.
|
|
|
|
runtime.GC()
|
2020-06-24 13:03:53 -04:00
|
|
|
time.Sleep(time.Second / 50)
|
2019-06-18 09:15:46 -04:00
|
|
|
//fmt.Printf("Loop complete\n")
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func memtest2() {
|
|
|
|
fmt.Println("memtest2 started")
|
2019-06-13 09:25:53 -04:00
|
|
|
i := 0
|
2019-06-11 12:38:22 -04:00
|
|
|
for {
|
2020-06-24 13:03:53 -04:00
|
|
|
o1 := ns.NSStringAlloc().InitWithGoString(fmt.Sprintf("two string %d", i))
|
|
|
|
o2 := ns.NSStringWithGoString(fmt.Sprintf("two string %d", i))
|
2019-06-11 12:38:22 -04:00
|
|
|
|
|
|
|
// NSWrap runs object constructors inside an @autoreleasepool block,
|
|
|
|
// and then calls "retain" on them before returning to Go. A Go
|
|
|
|
// finalizer is set allowing the Go GC to call Release().
|
|
|
|
|
2019-06-13 09:25:53 -04:00
|
|
|
o3 := ns.NSStringWithString(o1)
|
|
|
|
o4 := ns.NSStringAlloc()
|
|
|
|
_ = o4
|
2019-06-11 12:38:22 -04:00
|
|
|
|
2019-06-18 09:15:46 -04:00
|
|
|
a1 := ns.NSArrayAlloc()
|
|
|
|
|
|
|
|
// init methods in Objective-C always return a retained object.
|
|
|
|
// init may or may not return the same object that was sent in.
|
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
a1 = a1.InitWithObjects(o1, o2, o3, o4)
|
2019-06-18 09:15:46 -04:00
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
a2 := ns.NSArrayWithObjects(o1, o2, o3, o4)
|
2019-06-18 09:15:46 -04:00
|
|
|
|
|
|
|
// you can always nest alloc and init.
|
|
|
|
|
|
|
|
a3 := ns.NSMutableArrayAlloc().Init()
|
|
|
|
a3.AddObject(o1)
|
|
|
|
a3.AddObject(o2)
|
|
|
|
a3.AddObject(o3)
|
|
|
|
a3.AddObject(o4)
|
|
|
|
_ = a1
|
|
|
|
_ = a2
|
|
|
|
_ = a3
|
2019-06-11 12:38:22 -04:00
|
|
|
|
|
|
|
runtime.GC()
|
2020-06-24 13:03:53 -04:00
|
|
|
time.Sleep(time.Second / 50)
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:25:53 -04:00
|
|
|
func addStr(arr *ns.NSMutableArray) {
|
2019-06-18 09:15:46 -04:00
|
|
|
// temporary strings made by the 'WithGoString' methods should be released
|
|
|
|
// automatically by the GC.
|
|
|
|
|
2019-06-11 12:38:22 -04:00
|
|
|
s1 := ns.NSStringAlloc().InitWithGoString("a string")
|
|
|
|
arr.AddObject(s1)
|
|
|
|
|
|
|
|
// s1 should be eligible for Go garbage collection here, but is still referenced
|
|
|
|
// on the Objective-C side. By adding s1 to an array, the array automatically
|
|
|
|
// calls 'retain' on the underlying Objective-C string.
|
|
|
|
}
|
|
|
|
|
|
|
|
func memtest3() {
|
|
|
|
fmt.Println("memtest3 started")
|
|
|
|
|
2019-06-18 09:15:46 -04:00
|
|
|
for {
|
|
|
|
// arr will be garbage collected by Go
|
|
|
|
arr := ns.NSMutableArrayAlloc().Init()
|
|
|
|
addStr(arr)
|
|
|
|
runtime.GC()
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
// check that our string was retained.
|
|
|
|
|
|
|
|
s1 := arr.ObjectAtIndex(0)
|
2019-06-27 11:41:58 -04:00
|
|
|
gstr := s1.NSString().String()
|
2019-06-18 09:15:46 -04:00
|
|
|
_ = gstr
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func memtest4() {
|
|
|
|
fmt.Println("memtest4 started")
|
|
|
|
for {
|
2019-06-18 09:15:46 -04:00
|
|
|
o1 := ns.NSStringAlloc().InitWithGoString("red string")
|
|
|
|
|
|
|
|
// conversions to UTF8String internally create autoreleased strings
|
|
|
|
// in the Objective-C runtime. NSWrap runs these in a mini-
|
|
|
|
// @autoreleasepool block.
|
|
|
|
|
2019-06-11 12:38:22 -04:00
|
|
|
c1 := o1.UTF8String()
|
|
|
|
_ = o1
|
|
|
|
_ = c1
|
2019-06-13 09:25:53 -04:00
|
|
|
runtime.GC()
|
2020-06-24 13:03:53 -04:00
|
|
|
time.Sleep(time.Second / 50)
|
2019-06-27 11:41:58 -04:00
|
|
|
c1.Free() // you need to manually free UTF8Strings
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:25:53 -04:00
|
|
|
func memtest5() {
|
|
|
|
fmt.Println("memtest5 started")
|
|
|
|
i := 0
|
|
|
|
for {
|
2019-06-18 09:15:46 -04:00
|
|
|
// by incrementing i we can ensure that Objective-C needs to create
|
|
|
|
// a new NSString object at each loop iteration and cannot be reusing
|
|
|
|
// the same string object.
|
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
str := ns.NSStringWithGoString(fmt.Sprintf("blue string %d", i))
|
2019-06-18 09:15:46 -04:00
|
|
|
|
|
|
|
// SubstringFromIndex should be returning a newly allocated NSString,
|
|
|
|
// which is getting retained by NSWrap and released by a Go GC
|
|
|
|
// finalizer.
|
|
|
|
|
2019-06-13 09:25:53 -04:00
|
|
|
sub := str.SubstringFromIndex(5)
|
2019-06-18 17:29:52 -04:00
|
|
|
sub2 := sub.Copy().NSString()
|
|
|
|
sub3 := sub2.MutableCopy().NSString()
|
2019-06-18 09:15:46 -04:00
|
|
|
u := sub.UTF8String()
|
2019-06-18 17:29:52 -04:00
|
|
|
u2 := sub2.UTF8String()
|
|
|
|
u3 := sub3.UTF8String()
|
2020-06-24 13:03:53 -04:00
|
|
|
time.Sleep(time.Second / 50)
|
2019-06-13 09:25:53 -04:00
|
|
|
runtime.GC()
|
|
|
|
i++
|
2019-06-27 11:41:58 -04:00
|
|
|
u.Free()
|
|
|
|
u2.Free()
|
|
|
|
u3.Free()
|
|
|
|
_ = u
|
|
|
|
_ = u2
|
|
|
|
_ = u3
|
2019-06-19 14:07:33 -04:00
|
|
|
//fmt.Printf("loop completed\n")
|
2019-06-13 09:25:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 11:41:58 -04:00
|
|
|
func tmpdict(i int) *ns.NSString {
|
2020-06-24 13:03:53 -04:00
|
|
|
o1 := ns.NSStringWithGoString(fmt.Sprintf("temp string 1-%d", i))
|
|
|
|
o2 := ns.NSStringWithGoString(fmt.Sprintf("temp string 2-%d", i))
|
|
|
|
k1 := ns.NSStringWithGoString(fmt.Sprintf("temp key 1-%d", i))
|
|
|
|
k2 := ns.NSStringWithGoString(fmt.Sprintf("temp key 2-%d", i))
|
|
|
|
dict := ns.NSDictionaryWithObjectsAndKeys(o1, k1, o2, k2)
|
2019-06-27 11:41:58 -04:00
|
|
|
ret := dict.ValueForKey(k1)
|
|
|
|
//fmt.Printf("tmpdict(): string = %s\n",ret.NSString())
|
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
defer runtime.GC() // o1, o2, k1, k2, and dict can be released after we return
|
2019-06-27 11:41:58 -04:00
|
|
|
return ret.NSString() // should be retained by NSDictionary.ValueForKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
func tmparr(i int) *ns.NSString {
|
2020-06-24 13:03:53 -04:00
|
|
|
o1 := ns.NSStringWithGoString(fmt.Sprintf("temp string 3-%d", i))
|
|
|
|
o2 := ns.NSStringWithGoString(fmt.Sprintf("temp string 4-%d", i))
|
|
|
|
arr := ns.NSArrayWithObjects(o1, o2)
|
|
|
|
os := make([]*ns.Id, 0, 2)
|
|
|
|
arr.GetObjectsRange(&os, ns.NSMakeRange(0, 2))
|
2019-06-27 11:41:58 -04:00
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
defer runtime.GC() // collect o1, o2 and arr
|
2019-06-27 11:41:58 -04:00
|
|
|
return os[1].NSString() // should have been retained by NSArray.GetObjects()
|
|
|
|
}
|
|
|
|
|
|
|
|
func memtest6() {
|
|
|
|
fmt.Println("memtest6 started")
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
s1 := tmpdict(i)
|
|
|
|
s2 := tmparr(i)
|
|
|
|
time.Sleep(time.Second / 5)
|
|
|
|
u1 := s1.String() // make sure s1 and s2 are still available
|
|
|
|
u2 := s2.String()
|
2020-06-24 13:03:53 -04:00
|
|
|
e1 := fmt.Sprintf("temp string 1-%d", i)
|
2019-06-27 11:41:58 -04:00
|
|
|
if u1 != e1 {
|
2020-06-24 13:03:53 -04:00
|
|
|
fmt.Printf("tmpdict() error: %s != %s\n", u1, e1)
|
2019-06-27 11:41:58 -04:00
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
e2 := fmt.Sprintf("temp string 4-%d", i)
|
2019-06-27 11:41:58 -04:00
|
|
|
if u2 != e2 {
|
2020-06-24 13:03:53 -04:00
|
|
|
fmt.Printf("tmparr() error: %s != %s\n", u2, e2)
|
2019-06-27 11:41:58 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 12:38:22 -04:00
|
|
|
func main() {
|
2019-06-27 11:41:58 -04:00
|
|
|
fmt.Printf("MultiThreaded? %t\n", ns.NSThreadIsMultiThreaded())
|
|
|
|
th := ns.NSThreadNew()
|
|
|
|
th.Start()
|
|
|
|
fmt.Printf("MultiThreaded? %t\n", ns.NSThreadIsMultiThreaded())
|
2019-06-11 12:38:22 -04:00
|
|
|
go memtest1()
|
|
|
|
go memtest2()
|
|
|
|
go memtest3()
|
|
|
|
go memtest4()
|
2019-06-13 09:25:53 -04:00
|
|
|
go memtest5()
|
2019-06-27 11:41:58 -04:00
|
|
|
go memtest6()
|
|
|
|
|
2019-06-18 09:15:46 -04:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
// print a progress indicator
|
2020-06-24 13:03:53 -04:00
|
|
|
fmt.Printf("t = %s\n", time.Now())
|
2019-06-18 09:15:46 -04:00
|
|
|
time.Sleep(time.Second * 10)
|
|
|
|
}
|
|
|
|
}()
|
2019-06-11 12:38:22 -04:00
|
|
|
select {}
|
|
|
|
}
|