I have a
我有一个
public ref class Test
inside this class, I have:
在这堂课里面,我有:
int frameWidth;
int frameHeight;
int frameStride;
When I try to compile this, I get the error:
当我尝试编译它时,我收到错误:
error C2664: 'GetImageSize' : cannot convert parameter 1 from 'cli::interior_ptr<Type>' to 'int *'
GetImageSize is a native function and it works only if I move the declaration of the 3 ints above to outside the class or inside the block that calls GetImageSize.
GetImageSize是一个本机函数,只有当我将上面的3个int的声明移到类外部或调用GetImageSize的块内时,它才有效。
How can I solve this?
我怎么解决这个问题?
Those 3 ints needs to be accessible by more than one function within the class, right now I made it work because I moved them to outside the class, but it's not the right thing to do I believe since they become global.
这3个整数需要由班级内的多个职能部门访问,现在我已经将其工作了,因为我把它们移到了课外,但是我认为这不是正确的事情,因为它们变得全球化。
1 个解决方案
#1
According to this post, the reason you are seeing this is because the ints are inside a ref class which can be moved around the heap by the garbage collector at will, the address of the ints could change and you wouldn't be told.
根据这篇文章,你看到这个的原因是因为int是一个ref类,它可以被垃圾收集器随意移动到堆中,int的地址可能会改变,你不会被告知。
To overcome this, you need to tell the GC not to move the objects while you are using them. To do this you need to use
为了解决这个问题,您需要告诉GC在您使用它们时不要移动它们。要做到这一点,你需要使用
pin_ptr<int*> pinnedFrameWidth = &frameWidth;
then pass pinnedFrameWidth into GetImageSize. The pin_ptr will be automatically cast to int* when passed into the method.
然后将pinnedFrameWidth传递给GetImageSize。传递给方法时,pin_ptr将自动转换为int *。
You need to be careful when using pin_ptr. Because the GC can't move the instance of Test class around during a collection, the managed heap can become fragmented and, eventually, performance can suffer. Ideally pin as few objects for the least amount of time possible.
使用pin_ptr时需要小心。由于GC在集合期间无法移动Test类的实例,因此托管堆可能会碎片化,最终性能会受到影响。理想情况下,尽可能少地将对象固定在极少的时间内。
There is a brief discussion of pin pointers in this .Net Rocks show.
这个.Net Rocks节目中有针脚指针的简短讨论。
#1
According to this post, the reason you are seeing this is because the ints are inside a ref class which can be moved around the heap by the garbage collector at will, the address of the ints could change and you wouldn't be told.
根据这篇文章,你看到这个的原因是因为int是一个ref类,它可以被垃圾收集器随意移动到堆中,int的地址可能会改变,你不会被告知。
To overcome this, you need to tell the GC not to move the objects while you are using them. To do this you need to use
为了解决这个问题,您需要告诉GC在您使用它们时不要移动它们。要做到这一点,你需要使用
pin_ptr<int*> pinnedFrameWidth = &frameWidth;
then pass pinnedFrameWidth into GetImageSize. The pin_ptr will be automatically cast to int* when passed into the method.
然后将pinnedFrameWidth传递给GetImageSize。传递给方法时,pin_ptr将自动转换为int *。
You need to be careful when using pin_ptr. Because the GC can't move the instance of Test class around during a collection, the managed heap can become fragmented and, eventually, performance can suffer. Ideally pin as few objects for the least amount of time possible.
使用pin_ptr时需要小心。由于GC在集合期间无法移动Test类的实例,因此托管堆可能会碎片化,最终性能会受到影响。理想情况下,尽可能少地将对象固定在极少的时间内。
There is a brief discussion of pin pointers in this .Net Rocks show.
这个.Net Rocks节目中有针脚指针的简短讨论。