DllImport,回调和垃圾收集 [英] DllImport, Callbacks and garbage collection

查看:72
本文介绍了DllImport,回调和垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。


我正在努力使这项工作,但我有一个奇怪的行为。我有一个非常好的b $ b基本系统,我称之为非管理型dllimported。函数并给它一个

的回调函数结构。


有时,非托管部分会调用其中一个回调函数。但是

第一个指针的指针从地址变为0x00000001。

我无法弄明白为什么。


我的想法是收集垃圾,但我不明白为什么(我尝试了不同的方式,我目前使用结构实例作为静态)。另外

为什么垃圾收集器会把它设置为1而不是0?


任何链接或帮助将不胜感激:)


代码: http://www.rafb .net / paste / results / odKsGI15.html

推荐答案




如果你希望垃圾收集器不要改变你的地址使用你声明中的Fixed

命令,同样的事情发生在我身上使用不安全的代码。


祝你好运

SAlva

" Fabien Penso"写道:
Hi,

If you want the garbage collector not to change your addresses use the Fixed
command on your declaration, the same happened to me using unsafe code.

Best regards
SAlva
"Fabien Penso" wrote:
嗨。

我正在努力使这项工作,但我有一个奇怪的行为。我有一个非常基本的系统,我称之为非管理的dllimported系统。函数并给它一个回调函数的结构。

有时,非托管部分调用其中一个回调函数。但是第一个将其指针从其地址更改为0x00000001。
我无法弄清楚原因。
我的想法是垃圾收集,但我不明白为什么(我尝试了不同的方式,我目前使用结构实例作为静态)。另外
为什么垃圾收集器会将此设置为1而不是0?

任何链接或帮助将不胜感激:)

代码: http://www.rafb.net/paste/results/odKsGI15.html



Salvador写道:
Salvador wrote:


如果你想要垃圾收集器不要更改你的地址使用你声明中的Fixed
命令,同样的事情发生在我身上使用不安全的代码。
Hi,

If you want the garbage collector not to change your addresses use the Fixed
command on your declaration, the same happened to me using unsafe code.




我不确定它是否会起作用,固定"只能用很短的时间

时间,我的回调会一直调用,直到程序退出。

另外,新地址是0x00000001。我宁愿怀疑垃圾

收集物品的移动。



I am not sure it would work, "fixed" can only be used for a short amount
of time, my callbacks are called all the time until the program exits.
Also as the new address is "0x00000001" I would rather suspect a garbage
collecting that a move of the object.





Defenitly Garbarge集合更改内存地址以优化

访问并避免碎片。同样在外翻时收集您的物品将被移至第三代,因此它可以更改地址至少两次

如有必要。


如你所说,你可以只在方法环境或使用

stackalloc固定对象(因为堆栈不在垃圾收集域下)。如果你希望阻止对象在堆上移动你可以使用托管的

数组,托管数组的内容永远不会改变,因为垃圾

收藏家只会移动阵列的位置,但从不接触

的内容。


问候

Salva


" Fabien Penso"写道:
Hi,

Defenitly the Garbarge collection changes the memory address to optimize the
access and avoiding fragmentation. Also on evert collect your object will be
moved to the third generation so it can change the address at least two times
if necessary.

As you said, you can pin objects only on a method enviroment or using
stackalloc (because the stack is not under the garbage collection domain). If
you want to prevent the object to be moved on the heap you can use a managed
array, the contents of the managed array never changes because the garbage
collector will move only the position of the array but never touches the
content.

Regards
Salva

"Fabien Penso" wrote:
萨尔瓦多写道:


如果你想让垃圾收集器不改变你的地址,请使用Fixed <对你的声明发出命令,同样的事情发生在我身上使用不安全的代码。
Hi,

If you want the garbage collector not to change your addresses use the Fixed
command on your declaration, the same happened to me using unsafe code.



我不确定它会起作用,修复只能在短时间内使用,我的回调会一直调用,直到程序退出。
另外,因为新地址是0x00000001。我宁愿怀疑垃圾收集对象的移动。



I am not sure it would work, "fixed" can only be used for a short amount
of time, my callbacks are called all the time until the program exits.
Also as the new address is "0x00000001" I would rather suspect a garbage
collecting that a move of the object.



这篇关于DllImport,回调和垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆