Pregunta

while testing my winform app I noticed that, after a fixed time, it crushes and it stop, of course, working. I supposed it could crush because it starts to occupy a lot of memory and, after this time, the system will stop my application and release all the resources occupied. I found that using task manager and monitoring the Physical memory panel. In my app I have a lot of managed array and I keep on updating the values of them real time. Maybe, I'm not handling the right way those arrays and slowly but surely they start to occupy all the memory avaible.
My idea was to print somewhere the address of the array and discover if it changes its location. I declared my arrays just like that:

array<double, 1>^Array = gcnew array<double, 1>(1000);

I know I can find the address of Array in this way, in a console application:

    array<int, 1>^Array = gcnew array<int,1>(1000);
    for (int i = 0; i<Array->Length; i++) {
        Array[i] = i;
    }

    std::cout << "Address of Array: " << &Array;
    Console::WriteLine();

    Console::ReadLine();
    return 0;

It obviously works. Now, if I try to find the address of the array in a winform application, something goes wrong and I don't know why. For instance, I would have a button that should show up a MessageBox or set the text of a Label with the address of the array, but I can get only a boolean value ( compiler warning c4305 and compiler warning c4800 ). I tried interior_ptr and pin_ptr but I didn't have any result.
Does a way exist to know the address of a managed array in a winforms application?
Thanks a lot.

¿Fue útil?

Solución

You can use Marshal Class.

Marshal.UnsafeAddrOfPinnedArrayElement(arr, 0);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top