How can I add an item to a list if that item is essentially a pointer and avoid changing every item in my list to the newest instance of that item?
Here's what I mean:
I am doing image processing, and开发者_开发问答 there is a chance that I will need to deal with images that come in faster than I can process (for a short period of time). After this "burst" of images I will rely on the fact that I can process faster than the average image rate, and will "catch-up" eventually.So, what I want to do is put my images into a <List>
when I acquire them, then if my processing thread isn't busy, I can take an image from that list and hand it over.
My issue is that I am worried that since I am adding the image "Image1" to the list, then filling "Image1" with a new image (during the next image acquisition) I will be replacing the image stored in the list with the new image as well (as the image variable is actually just a pointer).
So, my code looks a little like this:
while (!exitcondition)
{
if(ImageAvailabe())
{
Image1 = AcquireImage();
ImgList.Add(Image1);
}
if(ImgList.Count > 0)
{
ProcessEngine.NewImage(ImgList[0]);
ImgList.RemoveAt(0);
}
}
Given the above, how can I ensure that:
- I don't replace all items in the list every time Image1 is modified. - I don't need to pre-declare a number of images in order to do this kind of processing. - I don't create a memory devouring monster.Any advice is greatly appreciated.
Just reinitialize:
Replace
Image1 = AcquireImage();
with
Image1 = new Image(AcquireImage());
or just say
ImageList.Add(new Image(AcquireImage()));
Your code is correct. None of your above code affects previously added images. The line:
Image1 = AcquireImage();
puts the reference (to an image) returned from AcquireImage into the Image1 reference variable. Then:
ImgList.Add(Image1);
adds that reference to your list. Changing your Image1 reference variable does not affect references already in the list.
Conceptually, your code will be fine. The important element is that AcquireImage() allocates a new instance for each incoming image.
If Image1 were a pointer, you would have a problem - however a C# reference is not a pointer.
If I understand what you're saying correctly, you want to be able to re-use a variable without overwriting its existing data. The good news is that you don't need to change anything. You're partially correct when you say that Image1
is a pointer: it's a reference to whichever image it's pointing to at the time. When you allocate it:
Image1 = AcquireImage();
you're not overwriting the contents of the existing image, but changing the reference so it points to the new image. Assuming AcquireImage
is working correctly and returns a new image every time, rather than overwriting the previous one, the above code will discard the existing reference in favour of the new one. However, as you've added it to the list already, a reference to the image is retained somewhere in your code, and so it will not be lost.
精彩评论