java.util.LinkedList causing infinite loop/OutOfMemoryError
<p>I'm implementing Cloneable with one of my classes, and I need to make a shallow copy (yes, only shallow) of a java.util.LinkedList to do it.  I tried using </p>

<pre><code>myList.clone() //myList is a java.util.LinkedList&lt;myType&gt;
</code></pre>

<p>but that was causing my program to stall, so I switched to using the copy constructor:</p>

<pre><code>new LinkedList&lt;myType&gt;(myList)
</code></pre>

<p>but it still hangs up. Now this was only happening with a myType that was an enum, but now I just had the same error with a non- enum type, though now that I think of it that type has an inner enum type.  this has happened both on Sun's jdk 1.6 and openjdk 1.6.  I'm sure I'm doing something wrong, but I can't figure out what I'd be doing that would break LinkedList.  It's not throwing an Exception until it eventually runs out of memory, (usually I kill it before this happens).  Anyway, any ideas what might be causing this?</p>

<p>here is the clone method:</p>

<pre><code>public Note clone(){
 List&lt;Accidental&gt; retAcc=new LinkedList&lt;Accidental&gt;();
 for(Accidental acc:accidentals)retAcc.add(acc.clone());
 return new Note(retAcc,position,restFlag,new LinkedList&lt;TieType&gt;(beginTies),new LinkedList&lt;TieType&gt;(endTies),tripFlag,duration);
}
</code></pre>

<p>TieType is an enum type, Accidental implements Cloneable and its clone() method is reproduced below.  position, restFlag, tripFlag, and duration are all primitives</p>

<pre><code>public Accidental clone(){
 return new Accidental(acType,fltPosition);

}
</code></pre>

<p>acType is an inner enum type, fltPosition is is a float, these initialize the only two fields in accidental.</p>

<p>the problem is happening in the last line of the Note.clone() method when I call the LinkedList copy constructor.  This used to be a call to LinkedList.clone, but I changed it in an attempt to avoid this problem.  Here is the stack trace for this error (there is no error here, as I suspended the thread when it froze, rather than waiting for it to run out of memory, but as you can see, it was in the middle of the LinkedList copy constructor)</p>

<blockquote>
  <p>LinkedList.(Collection) line: 115<br>
  Note.clone() line: 86<br>
  Note.simplify(int) line: 98<br>
  Note.split(int, List, List) line: 179<br>
  ManagedPart$Measure.adjustRemaining(int) line: 378 
  ManagedPart$Measure.add(Note) line: 349<br>
  ManagedPart$Measure.access$1(ManagedPart$Measure, Note) line: 345<br>
  ManagedPart.addNote(Note) line: 216<br>
  PartEditor$EditPanel$3.actionPerformed(ActionEvent) line: 104<br>
  JButton(AbstractButton).fireActionPerformed(ActionEvent) line: 2012<br>
  AbstractButton$Handler.actionPerformed(ActionEvent) line: 2335 
  DefaultButtonModel.fireActionPerformed(ActionEvent) line: 404<br>
  DefaultButtonModel.setPressed(boolean) line: 259<br>
  BasicButtonListener.mouseReleased(MouseEvent) line: 253<br>
  JButton(Component).processMouseEvent(MouseEvent) line: 6108<br>
  JButton(JComponent).processMouseEvent(MouseEvent) line: 3276<br>
  JButton(Component).processEvent(AWTEvent) line: 5873<br>
  JButton(Container).processEvent(AWTEvent) line: 2105<br>
  JButton(Component).dispatchEventImpl(AWTEvent) line: 4469<br>
  JButton(Container).dispatchEventImpl(AWTEvent) line: 2163<br>
  JButton(Component).dispatchEvent(AWTEvent) line: 4295<br>
  LightweightDispatcher.retargetMouseEvent(Component, int, MouseEvent) line: 4461<br>
  LightweightDispatcher.processMouseEvent(MouseEvent) line: 4125 
  LightweightDispatcher.dispatchEvent(AWTEvent) line: 4055<br>
  MusedDesktopClient(Container).dispatchEventImpl(AWTEvent) line: 2149<br>
  MusedDesktopClient(Window).dispatchEventImpl(AWTEvent) line: 2478<br>
  MusedDesktopClient(Component).dispatchEvent(AWTEvent) line: 4295<br>
  EventQueue.dispatchEvent(AWTEvent) line: 604<br>
  EventDispatchThread.pumpOneEventForFilters(int) line: 275<br>
  EventDispatchThread.pumpEventsForFilter(int, Conditional, EventFilter) line: 200<br>
  EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: 190<br>
  EventDispatchThread.pumpEvents(int, Conditional) line: 185 
  EventDispatchThread.pumpEvents(Conditional) line: 177<br>
  EventDispatchThread.run() line: 138    </p>
</blockquote>

<p>Also recently, I've had a similar problem with the construction of the LinkedList in the first line of Note.clone() i. e.</p>

<pre><code>List&lt;Accidental&gt; retAcc=new LinkedList&lt;Accidental&gt;();
</code></pre>

<p>here is the stack trace for that:</p>

<blockquote>
  <p>Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    at java.util.LinkedList.(LinkedList.java:95)
    at com.mused.util.noteMgmt.Note.clone(Note.java:84)
    at com.mused.util.noteMgmt.Note.simplify(Note.java:98)
    at com.mused.util.noteMgmt.Note.split(Note.java:179)
    at 
  com.mused.util.noteMgmt.ManagedPart$Measure.adjustRemaining(ManagedPart.java:378)
    at com.mused.util.noteMgmt.ManagedPart$Measure.insert(ManagedPart.java:335)
    at com.mused.util.noteMgmt.ManagedPart.insertNote(ManagedPart.java:223)
    at com.mused.gui.editor.PartEditor.currentIndexChanged(PartEditor.java:161)
    at com.mused.gui.NoteViewer.fireIndexChangeEvent(NoteViewer.java:178)
    at com.mused.gui.NoteViewer.setCurrentIndex(NoteViewer.java:417)
    at com.mused.gui.NoteViewer.updateSelected(NoteViewer.java:627)
    at com.mused.gui.NoteViewer.mouseMoved(NoteViewer.java:725)
    at java.awt.Component.processMouseMotionEvent(Component.java:6153)
    at javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3294)
    at java.awt.Component.processEvent(Component.java:5877)
    at java.awt.Container.processEvent(Container.java:2105)
    at java.awt.Component.dispatchEventImpl(Component.java:4469)
    at java.awt.Container.dispatchEventImpl(Container.java:2163)
    at java.awt.Component.dispatchEvent(Component.java:4295)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4138)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
    at java.awt.Container.dispatchEventImpl(Container.java:2149)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4295)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:604)
    at 
  java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at 
  java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)</p>
</blockquote>
