Convert all Go wrapped objects to pointers so the Go GC can track
all of the Objective-C pointers. Still working on bug with typedefs to wrapped types.
This commit is contained in:
+15
-12
@@ -20,8 +20,12 @@ func pb2() {
|
||||
a.Terminate(a)
|
||||
}
|
||||
|
||||
func didFinishLaunching(n ns.NSNotification) {
|
||||
func didFinishLaunching(n *ns.NSNotification) {
|
||||
fmt.Println("Go: did finish launching")
|
||||
fmt.Printf("Notification n = %p\n",n)
|
||||
fmt.Printf("Notification n.Ptr() = %p\n",n.Ptr())
|
||||
fmt.Printf("Notification n.Name() = %p\n",n.Name())
|
||||
fmt.Printf("Notification n.Name().Ptr() = %p\n",n.Name().Ptr())
|
||||
fmt.Printf("Notification: %s\n", n.Name().UTF8String())
|
||||
//Set up an NSWindow
|
||||
win = ns.NSWindowAlloc().InitWithContentRectStyleMask(
|
||||
@@ -83,40 +87,39 @@ func didFinishLaunching(n ns.NSNotification) {
|
||||
|
||||
cv := win.ContentView()
|
||||
|
||||
cv.AddSubview(b1.NSView)
|
||||
cv.AddSubview(b2.NSView)
|
||||
cv.AddSubview(&b1.NSView)
|
||||
cv.AddSubview(&b2.NSView)
|
||||
|
||||
viewmap := ns.NSDictionaryWithObjectsForKeys(
|
||||
ns.NSArrayWithObjects(b1, b2),
|
||||
ns.NSArrayWithObjects(nst("b1"), nst("b2")))
|
||||
|
||||
cv.AddConstraints(ns.NSLayoutConstraintsWithVisualFormat(
|
||||
nst("V:|-[b1]"), 0, ns.NSDictionary{}, viewmap))
|
||||
nst("V:|-[b1]"), 0, nil, viewmap))
|
||||
cv.AddConstraints(ns.NSLayoutConstraintsWithVisualFormat(
|
||||
nst("H:|-[b1]"), 0, ns.NSDictionary{}, viewmap))
|
||||
nst("H:|-[b1]"), 0, nil, viewmap))
|
||||
cv.AddConstraints(ns.NSLayoutConstraintsWithVisualFormat(
|
||||
nst("H:[b1]-[b2]"), ns.NSLayoutFormatAlignAllBaseline,
|
||||
ns.NSDictionary{}, viewmap))
|
||||
nst("H:[b1]-[b2]"), ns.NSLayoutFormatAlignAllBaseline, nil, viewmap))
|
||||
|
||||
a.ActivateIgnoringOtherApps(1)
|
||||
}
|
||||
|
||||
func shouldTerminateAfterLastWindowClosed(s ns.NSApplication) ns.BOOL {
|
||||
func shouldTerminateAfterLastWindowClosed(s *ns.NSApplication) ns.BOOL {
|
||||
return 1
|
||||
}
|
||||
|
||||
func willTerminate(n ns.NSNotification) {
|
||||
func willTerminate(n *ns.NSNotification) {
|
||||
fmt.Println("Go: will terminate")
|
||||
}
|
||||
|
||||
func didBecomeActive(n ns.NSNotification) {
|
||||
func didBecomeActive(n *ns.NSNotification) {
|
||||
fmt.Println("Go: did become active")
|
||||
fmt.Printf("Notification: %s\n", n.Name().UTF8String())
|
||||
}
|
||||
|
||||
var (
|
||||
a ns.NSApplication
|
||||
win ns.NSWindow
|
||||
a *ns.NSApplication
|
||||
win *ns.NSWindow
|
||||
)
|
||||
|
||||
func app() {
|
||||
|
||||
@@ -38,21 +38,21 @@ func main() {
|
||||
fmt.Printf("i1 = %@\n", i1)
|
||||
fmt.Printf("i1.Ptr() = %p\n", i1.Ptr())
|
||||
fmt.Printf("\nNSArray.ObjectEnumerator().ForIn():\n")
|
||||
a.ObjectEnumerator().ForIn(func(o ns.Id) bool {
|
||||
a.ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
||||
fmt.Println(o.NSString())
|
||||
return true
|
||||
})
|
||||
fmt.Printf("\nNSSetWithObjectsCount():\n")
|
||||
s1 := ns.NSSetWithObjectsCount(&[]ns.Id{n1.Id, n2.Id}, 2)
|
||||
s1 := ns.NSSetWithObjectsCount(&[]*ns.Id{&n1.Id, &n2.Id}, 2)
|
||||
fmt.Printf("\nNSSet.ObjectEnumerator().ForIn():\n")
|
||||
s1.ObjectEnumerator().ForIn(func(o ns.Id) bool {
|
||||
s1.ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
||||
fmt.Println(o.NSString())
|
||||
return true
|
||||
})
|
||||
fmt.Printf("\nNSMutableArrayWithObjects()\n")
|
||||
a = ns.NSMutableArrayWithObjects(n1, s1)
|
||||
fmt.Printf("\nNSArray.ObjectEnumerator().ForIn():\n")
|
||||
a.ObjectEnumerator().ForIn(func(o ns.Id) bool {
|
||||
a.ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
||||
fmt.Printf("%s -- ", o.ClassName().UTF8String())
|
||||
switch {
|
||||
case o.IsKindOfClass(ns.NSStringClass()):
|
||||
@@ -67,7 +67,7 @@ func main() {
|
||||
fmt.Printf("\nNSArrayWithObjects()\n")
|
||||
a2 = ns.NSArrayWithObjects(n1, n2, n3, s1)
|
||||
fmt.Printf("\nNSArray.ObjectEnumerator().ForIn():\n")
|
||||
a2.ObjectEnumerator().ForIn(func(o ns.Id) bool {
|
||||
a2.ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
||||
switch {
|
||||
case o.IsKindOfClass(ns.NSStringClass()):
|
||||
fmt.Println(o.NSString().UTF8String())
|
||||
@@ -84,9 +84,9 @@ func main() {
|
||||
ns.NSArrayWithObjects(nst("obj1"), nst("obj2")),
|
||||
ns.NSArrayWithObjects(nst("key1"), nst("key2")),
|
||||
)
|
||||
os := make([]ns.Id, 0, 5)
|
||||
os := make([]*ns.Id, 0, 5)
|
||||
fmt.Printf("Length of os is %d\n", len(os))
|
||||
ks := make([]ns.Id, 0, 5)
|
||||
ks := make([]*ns.Id, 0, 5)
|
||||
fmt.Printf("\nGetObjects()\n")
|
||||
d.GetObjects(&os, &ks, 4)
|
||||
fmt.Printf("Length of os is now %d\n", len(os))
|
||||
@@ -94,7 +94,7 @@ func main() {
|
||||
fmt.Printf("-- %s -> %s\n", k.NSString(), os[i].NSString())
|
||||
}
|
||||
fmt.Printf("\nNSStringWithContentsOfURLEncoding()\n")
|
||||
err := make([]ns.NSError, 1)
|
||||
err := make([]*ns.NSError, 1)
|
||||
n1 = ns.NSStringWithContentsOfURLEncoding(ns.NSURLWithGoString("htttypo://example.com"), 0, &err)
|
||||
fmt.Printf("err: %s\n", err[0].LocalizedDescription())
|
||||
|
||||
|
||||
+29
-7
@@ -18,7 +18,7 @@ func releaseX(x int) func (ns.MyClassSupermethods) {
|
||||
func memtest1() {
|
||||
fmt.Println("memtest1 started")
|
||||
for {
|
||||
arr := make([]ns.MyClass,1000)
|
||||
arr := make([]*ns.MyClass,1000)
|
||||
for i := 0; i < 1000; i++ {
|
||||
// Alloc methods set a finalizer that causes the Go GC to
|
||||
// Release these objects.
|
||||
@@ -27,8 +27,6 @@ func memtest1() {
|
||||
|
||||
// You can still manually retain objects, but that will cause
|
||||
// them to stick around after their Go pointers are collected.
|
||||
// This may be necessary if you are adding objects to an
|
||||
// Objective-C collection?
|
||||
//arr[i].Retain() // uncomment for leak
|
||||
}
|
||||
// Manually run the Go GC at every loop iteration. May not be needed
|
||||
@@ -41,24 +39,31 @@ func memtest1() {
|
||||
|
||||
func memtest2() {
|
||||
fmt.Println("memtest2 started")
|
||||
i := 0
|
||||
for {
|
||||
o1 := ns.NSStringAlloc().InitWithGoString("one string")
|
||||
o1 := ns.NSStringAlloc().InitWithGoString(fmt.Sprintf("two string %d",i))
|
||||
o2 := ns.NSStringWithGoString(fmt.Sprintf("two string %d",i))
|
||||
|
||||
// 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().
|
||||
|
||||
o2 := ns.NSStringWithGoString("two string") // does not leak
|
||||
o3 := ns.NSStringWithString(o1)
|
||||
o4 := ns.NSStringAlloc()
|
||||
_ = o4
|
||||
|
||||
arr := ns.NSArrayAlloc().InitWithObjects(o1,o2)
|
||||
//arr := ns.NSArrayAlloc().InitWithObjects(o1,o1)
|
||||
arr := ns.NSArrayWithObjects(o1,o2,o3,o4)
|
||||
_ = arr
|
||||
|
||||
//o1.Release()
|
||||
//o1.Release()
|
||||
runtime.GC()
|
||||
time.Sleep(time.Second/50)
|
||||
}
|
||||
}
|
||||
|
||||
func addStr(arr ns.NSMutableArray) {
|
||||
func addStr(arr *ns.NSMutableArray) {
|
||||
s1 := ns.NSStringAlloc().InitWithGoString("a string")
|
||||
arr.AddObject(s1)
|
||||
|
||||
@@ -86,14 +91,31 @@ func memtest4() {
|
||||
c1 := o1.UTF8String()
|
||||
_ = o1
|
||||
_ = c1
|
||||
runtime.GC()
|
||||
time.Sleep(time.Second/10)
|
||||
}
|
||||
}
|
||||
|
||||
func memtest5() {
|
||||
fmt.Println("memtest5 started")
|
||||
i := 0
|
||||
for {
|
||||
str := ns.NSStringWithGoString(fmt.Sprintf("five string %d",i))
|
||||
_ = str
|
||||
sub := str.SubstringFromIndex(5)
|
||||
_ = sub
|
||||
fmt.Printf("sub = %s\n",sub)
|
||||
time.Sleep(time.Second/10)
|
||||
runtime.GC()
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
go memtest1()
|
||||
go memtest2()
|
||||
go memtest3()
|
||||
go memtest4()
|
||||
go memtest5()
|
||||
select {}
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
package ns
|
||||
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -x objective-c -fno-objc-arc
|
||||
#cgo LDFLAGS: -framework Foundation
|
||||
#pragma clang diagnostic ignored "-Wformat-security"
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
//export MyClassRelease
|
||||
func MyClassRelease(o unsafe.Pointer) {
|
||||
MyClassMux.RLock()
|
||||
cb := MyClassLookup[o].Release
|
||||
MyClassMux.RUnlock()
|
||||
if cb == nil { return }
|
||||
self := MyClass{}
|
||||
self.ptr = o
|
||||
super := MyClassSupermethods{
|
||||
self.SuperRelease,
|
||||
}
|
||||
cb(super)
|
||||
}
|
||||
|
||||
//export MyClassDealloc
|
||||
func MyClassDealloc(o unsafe.Pointer) {
|
||||
MyClassMux.RLock()
|
||||
cb := MyClassLookup[o].Dealloc
|
||||
MyClassMux.RUnlock()
|
||||
if cb == nil { return }
|
||||
cb()
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -11,6 +11,8 @@ subclasses:
|
||||
NSObject:
|
||||
- dealloc
|
||||
- release
|
||||
functions:
|
||||
- NSMakeRange
|
||||
frameworks:
|
||||
- Foundation
|
||||
pragma: [ clang diagnostic ignored "-Wformat-security" ]
|
||||
|
||||
+32
-26
@@ -74,6 +74,7 @@ func memtest2() {
|
||||
s1 := ns.NSStringWithGoString(fmt.Sprintf("string-%d", i))
|
||||
_ = s1
|
||||
//o1.Retain() // uncomment for leak
|
||||
i++
|
||||
})
|
||||
time.Sleep(time.Second / 3)
|
||||
}
|
||||
@@ -94,24 +95,24 @@ func memtest3() {
|
||||
arr.Autorelease()
|
||||
arr.AddObject(ns.NSStringWithGoString(fmt.Sprintf("my string %d",i)))
|
||||
s1 := ns.NSStringWithGoString(fmt.Sprintf("my other string %d",i))
|
||||
fmt.Printf("%s\n",arr.ObjectAtIndex(0).NSString())
|
||||
_ = s1
|
||||
i++
|
||||
|
||||
for {
|
||||
for x := 0; x < 3; x++ {
|
||||
ns.Autoreleasepool(func() {
|
||||
str := arr.ObjectAtIndex(0).NSString()
|
||||
fmt.Println(str) // does not leak in an autorelease pool
|
||||
time.Sleep(time.Second / 2)
|
||||
fmt.Printf("%d->%s\n",x,str) // does not leak in an autorelease pool
|
||||
time.Sleep(time.Second / 5)
|
||||
})
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(time.Second/2)
|
||||
i++
|
||||
})
|
||||
}
|
||||
fmt.Println("memtest3: done")
|
||||
}
|
||||
|
||||
//Test of manual memory management. Lets run multiple goroutines here to
|
||||
//confirm we can use multiple threads if autorelease pools are not in play.
|
||||
//Test of manual memory management.
|
||||
func memtest4() {
|
||||
go memtest4a()
|
||||
go memtest4a()
|
||||
@@ -122,10 +123,16 @@ func memtest4() {
|
||||
go memtest4c()
|
||||
go memtest4c()
|
||||
go memtest4c()
|
||||
// Exactly one goroutine (locked to an OS thread) can use an
|
||||
// autorelease pool (?)
|
||||
// running multiple separate threads with autorelease pools...
|
||||
go memtest1()
|
||||
go memtest2()
|
||||
go memtest3()
|
||||
go memtest1()
|
||||
go memtest2()
|
||||
go memtest3()
|
||||
go memtest1()
|
||||
go memtest2()
|
||||
go memtest3()
|
||||
}
|
||||
|
||||
func memtest4a() {
|
||||
@@ -140,24 +147,34 @@ func memtest4b() {
|
||||
i := 0
|
||||
for {
|
||||
o1 := ns.NSObjectAlloc() // need to Release
|
||||
|
||||
// These object constructors will always leak. In the case of an
|
||||
// immutable string, it is not an issue unless a large number of different
|
||||
// strings are going to be made.
|
||||
//s1 := ns.NSStringWithGoString(fmt.Sprintf("a string %d",i)) // uncomment for leak
|
||||
s1 := ns.NSStringWithGoString("a string")
|
||||
i++
|
||||
arr := ns.NSArrayAlloc().InitWithObjects(s1) // need to Release arr
|
||||
s2 := arr.ObjectAtIndex(0).NSString()
|
||||
|
||||
//If you try to convert an NSString to UTF8String, CString (*Char),
|
||||
//or GoString, Objective-C runtime will leak an
|
||||
//NSTaggedPointerCStringContainer. Don't know why or how to fix it.
|
||||
//There would be no leak if we were using an autorelease pool.
|
||||
//u := str.UTF8String() // uncomment for leak
|
||||
// If you try to convert an NSString to UTF8String, CString (*Char),
|
||||
// or GoString, Objective-C runtime creates an autoreleased
|
||||
// NSTaggedPointerCStringContainer internally and there is no way to
|
||||
// release it unless you called your method from within an autorelease
|
||||
// pool. The following two calls cause a leak.
|
||||
//u := s1.UTF8String() // uncomment for leak
|
||||
//fmt.Println(s1) // uncomment for leak
|
||||
|
||||
time.Sleep(time.Second / 50)
|
||||
|
||||
o1.Release()
|
||||
arr.Release()
|
||||
//s1.Release() // does not work
|
||||
|
||||
//s1.Release() // does not prevent the leak caused by
|
||||
// NSStringWithGoString: s1 is autoreleased by the Objective-C
|
||||
// runtime, these methods will always leak without autorelease
|
||||
// pools.
|
||||
|
||||
_ = o1
|
||||
_ = s2
|
||||
}
|
||||
@@ -173,17 +190,6 @@ func memtest4c() {
|
||||
}
|
||||
}
|
||||
|
||||
func fin(o *ns.MyClass) {
|
||||
o.Release()
|
||||
}
|
||||
|
||||
func releaseX(x int) func (ns.MyClassSupermethods) {
|
||||
return func(super ns.MyClassSupermethods) {
|
||||
fmt.Printf("--release %d\n", x)
|
||||
super.Release() // comment out for leak
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
//Uncomment more than one test at a time for a crash.
|
||||
//Note: You may not run autorelease pools from multiple goroutines.
|
||||
|
||||
Reference in New Issue
Block a user