Click to See Complete Forum and Search --> : after memcpy


rowgram
December 9th, 2005, 05:55 PM
I've got a function that outputs an array. I'm calling it inside my code as :
function A (...,...,real_data).

Inside this function, I've got the following statement inside a loop with index i :

memcpy(&data[i], &source, sizeof(source));

My understanding is that this copies the data from buffer source to buffer data[i]. data is a struct & contains an int counter that I need to display immediately, so I do :

textBox1->set_Text(Convert::ToString(data[i].counter));

but I get 0. Is there some lag between when a destination buffer is copied to & when it's contents can be accessed ? How do I access counter ?

However, textBox1->set_Text(Convert::ToString(i));
works correctly - I see the correct values being displayed (the index scrolls).
Also when I exit out of the function, data - the output of the function - is now called real_data (see above) & accessing it via :

textBox1->set_Text(Convert::ToString(real_data[i].counter));

works fine.
But by then there's been a large delay between the data that has been received & when it is displayed. I guess I could create a 2nd thread, but I've never done this before, it looks daunting, & it seems that in this looping structure a set_Text call after memcpy could work without the need for threads (I'm sure I'll get a chance to work on a multi-threaded app, but I'm hoping to do this later when I have more time).

cilu
December 9th, 2005, 06:06 PM
[ redirected thread ]

NoHero
December 9th, 2005, 06:30 PM
You cannot and must not use old C functions on managed heap. Data on the managed heap is likely to be moved, and unknown in it's way it is decoded, thus you are more likely to destroy it.

To copy data use either the Clone() method or the normal assign operator!

rowgram
December 10th, 2005, 07:27 PM
So, if I understand it correctly, I should replace memcpy with Clone() or assign operator.
Is it fair to say that only .NET functions should be used to place data on the managed heap ? Besides copying data, what other operations would require interaction with the managed heap ?

NoHero
December 11th, 2005, 05:58 AM
So, if I understand it correctly, I should replace memcpy with Clone() or assign operator.

Yes... Clone() creates a copy of the object and the assignment operator copies the reference. An object on the managed heap is only released when there is not reference left which points to it.

Is it fair to say that only .NET functions should be used to place data on the managed heap?

Exactly. Any operation with managed objects cause an operation on the managed heap, because any managed object relies on the managed heap.

Besides copying data, what other operations would require interaction with the managed heap ?

As said copying, reading from managed objects (e.g. you retreive a property from a managed class), writing managed objects (e.g. you set a property from a managed class). Any operation which is done on managed objects.