Skip to contentSkip to conversation

ui/cocoa.m: Fix compiler and runtime warnings on 10.8

PATCH Superseded Peter Maydell 1 participant 6 messages Mar 25, 2013 – Apr 6, 2013

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover ui/cocoa.m: Fix compiler and runtime warnings on 10.8

These patches fix various compiler and runtime warnings QEMU provokes on MacOSX 10.8. The first two fix a leak and some deprecated functions which cause warnings in the system log when QEMU runs. The second two avoid some functions which cause compile time warnings about use of functions deprecated in 10.6.

I believe from my reading of the documentation that these changes should still work OK on 10.3, but I don't have any way of testing that.

Andreas: I don't write much ObjC so I don't really have a feel for what is good coding style. I mostly tried to follow the existing code (eg "//" style comments) and QEMU's C style (eg 80 cols max). I'm happy to change any of the indentation or whatever to suit your preferences.

With this patch set the only remaining compile time warnings from clang on 10.8 are about deprecated methods in audio/coreaudio.c.

Changes v1->v2:
* rebased and fixed up following recent console changes

Peter Maydell (4):
ui/cocoa.m: Fix leaks of NSScreen and NSConcreteMapTable ui/cocoa.m: Avoid deprecated CPS* functions ui/cocoa.m: Avoid deprecated NSOpenPanel filename method ui/cocoa.m: Avoid deprecated NSOpenPanel beginSheetForDirectory

 ui/cocoa.m | 52 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 32 insertions(+), 20 deletions(-)
Signature · 1 line
1.7.11.4
#1 lore raw

PATCH v2 1/4 ui/cocoa.m: Fix leaks of NSScreen and NSConcreteMapTable

On MacOSX 10.8 QEMU provokes system log messages:
11/03/2013 17:03:29.998 qemu-system-arm[42586]: objc[42586]: Object 0x7ffbf9c2f3b0 of class NSScreen autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug

11/03/2013 17:03:29.999 qemu-system-arm[42586]: objc[42586]: Object 0x7ffbf9c3a010 of class NSConcreteMapTable autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug

This is because we call back into Cocoa from threads other than the UI thread (specifically from the CPU thread). Since we created these threads via the POSIX API rather than NSThread, they don't have automatically created autorelease pools. Guard all the functions where QEMU can call back into the Cocoa UI code with autorelease pools so that we don't leak any Cocoa objects.

---
 ui/cocoa.m | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
ui/cocoa.m +10 -1
index 048cc97..e9120e4 100644
@@ -960,6 +960,8 @@ int main (int argc, const char * argv[]) {
 static void cocoa_update(DisplayChangeListener *dcl,
                          int x, int y, int w, int h)
 {
+    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
+
     COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
 
     NSRect rect;
@@ -973,18 +975,24 @@ static void cocoa_update(DisplayChangeListener *dcl,
             h * [cocoaView cdy]);
     }
     [cocoaView setNeedsDisplayInRect:rect];
+
+    [pool release];
 }
 
 static void cocoa_switch(DisplayChangeListener *dcl,
                          DisplaySurface *surface)
 {
-    COCOA_DEBUG("qemu_cocoa: cocoa_resize\n");
+    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
+    COCOA_DEBUG("qemu_cocoa: cocoa_switch\n");
     [cocoaView switchSurface:surface];
+    [pool release];
 }
 
 static void cocoa_refresh(DisplayChangeListener *dcl)
 {
+    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
+
     COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
 
     if (kbd_mouse_is_absolute()) {
@@ -1007,6 +1015,7 @@ static void cocoa_refresh(DisplayChangeListener *dcl)
         }
     } while(event != nil);
     vga_hw_update();
+    [pool release];
 }
 
 static void cocoa_cleanup(void)
Signature · 1 line
1.7.11.4
#2 lore raw

PATCH v2 2/4 ui/cocoa.m: Avoid deprecated CPS* functions

In reply to Peter Maydell

The functions CPSGetCurrentProcess and CPSEnableForegroundOperation are deprecated in newer versions of MacOSX and cause warning messages to be logged to the system log. Instead, use the new preferred method of promoting our console process up to a graphical app with menubar and Dock icon, which is TransformProcessType. (This function came in with MacOSX 10.3, so there's no need to retain the old method as we don't support anything earlier than 10.3 anyway.)

---
 ui/cocoa.m | 23 ++++++-----------------
 1 file changed, 6 insertions(+), 17 deletions(-)
ui/cocoa.m +6 -17
index e9120e4..e768b05 100644
@@ -851,22 +851,10 @@ QemuCocoaView *cocoaView;
 
 
 
-// Dock Connection
-typedef struct CPSProcessSerNum
-{
-        UInt32                lo;
-        UInt32                hi;
-} CPSProcessSerNum;
-
-OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn);
-OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
-OSErr CPSSetFrontProcess( CPSProcessSerNum *psn);
-
 int main (int argc, const char * argv[]) {
 
     gArgc = argc;
     gArgv = (char **)argv;
-    CPSProcessSerNum PSN;
     int i;
 
     /* In case we don't need to display a window, let's not do that */
@@ -890,12 +878,13 @@ int main (int argc, const char * argv[]) {
     }
 
     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
-    [NSApplication sharedApplication];
 
-    if (!CPSGetCurrentProcess(&PSN))
-        if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
-            if (!CPSSetFrontProcess(&PSN))
-                [NSApplication sharedApplication];
+    // Pull this console process up to being a fully-fledged graphical
+    // app with a menubar and Dock icon
+    ProcessSerialNumber psn = { 0, kCurrentProcess };
+    TransformProcessType(&psn, kProcessTransformToForegroundApplication);
+
+    [NSApplication sharedApplication];
 
     // Add menus
     NSMenu      *menu;
Signature · 1 line
1.7.11.4
#3 lore raw

PATCH v2 3/4 ui/cocoa.m: Avoid deprecated NSOpenPanel filename method

In reply to Peter Maydell

Avoid the NSOpenPanel filename method (deprecated in MacOSX 10.6) in favour of using the URL method and extracting the path from the resulting NSUrl object.

---
 ui/cocoa.m | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ui/cocoa.m +1 -1
index e768b05..36181d9 100644
@@ -810,7 +810,7 @@ QemuCocoaView *cocoaView;
         exit(0);
     } else if(returnCode == NSOKButton) {
         const char *bin = "qemu";
-        char *img = (char*)[ [ sheet filename ] cStringUsingEncoding:NSASCIIStringEncoding];
+        char *img = (char*)[ [ [ sheet URL ] path ] cStringUsingEncoding:NSASCIIStringEncoding];
 
         char **argv = (char**)malloc( sizeof(char*)*3 );
 
Signature · 1 line
1.7.11.4
#4 lore raw

PATCH v2 4/4 ui/cocoa.m: Avoid deprecated NSOpenPanel beginSheetForDirectory

In reply to Peter Maydell

In MacOSX 10.6 and above the NSOpenPanel beginSheetForDirectory method is deprecated. Use the preferred replacements instead. We retain the original code for use on earlier MacOSX versions because the replacement methods don't exist before 10.6.

---
 ui/cocoa.m | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
ui/cocoa.m +15 -1
index 36181d9..79fd41b 100644
@@ -35,6 +35,9 @@
 #ifndef MAC_OS_X_VERSION_10_5
 #define MAC_OS_X_VERSION_10_5 1050
 #endif
+#ifndef MAC_OS_X_VERSION_10_6
+#define MAC_OS_X_VERSION_10_6 1060
+#endif
 
 
 //#define DEBUG
@@ -771,9 +774,20 @@ QemuCocoaView *cocoaView;
         NSOpenPanel *op = [[NSOpenPanel alloc] init];
         [op setPrompt:@"Boot image"];
         [op setMessage:@"Select the disk image you want to boot.\n\nHit the \"Cancel\" button to quit"];
-        [op beginSheetForDirectory:nil file:nil types:[NSArray arrayWithObjects:@"img",@"iso",@"dmg",@"qcow",@"cow",@"cloop",@"vmdk",nil]
+        NSArray *filetypes = [NSArray arrayWithObjects:@"img", @"iso", @"dmg",
+                                 @"qcow", @"cow", @"cloop", @"vmdk", nil];
+#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
+        [op setAllowedFileTypes:filetypes];
+        [op beginSheetModalForWindow:normalWindow
+            completionHandler:^(NSInteger returnCode)
+            { [self openPanelDidEnd:op
+                  returnCode:returnCode contextInfo:NULL ]; } ];
+#else
+        // Compatibility code for pre-10.6, using deprecated method
+        [op beginSheetForDirectory:nil file:nil types:filetypes
               modalForWindow:normalWindow modalDelegate:self
               didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
+#endif
     } else {
         // or launch QEMU, with the global args
         [self startEmulationWithArgc:gArgc argv:(char **)gArgv];
Signature · 1 line
1.7.11.4
#5 lore raw
In reply to Peter Maydell

Ping? Does anybody with a PPC MacOS (10.3 or better) have time to test these patches?

(C.W.: I cc'd you since you submitted a macos patch recently so I figured you might be interested.)

Patchwork urls:
http://patchwork.ozlabs.org/patch/231004/
http://patchwork.ozlabs.org/patch/231006/
http://patchwork.ozlabs.org/patch/231003/
http://patchwork.ozlabs.org/patch/231005/

thanks
-- PMM

Quoted 34 lines · Peter Maydell

These patches fix various compiler and runtime warnings QEMU provokes on MacOSX 10.8. The first two fix a leak and some deprecated functions which cause warnings in the system log when QEMU runs. The second two avoid some functions which cause compile time warnings about use of functions deprecated in 10.6.

I believe from my reading of the documentation that these changes should still work OK on 10.3, but I don't have any way of testing that.

Andreas: I don't write much ObjC so I don't really have a feel for what is good coding style. I mostly tried to follow the existing code (eg "//" style comments) and QEMU's C style (eg 80 cols max). I'm happy to change any of the indentation or whatever to suit your preferences.

With this patch set the only remaining compile time warnings from clang on 10.8 are about deprecated methods in audio/coreaudio.c.

Changes v1->v2:
* rebased and fixed up following recent console changes

Peter Maydell (4):
ui/cocoa.m: Fix leaks of NSScreen and NSConcreteMapTable ui/cocoa.m: Avoid deprecated CPS* functions ui/cocoa.m: Avoid deprecated NSOpenPanel filename method ui/cocoa.m: Avoid deprecated NSOpenPanel beginSheetForDirectory

 ui/cocoa.m | 52 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 32 insertions(+), 20 deletions(-)

--
1.7.11.4