Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UninitializedDeallocWarning: NSNotification object with postNotification_ is empty and wont work #458

Closed
leifadev opened this issue Mar 11, 2022 · 3 comments

Comments

@leifadev
Copy link

Problem

Having trouble making a notification come up. I believe I am getting the warning because the object x I am making in the first place is empty as from print statements. After the failed attempt too, no print statements would show up again too.

Cocoa.NSNotification.alloc().init_() is supposed to be an empty notification called by Cocoa.NSNotificationCenter.alloc().postNotification_(x), but it doesn't work and I get the UninitializedDeallocWarning error below:

Code

@objc.IBAction
def helplink_(self, url):
  x = Cocoa.NSNotification.alloc().init_()
  Cocoa.NSNotificationCenter.alloc().postNotification_(x)

Error

PycharmProjects/shoutout/main.py:77: UninitializedDeallocWarning: leaking an 
uninitialized object of type NSNotificationCenter
  x = Cocoa.NSNotification.alloc().init_()

Here is working code for launching a NSURL string object...

x = Cocoa.NSURL.alloc().initWithString_(self.gitUrl)
Cocoa.NSWorkspace.alloc().openURL_(x)

I am confused on how I can solve this, I will try to figure out a way to use other ones besides postNotification: or initWithName:object:userInfo:, etc. Any help on what I am doing wrong?

@ronaldoussoren
Copy link
Owner

PyObjC maps Objective-C's method calls directly into Python. Because of this object creation is a two step process, an explicit version of Python's __new__ and __init__ methods, for example:

obj = NSObject.alloc().init()

Leaving out the call to an init* method results in a partially initialised object, hence the UninitializedDeallocWarning. And alternative to using the two-step construction proces is to use one of the factory methods on a class (such as Cocoa.NSArray.array()).

In your particular case the call to postNotification_ uses the wrong pattern, it should be something like this:

@objc.IBAction
def helplink_(self, url):
    x = Cocoa.NSNotification.alloc().init() # Or probably    x = Cocoa.NSNotification.notificationWithName_object_("hi", 42) 
    Cocoa.NSNotificationCenter.defaultCenter().postNotification_(x) 

BTW. I intent to add more pythonic object creation in a future version, hopefully this year. See #275 for the basic idea.

@leifadev
Copy link
Author

leifadev commented Mar 13, 2022

BTW. I intent to add more pythonic object creation in a future version, hopefully this year. See #275 for the basic idea.

What do you mean by exactly making it more pythonic? Meaning allowing things ot be simplified in certain ways, super or subclassing stuff?

P.S. Also, I made a discord server for people to talk and get support about this, it has a simple open-source wiki with tickets for people to add good info. https://dsc.gg/pyobjc

If you think it would be helpful you can link it somewhere, or otherwise ill invite someone separately if I help them I dont know

@leifadev
Copy link
Author

leifadev commented Mar 13, 2022

Sadly after I wrote the above comment, I got this error:

/Users/ddd/PycharmProjects/shoutout/main.py:73: UninitializedDeallocWarning: leaking an uninitialized object of type NSConcreteNotification

However with x = Cocoa.NSNotification.notificationWithName_object_("hi", 42), it passed and registered at the memory address as a NSConcreteNotification object here: NSConcreteNotification 0x1001df2f0 {name = hi; object = 42}

However, no notification showed up sadly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants