diff --git a/virtualization.go b/virtualization.go index a32a318..4386107 100644 --- a/virtualization.go +++ b/virtualization.go @@ -9,7 +9,6 @@ package vz */ import "C" import ( - "log" "runtime/cgo" "sync" "unsafe" @@ -130,8 +129,6 @@ func NewVirtualMachine(config *VirtualMachineConfiguration) (*VirtualMachine, er func (v *VirtualMachine) finalize() { v.finalizeOnce.Do(func() { - v.stateHandle.Delete() - // deleteStateHandler(unsafe.Pointer(&v.stateHandle)) objc.ReleaseDispatch(v.dispatchQueue) objc.Release(v) }) @@ -163,7 +160,6 @@ func changeStateOnObserver(newStateRaw C.int, cgoHandlerPtr unsafe.Pointer) { v, _ := stateHandler.Value().(*machineState) v.mu.Lock() newState := VirtualMachineState(newStateRaw) - log.Println(newState.String()) v.state = newState // for non-blocking go func() { v.stateNotify <- newState }() diff --git a/virtualization_11.h b/virtualization_11.h index 92b1000..d24ad71 100644 --- a/virtualization_11.h +++ b/virtualization_11.h @@ -13,11 +13,19 @@ void connectionHandler(void *connection, void *err, void *cgoHandlerPtr); void changeStateOnObserver(int state, void *cgoHandler); bool shouldAcceptNewConnectionHandler(void *cgoHandler, void *connection, void *socketDevice); +void deleteStateHandler(void *cgoHandlerPtr); @interface Observer : NSObject - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context; @end +@interface ObservableVZVirtualMachine : VZVirtualMachine +- (instancetype)initWithConfiguration:(VZVirtualMachineConfiguration *)configuration + queue:(dispatch_queue_t)queue + statusHandler:(void *)statusHandler; +- (void)dealloc; +@end + /* VZVirtioSocketListener */ @interface VZVirtioSocketListenerDelegateImpl : NSObject - (instancetype)initWithHandler:(void *)cgoHandler; diff --git a/virtualization_11.m b/virtualization_11.m index f807c94..0216fb5 100644 --- a/virtualization_11.m +++ b/virtualization_11.m @@ -26,6 +26,32 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N } @end +@implementation ObservableVZVirtualMachine { + Observer *_observer; + void *_stateHandler; +}; +- (instancetype)initWithConfiguration:(VZVirtualMachineConfiguration *)configuration + queue:(dispatch_queue_t)queue + statusHandler:(void *)statusHandler +{ + self = [super initWithConfiguration:configuration queue:queue]; + _observer = [[Observer alloc] init]; + [self addObserver:_observer + forKeyPath:@"state" + options:NSKeyValueObservingOptionNew + context:statusHandler]; + return self; +} + +- (void)dealloc +{ + [self removeObserver:_observer forKeyPath:@"state"]; + deleteStateHandler(_stateHandler); + [_observer release]; + [super dealloc]; +} +@end + @implementation VZVirtioSocketListenerDelegateImpl { void *_cgoHandler; } @@ -663,16 +689,10 @@ VZVirtioSocketConnectionFlat convertVZVirtioSocketConnection2Flat(void *connecti void *newVZVirtualMachineWithDispatchQueue(void *config, void *queue, void *statusHandler) { if (@available(macOS 11, *)) { - VZVirtualMachine *vm = [[VZVirtualMachine alloc] + ObservableVZVirtualMachine *vm = [[ObservableVZVirtualMachine alloc] initWithConfiguration:(VZVirtualMachineConfiguration *)config - queue:(dispatch_queue_t)queue]; - @autoreleasepool { - Observer *o = [[Observer alloc] init]; - [vm addObserver:o - forKeyPath:@"state" - options:NSKeyValueObservingOptionNew - context:statusHandler]; - } + queue:(dispatch_queue_t)queue + statusHandler:statusHandler]; return vm; } diff --git a/virtualization_12.m b/virtualization_12.m index 523fc93..7a0d64f 100644 --- a/virtualization_12.m +++ b/virtualization_12.m @@ -219,17 +219,17 @@ void setStreamsVZVirtioSoundDeviceConfiguration(void *audioDeviceConfiguration, @param error If not nil, assigned with the error if the initialization failed. @return A VZDiskImageStorageDeviceAttachment on success. Nil otherwise and the error parameter is populated if set. */ -void *newVZDiskImageStorageDeviceAttachmentWithCacheAndSyncMode(const char *diskPath, bool readOnly, int cacheMode, int syncMode, void **error) +void *newVZDiskImageStorageDeviceAttachmentWithCacheAndSyncMode(const char *diskPath, bool readOnly, int cacheMode, int syncMode, void **error) { if (@available(macOS 12, *)) { NSString *diskPathNSString = [NSString stringWithUTF8String:diskPath]; NSURL *diskURL = [NSURL fileURLWithPath:diskPathNSString]; return [[VZDiskImageStorageDeviceAttachment alloc] - initWithURL:diskURL - readOnly:(BOOL)readOnly - cachingMode:(VZDiskImageCachingMode)cacheMode - synchronizationMode: (VZDiskImageSynchronizationMode)syncMode - error:(NSError *_Nullable *_Nullable)error]; + initWithURL:diskURL + readOnly:(BOOL)readOnly + cachingMode:(VZDiskImageCachingMode)cacheMode + synchronizationMode:(VZDiskImageSynchronizationMode)syncMode + error:(NSError *_Nullable *_Nullable)error]; } RAISE_UNSUPPORTED_MACOS_EXCEPTION();