BUG: The System.Collections.Queue.Clone method loses data while cloning objects (829445)



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

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 "Applies to" section.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual C# Projects, and then click Console Application.
  4. In the Name text box, type QueueCloning, and then click OK.
  5. Paste the following line of code at the beginning of the Class1.cs file:
    using System.Collections;
  6. 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("Hello");
    myQueue.Enqueue("There");
    myQueue.Enqueue("World");
    			
    // 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( "The element {0} is : {1}", i, array[ i ]);
    Console.ReadLine();
  7. On the Debug menu, click Start.
  8. You may receive the following output in the console window:The element 0 is :
    The element 1 is : ThereNote In the output, the first element is missing.

Modification Type:MinorLast Reviewed:2/4/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbvs2002sp1sweep kbpending kbCollections kbbug KB829445 kbAudDeveloper