Microsoft KB Archive/829445

= BUG: The System.Collections.Queue.Clone method loses data while cloning objects =

Article ID: 829445

Article Last Modified on 2/4/2006

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



SYMPTOMS
The Queue class is a Microsoft .NET Framework Class Library class. It represents a first-in, first-out collection of objects. However, if you use the Queue.Clone method to clone a queue, and if the call to the Queue.Dequeue method is made before the call to Queue.Clone method is made, the data is lost.



CAUSE
A bounded buffer is used to implement the Queue class. The Clone method uses the beginning of the buffer, instead of the head pointer, as the starting point. When the Dequeue method is called, the first element is removed. However, the beginning of the buffer remains the same. Therefore, the first element appears as an empty element and the last element is lost in the cloning process.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Under Project Types, click Visual C# Projects, and then click Console Application. In the Name text box, type QueueCloning, and then click OK.  Paste the following line of code at the beginning of the Class1.cs file: using System.Collections;   In the Class1.cs file, paste the following code in the Main function: Queue myQueue = new Queue; // Add some elements to the queue. myQueue.Enqueue(&quot;Hello&quot;); myQueue.Enqueue(&quot;There&quot;); myQueue.Enqueue(&quot;World&quot;); // Remove an element from the queue. myQueue.Dequeue; // Clone the queue by using the Clone method. Queue qClone = (Queue)myQueue.Clone; // Get the elements into an array to print. object[] array = qClone.ToArray;

// Print the array objects. for( int i = 0; i < array.Length; i++ ) Console.WriteLine( &quot;The element {0} is : {1}&quot;, i, array[ i ]); Console.ReadLine; </li> On the Debug menu, click Start.</li> You may receive the following output in the console window:

The element 0 is :

The element 1 is : There

Note In the output, the first element is missing.</li></ol>

Keywords: kbbug kbcollections kbpending kbvs2002sp1sweep kbvs2005swept kbvs2005doesnotapply KB829445

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.