rumps icon indicating copy to clipboard operation
rumps copied to clipboard

memory leak for dynamically generated menus

Open chadselph opened this issue 7 years ago • 2 comments

This is a really great library, a joy to use! However, I managed to his an edge case that probably isn't a common one.

I was generating menus dynamically, based on the JIRA Agile boards API, refreshing the menu every 30 seconds. My original approach was a naive:

@rumps.timer(30)
def update_menu(_):
    m = generate_menu()
    app.menu.clear()
    app.menu = m

this worked great, except that it seemed to be leaking a lot of memory. Digging in, it doesn't look like when the Python MenuItem object is deleted, the corresponding Cocoa NSMenu/NSMenuItem memory isn't released?

chadselph avatar Apr 11 '17 14:04 chadselph

So I actually had occasion where I had to do a similar thing as well, not as intensive as 30 seconds but the app was intended to run for a long time so it was a concern. I dug into this slightly deeper and have a bit of bad news, I don't know if it is really fixable in rumps itself. What's going on here is that most objects in Obj-C use autorelease pools to manage their memory, this includes Cocoa objects like NSMenu and the like. PyObjC was supposed to use autoreleases to take care of that for you, but it looks to be a kind of obscure problem with PyObjC that when you use Cocoa and autorelease things tend to get funky.

I never really got a solution to this, but I would suggest that you talk to the PyObjC people for solutions because fundamentally that is where the problem is stemming from.

This was probably the closest issue I found that was filed, unfortunately it was closed due to some poor communication from the issuer's part

oceanusxiv avatar Nov 15 '17 19:11 oceanusxiv

So I actually had occasion where I had to do a similar thing as well, not as intensive as 30 seconds but the app was intended to run for a long time so it was a concern. I dug into this slightly deeper and have a bit of bad news, I don't know if it is really fixable in rumps itself. What's going on here is that most objects in Obj-C use autorelease pools to manage their memory, this includes Cocoa objects like NSMenu and the like. PyObjC was supposed to use autoreleases to take care of that for you, but it looks to be a kind of obscure problem with PyObjC that when you use Cocoa and autorelease things tend to get funky.

I never really got a solution to this, but I would suggest that you talk to the PyObjC people for solutions because fundamentally that is where the problem is stemming from.

This was probably the closest issue I found that was filed, unfortunately it was closed due to some poor communication from the issuer's part

updated link to the pyObjC issue #199

rosenblu1 avatar May 02 '23 22:05 rosenblu1