<!--
{
  "availability" : [
    "macCatalyst: 13.1.0 - 13.1.0"
  ],
  "documentType" : "symbol",
  "framework" : "CoreGraphics",
  "identifier" : "/documentation/CoreGraphics/CGWindowServerCFMachPort()",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Core Graphics"
    ],
    "preciseIdentifier" : "c:@F@CGWindowServerCFMachPort"
  },
  "title" : "CGWindowServerCFMachPort()"
}
-->

# CGWindowServerCFMachPort()

Returns a Core Foundation Mach port (CFMachPort) that corresponds to the macOS window server.

```
func CGWindowServerCFMachPort() -> CFMachPort?
```

## Return Value

A Core Foundation Mach port, or `NULL` if the window server is not running. When you no longer need the port, you should release it using the function `CFRelease`.

## Discussion

You can use this function to detect whether the window server process exits or is not running. If this function returns `NULL`, the window server is not running. This code example shows how to register a callback function to detect when the window server exits:

```objc
static void handleWindowServerDeath( CFMachPortRef port, void *info  )
{
    printf( "Window Server port death detected!\n" );
    CFRelease(port);
    exit(1);
}
 
static void watchForWindowServerDeath()
{
    CFMachPortRef port = CGWindowServerCFMachPort();
    CFMachPortSetInvalidationCallBack(port, handleWindowServerDeath);
}
```

Note that this callback only works if your program has an active run loop.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)