Memory management between Objective-C Core WLAN Functions called from Go lang

Hi Experts,

I am working on a application where I am calling Objective-C Core WLAN API from golang with below code,

darwin_lib.m

void ConnectWiFi(char * cInterfacename, char * cSSID, char * cSecurity,  char *cSubsecurity, char * cPassphrase,  char * cOnexuser, char * cOnexpass) {

@autoreleasepool {
        NSString * interfacename = [[NSString alloc] initWithUTF8String:cInterfacename];
        NSString * ssid = [[NSString alloc] initWithUTF8String:cSSID];
        NSString * security = [[NSString alloc] initWithUTF8String:cSecurity];
        NSString * subsecurity = [[NSString alloc] initWithUTF8String:cSubsecurity];
        NSString * passphrase = [[NSString alloc] initWithUTF8String:cPassphrase];
        NSString * onexuser = [[NSString alloc] initWithUTF8String:cOnexuser];
        NSString * onexpass = [[NSString alloc] initWithUTF8String:cOnexpass];
        NSError * connecterr = nil;
        CWWiFiClient * cwwificlient = [CWWiFiClient sharedWiFiClient];
        CWInterface *a = [cwwificlient interfaceWithName:interfacename];
        NSSet<CWNetwork *> * network = [a scanForNetworksWithName:ssid error:&connecterr];
**** some more code *****
BOOL result = [a associateToEnterpriseNetwork:network.anyObject identity:nil username:onexuser password:onexpass error:&connecterr];

main.go

package main

// #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework CoreWLAN
// #cgo LDFLAGS: -framework Foundation
// #include "clib.h"
import "C"
import "fmt"

func main() {
	var status C.int = C.ConnectWiFi()
   time.Sleep(20)
	*** some more code and long running process ***
}

Now what I am trying to understand here from memory release point of view is,

  • Is object created in C code will be freed once I reach time.Sleep(20) e.g cwwificlient a spcifically to Core WLAN side or do I have to call release specifically on object?
  • I tried calling release on cwwificlient and program is crashing on second execution, not sure why it would crash on only second execution, why not first time itself?
  • I tried calling release on a and program is not crashing.

Do I have to free all the Core WLAN related object myself or autoreleasepool will take care of it?

  • As whole code is inside autoreleasepool, will all the objects be released on it's own even though function is being called from go code? As go's garbage collector has no idea how many objects C code created so go's garbage collector won't be of any help here
  • Is autoreleasepool, can also take care of releasing any alloc object e.g ssid created inside C code ?

Replies

Is your Objective-C code compiled in MRR or ARC mode?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks Quinn, for looking into query. Yes, I am using ARC Mode while compiling.

  • Looks like I was wrong on above reply, go can not compile ARC as per this discussion, https://github.com/progrium/macdriver/discussions/194. So I am still not sure go compilation and memory management part. But here is good article on https://github.com/progrium/macdriver/blob/main/docs/memorymanagement.md though still it lacks some practical info though.

Add a Comment

Looks like I was wrong on above reply, go can not compile ARC

OK.

So, lemme start you out with Objective-C Memory Management for Swift Programmers. Read that through, apply the lessons, and lemme know where you land.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"