Add garbage collection memory mangement mode (gogc). Lots of fixes

related to pointer handling and memory management.
This commit is contained in:
gmp
2019-06-18 09:15:46 -04:00
parent 79bb282188
commit e5c8e114bf
8 changed files with 342 additions and 109 deletions
+44 -8
View File
@@ -5,6 +5,8 @@ import "C"
import (
"encoding/binary"
"fmt"
"runtime"
"time"
"git.wow.st/gmp/nswrap/examples/bluetooth/ns"
)
@@ -25,34 +27,53 @@ func updateState(c *ns.CBCentralManager) {
fmt.Printf(" powered on\n")
cm.ScanForPeripheralsWithServices(ns.NSArrayWithObjects(hrm_uuid), nil)
}
fmt.Printf("Go: updateState returning\n")
}
func discoverPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral, d *ns.NSDictionary, rssi *ns.NSNumber) {
fmt.Printf("Did discover peripheral\n")
c.StopScan()
if peripheral.Ptr() != nil {
// if we already have a pointer to a peripheral, check that this one is different,
// and if so release the old one. Be careful to check the Objective-C pointers
// here as the Go pointers will differ.
if peripheral != nil && p.Ptr() != peripheral.Ptr() {
peripheral.Release()
}
peripheral = p
// we need to take ownership of this peripheral so CoreBluetooth doesn't
// dealloc it.
peripheral.Retain()
c.ConnectPeripheral(peripheral, nil)
}
func connectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral) {
fmt.Printf("Did connect peripheral\n")
// set ourselves up as a peripheral delegate
p.SetDelegate(cd)
// discover all services on this device
p.DiscoverServices(nil)
fmt.Printf("Go: discoverPeripheral returning\n")
}
func discoverServices(p *ns.CBPeripheral, e *ns.NSError) {
fmt.Printf("Did discover services\n")
p.Services().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
serv := o.CBService()
uuid := serv.UUID()
switch {
case serv.UUID().IsEqualTo(hrm_uuid):
case uuid.IsEqualTo(hrm_uuid):
fmt.Printf("--heart rate monitor service\n")
p.DiscoverCharacteristics(nil, serv)
case serv.UUID().IsEqualTo(ns.CBUUIDWithGoString("180A")):
case uuid.IsEqualTo(info_uuid):
fmt.Printf("--device information service\n")
p.DiscoverCharacteristics(nil, serv)
default:
@@ -60,6 +81,7 @@ func discoverServices(p *ns.CBPeripheral, e *ns.NSError) {
}
return true
})
fmt.Printf("Go: discoverServices returning\n")
}
func hr(d *ns.NSData) int {
@@ -77,12 +99,14 @@ func hr(d *ns.NSData) int {
func discoverCharacteristics(p *ns.CBPeripheral, s *ns.CBService, e *ns.NSError) {
fmt.Printf("Did discover characteristics\n")
fmt.Printf("----%s\n", s.UUID().UUIDString().UTF8String())
if s.UUID().IsEqualTo(hrm_uuid) {
uuid := s.UUID()
fmt.Printf("----%s\n", uuid.UUIDString().UTF8String())
if uuid.IsEqualTo(hrm_uuid) {
s.Characteristics().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
chr := o.CBCharacteristic()
fmt.Printf("------%s\n", chr.UUID().UUIDString().UTF8String())
if chr.UUID().IsEqualTo(hrv_uuid) {
chuuid := chr.UUID()
fmt.Printf("------%s\n", chuuid.UUIDString().UTF8String())
if chuuid.IsEqualTo(hrv_uuid) {
p.SetNotifyValue(1, chr)
v := chr.Value()
fmt.Println(hr(v))
@@ -90,6 +114,7 @@ func discoverCharacteristics(p *ns.CBPeripheral, s *ns.CBService, e *ns.NSError)
return true
})
}
fmt.Printf("Go: discoverCharacteristics returning\n")
}
func updateValue(p *ns.CBPeripheral, chr *ns.CBCharacteristic, e *ns.NSError) {
@@ -97,11 +122,13 @@ func updateValue(p *ns.CBPeripheral, chr *ns.CBCharacteristic, e *ns.NSError) {
v := chr.Value()
fmt.Printf("Heart rate: %d\n", hr(v))
}
fmt.Printf("Go: updateValue returning\n")
}
var (
hrm_uuid *ns.CBUUID
hrv_uuid *ns.CBUUID
info_uuid *ns.CBUUID
cd *ns.CBDelegate
cm *ns.CBCentralManager
peripheral *ns.CBPeripheral
@@ -121,9 +148,18 @@ func main() {
hrm_uuid = ns.CBUUIDWithGoString("180D")
hrv_uuid = ns.CBUUIDWithGoString("2A37")
info_uuid = ns.CBUUIDWithGoString("180A")
//We defined our own queue because this won't work on the main queue.
// We defined our own queue because this won't work on the main queue.
cm = ns.CBCentralManagerAlloc().InitWithDelegateQueue(cd, queue)
// For debugging purposes, run GC every second to make sure things are not
// over-released.
go func() {
for {
runtime.GC()
time.Sleep(time.Second)
}
}()
select {}
}
+2 -1
View File
@@ -22,6 +22,7 @@ classes:
- NSDictionary
- NSEnumerator
- NSString
- NSAutoreleasePool
functions: [ NSMakeRange, dispatch_queue_create ]
enums: [ CB.* ]
@@ -38,4 +39,4 @@ delegates:
- peripheralDidUpdateValueForCharacteristic
pragma: [ clang diagnostic ignored "-Wformat-security" ]
#gogc: true
gogc: true