信号处理程序可以在PyQt中泄漏内存吗?

编程入门 行业动态 更新时间:2024-10-26 17:25:22
本文介绍了信号处理程序可以在PyQt中泄漏内存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

简短问题:

可以通知处理程序内存泄漏.

Can signal handlers memory leak.

常见问题:

在C#中,如果我将处理程序附加到事件

In C#, if I attach a handler to an event

left_object.left_event += right_object.right_handler

然后,当我摆脱right_object时,我需要删除该处理程序,否则垃圾收集器将永远不会处理它(因为left_object.left_event保留了指向right_object的指针)

Then I need to remove the handler when I get rid of right_object or the garbage collector will never dispose of it (since left_object.left_event retains a pointer to right_object)

PyQt 信号和插槽也是如此.

Is the same also true of PyQt signals and slots.

left_object.left_signal.connect( right_object.right_handler )

我从这个问题中看到,Qt会自动断开信号和插槽的链接,当调用left_object或right_object的析构函数时,但是在Python中,我无法显式调用该析构函数,并且right_handler是普通函数.

I see from this question that Qt automatically delinks signals and slots, when the destructor of either left_object or right_object is called, but in Python I cannot explicitly call the destructor, and right_handler is a plain-old-function.

我是否需要删除处理程序以防止right_object的内存泄漏,还是PyQt使用某种弱引用?

Do I need to remove the handler to prevent right_objects memory-leaking, or does PyQt use some sort of weak-referencing?

虽然其中一个答案与此相关,但这个类似的问题询问PyQt如何处理lambda表达式内的对象,而不是PyQt如何处理信号.

While one of the answers touches on this, this similar question asks about how PyQt handles objects inside lambda expressions, not how PyQt handles signals.

推荐答案

使用此设计可能会导致内存泄漏.如果这些连接包含使对象保持活动状态的引用,则需要断开信号和插槽的连接.

It is possible to have a memory leak with this design. You need to disconnect the signals and slots if those connections contain references which are keeping objects alive.

这是否实际发生取决于right_handler的确切含义.如果right_handler是对self的引用的闭包,则您有此问题.

Whether or not this actually happens depends on what exactly right_handler is. If right_handler is a closure with a reference to self then you have this problem.

更多推荐

信号处理程序可以在PyQt中泄漏内存吗?

本文发布于:2023-05-25 07:31:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/285800.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:信号处理   内存   程序   PyQt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!