何时释放 npapi 插件中的对象

发布于 2024-08-15 16:23:14 字数 604 浏览 8 评论 0原文

我对 npapi 中的引用计数感到困惑。大多数情况下,我不知道哪种方法会增加引用计数。有人能详细解释一下吗?为了方便起见,我在这里列出了最常用的 NPN_* 函数和我自己的理解:

NPN_CreateObject: set ref count to 0

NPN_RetainObject: inc ref count

NPN_ReleaseObject: dec ref count

NPN_Evaluate: ?? (如果返回 NPObject*)

NPN_GetValue: ?? (如果返回 NPObject*)

NPN_SetValue: ?? (如果设置为 NPObject*)

NPN_GetProperty: ?? (如果返回 NPObject*)

NPN_SetProperty: ?? (如果设置为 NPObject*)

NPN_RemoveProperty: ??

NPN_枚举:??

NPN_构造:??

另一件事:npapi 是嵌套发布吗? (如果 NPObject* 具有 NPObject* 属性,释放父级将减少子级的引用计数)。

谢谢。

I am confused about the ref count in npapi. Mostly, I don't know which method will increase ref count. Can anyone explain in detail about this? For the convenience, I listed most common used NPN_* functions here and my own understanding:

NPN_CreateObject: set ref count to 0

NPN_RetainObject: inc ref count

NPN_ReleaseObject: dec ref count

NPN_Evaluate: ?? (in case return an NPObject*)

NPN_GetValue: ?? (in case return an NPObject*)

NPN_SetValue: ?? (in case set to an NPObject*)

NPN_GetProperty: ?? (in case return an NPObject*)

NPN_SetProperty: ?? (in case set to an NPObject*)

NPN_RemoveProperty: ??

NPN_Enumerate: ??

NPN_Construct: ??

another thing: is npapi do nested release? (In case NPObject* with a property of NPObject*, release parent will decrease the ref count of child).

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦毁影碎の 2024-08-22 16:23:14

评论里没有足够的空间来很好地回答你的问题,所以我会放在另一个答案中。

每当您的代码从 NPObject 函数(上面提到的函数之一)获取 NPObject 时,您都应该在完成后释放该 NPObject。 (这可能是立即的,或者您可以将其保存一段时间并在对象被销毁时释放它)。 NPVariant 也是如此。对于传递到 Invoke 函数的参数,它确实成立,但是您设置的返回值将在完成后由浏览器释放。

当您调用 NPN_GetValue 并从那里获取 NPObject 时,它也必须被释放。这意味着当浏览器调用 NPP_GetValue 时,它​​将在完成后释放您的 NPObject。如果您希望每次浏览器调用 NPP_GetValue 来获取您的 NPObject 时都创建一个新的 NPObject,那么您不必对其调用 NPN_RetainObject; NPAPI 示例中的假设是您正在保存 NPObject 的副本,以便在删除插件对象之前它不会被删除。

由于浏览器每次调用 NPP_GetValue 来获取 NPObject 时都会调用 Release,因此您需要确保在返回引用计数之前递增它。在要保留它的情况下不必调用它两次的原因是 NPN_CreateObject 在返回对象之前执行隐式 Retain。

我在这里写了更详细的解释:

There isn't room in comments to answer your question in the comment well, so I'll put it in another answer.

Any time your code gets an NPObject from a NPObject function (one of those you mentioned above), you should Release that NPObject when you're done with it. (that could be immediately, or you could save it for awhile and release it when your object gets destroyed). The same holds true with a NPVariant. It does not hold true with the arguments passed into your Invoke function, but the return value you set will get released by the browser when it's done.

When you call NPN_GetValue and get an NPObject from there, that also has to be released. This means that when the browser calls NPP_GetValue, it will release your NPObject when it's done. If you want to create a new NPObject every time the browser calls NPP_GetValue to get your NPObject, then you don't have to call NPN_RetainObject on it; the assumption in the NPAPI example is that you are saving a copy of your NPObject so that it doesn't get deleted until your plugin object gets deleted.

Since the browser will call Release for every time that it calls NPP_GetValue to get your NPObject, you need to make sure that the refcount is incremented before you return it. The reason you don't have to call it twice in the case that you're going to keep it is that NPN_CreateObject does an implicit Retain before it returns your object.

I have written up a more detailed explanation here:

差↓一点笑了 2024-08-22 16:23:14

首先,纠正一个误解:NPN_CreateObject 将 refCount 设置为 1,而不是 0。然后,当您调用 NPN_RetainObject 时,它会增加引用计数,而 NPN_ReleaseObject 将减少它。如果 ReleaseObject 将引用计数减少到 0,它也会通过从 NPClass 调用 deallocate 函数来释放它(在执行任何需要的清理后应该删除 NPObject),

请参阅: https://developer.mozilla.org/En/NPClass

除此之外,任何其他 NPClass 函数的一个良好的一般经验法则是,任何时候您将NPVariant 中的 NPObject,需要调用 NPN_RetainObject。要记住这一点,只需记住,当您完成 NPVariant(您已经使用过并且已经完成的一个,而不是作为返回值传递的一个)时,您调用 NPN_ReleaseVariantValue,如果它是一个 NPString 数据,它将释放 NPString 数据。字符串或 NPObject(如果它是对象)。

因此,从任何其他方法中,如果您返回 NPObject,则需要在将其填充到 NPVariant 之前调用 NPN_RetainObject。另外,如果保存 NPObject 的副本,则应在其上调用 NPN_RetainObject,并在保存完成后调用 NPN_ReleaseObject。还值得一提的是,您永远不应该从主线程以外的线程调用任何 NPN_ 方法。

这有帮助吗?

First, to correct a misconception: NPN_CreateObject sets the refCount to 1, not to 0. Then, when you call NPN_RetainObject it increments the refcount, and NPN_ReleaseObject will decrement it. If ReleaseObject decrements the refcount to 0, it will also free it by calling the deallocate function from your NPClass (which should delete the NPObject after doing any needed cleanup)

see: https://developer.mozilla.org/En/NPClass

beyond that, a good general rule of thumb for any of the other NPClass functions is that any time you put an NPObject in a NPVariant, you need to call NPN_RetainObject. To remember this, simply remember that when you're done with an NPVariant (one that you have used and are done with, not one that was passed as a return value), you call NPN_ReleaseVariantValue, which will release the NPString data if it's a string or the NPObject if it's an object.

So from any of the other methods, if you are returning an NPObject, you need to call NPN_RetainObject before you stuff it into the NPVariant. Also, if you save a copy of the NPObject, you should call NPN_RetainObject on it and NPN_ReleaseObject when you're done saving it. It also bears mentioning that you should never call any NPN_ method from a thread other than the main thread.

Does that help?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文