Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default 0 is not a valid connection ID. error with swift and cocoa programmatically

Tags:

swift

cocoa

I'm writing a mac os targeted game engine in swift. Im using metal for rendering and cocoa for handling the window. The entire app is being made programmatically. I'm not using a storyboard or any xibs

I have this file: Window.swift

import Cocoa

class Window : NSObject
{

    let width, height : Int
    let title : String
    let size : CGSize
    let rect : NSRect
    let window : NSWindow

    init(width : Int, height: Int, title: String) {
        self.width = width
        self.height = height
        self.title = title
        self.size = CGSize(width: width, height: height)
        self.rect = NSRect(origin: .zero, size: size)
        self.window = NSWindow(contentRect: rect, styleMask: 
        .closable, backing: .buffered, defer: false)
    }

    func createWindow()
    {
        window.title = title
        window.isOpaque = false
        window.center()
        window.isMovableByWindowBackground = true
        window.backgroundColor = NSColor.gray
        window.makeKeyAndOrderFront(window)
    }
}

And this is main.swift

let window = Window.init(width: 1280, height: 720, title: "Serious")
window.createWindow()

This code seems fine but for some reason i get this error: 2020-03-25 10:34:49.719557-0700 GameEngine[1355:50613] [default] 0 is not a valid connection ID. 2020-03-25 10:34:49.727881-0700 GameEngine[1355:50613] [default] 0 is not a valid connection ID. 2020-03-25 10:34:49.728046-0700 GameEngine[1355:50613] [default] 0 is not a valid connection ID. Program ended with exit code: 0

like image 994
Pranav Grandhi Avatar asked Jan 23 '26 15:01

Pranav Grandhi


1 Answers

This answer is a bit late, but I'm posting it in the hopes that some other poor sap like me might be spared the painstaking process of stepping through assembly instructions. As Willeke mentioned in the comments, an invalid style mask will cause this message to be logged. In my case it was a closable, miniaturizable, closable window. Adding the titled flag solved the issue. My understanding is that if you are creating the window before initializing an Application instance or calling NSApplicationMain, an invalid combination of style bits may not throw an exception, but can cause execution to take a route that ends up trying to locate the Window ID in the wrong place, resulting in an ID of 0 being sent to the window server.

like image 126
BMPatterson Avatar answered Jan 26 '26 05:01

BMPatterson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!