Buggy code:
public TreeItem getParentItem ( ) { } public TreeItem [ ] getSelection ( ) { } int getSelection ( TreeItem [ ] result , int item , int ptr , int index ) { } public int getSelectionCount ( ) { } public TreeColumn getSortColumn ( ) { } public int getSortDirection ( ) { } public TreeItem getTopItem ( ) { } int helpProc ( int inControl , int inGlobalMouse , int inRequest , int outContentProvided , int ioHelpContent ) { } int hitTestProc ( int browser , int id , int property , int theRect , int mouseRect ) { } void hookEvents ( ) { } public int indexOf ( TreeColumn column ) { } public int indexOf ( TreeItem item ) { } int _indexOf ( TreeItem parentItem , TreeItem item ) { } int itemCompareProc ( int browser , int itemOne , int itemTwo , int sortProperty ) { } int itemDataProc ( int browser , int id , int property , int itemData , int setValue ) { } int itemNotificationProc ( int browser , int id , int message ) { } int kEventAccessibleGetNamedAttribute ( int nextHandler , int theEvent , int userData ) { } int kEventControlGetClickActivation ( int nextHandler , int theEvent , int userData ) { } int kEventControlSetCursor ( int nextHandler , int theEvent , int userData ) { } int kEventUnicodeKeyPressed ( int nextHandler , int theEvent , int userData ) { } int kEventMouseDown ( int nextHandler , int theEvent , int userData ) { } void releaseItem ( TreeItem item , boolean release ) { } void releaseItems ( int [ ] ids ) { } void releaseChildren ( boolean destroy ) { } void releaseWidget ( ) { } public void removeAll ( ) { } public void removeSelectionListener ( SelectionListener listener ) { } public void removeTreeListener ( TreeListener listener ) { } void resetVisibleRegion ( int control ) { } public void setInsertMark ( TreeItem item , boolean before ) { } public void selectAll ( ) { } public void select ( TreeItem item ) { } void setBackground ( float [ ] color ) { } int setBounds ( int x , int y , int width , int height , boolean move , boolean resize , boolean events ) { } public void setColumnOrder ( int [ ] order ) { } void setFontStyle ( Font font ) { } public void setHeaderVisible ( boolean show ) { } public void setItemCount ( int count ) { } void setItemCount ( TreeItem parentItem , int count ) { int itemCount = getItemCount ( parentItem ) ; if ( count == itemCount ) return ; setRedraw ( false ) ; int [ ] top = new int [ 1 ] ; int [ ] left = new int [ 1 ] ; OS . GetDataBrowserScrollPosition ( handle , top , left ) ; DataBrowserCallbacks callbacks = new DataBrowserCallbacks ( ) ; OS . GetDataBrowserCallbacks ( handle , callbacks ) ; callbacks . v1_itemNotificationCallback = 0 ; callbacks . v1_itemCompareCallback = 0 ; OS . SetDataBrowserCallbacks ( handle , callbacks ) ; int [ ] ids = ( parentItem == null ) ? childIds : parentItem . childIds ; if ( count < itemCount ) { int [ ] removeIds = new int [ itemCount - count ] ; int removeCount = 0 ; for ( int index = ( ids . length ) - 1 ; index >= count ; index -- ) { int id = ids [ index ] ; if ( id != 0 ) { TreeItem item = _getItem ( id , false ) ; if ( ( item != null ) && ( ! ( item . isDisposed ( ) ) ) ) { item . dispose ( ) ; } else { if ( ( parentItem == null ) || ( parentItem . getExpanded ( ) ) ) { <START_BUG> removeIds [ ( ( removeIds . length ) - removeCount ) ] = id ; <END_BUG> removeCount ++ ; ( visibleCount ) -- ; } } } } if ( ( removeCount != 0 ) && ( removeCount != ( removeIds . length ) ) ) { int [ ] tmp = new int [ removeCount ] ; System . arraycopy ( removeIds , ( ( removeIds . length ) - removeCount ) , tmp , 0 , removeCount ) ; removeIds = tmp ; } if ( ( removeCount != 0 ) && ( ( OS . RemoveDataBrowserItems ( handle , kDataBrowserNoItem , removeCount , removeIds , 0 ) ) != ( OS . noErr ) ) ) { error ( ERROR_ITEM_NOT_REMOVED ) ; } int lastIndex = items . length ; for ( int i = items . length ; i > 0 ; i -- ) { if ( ( items [ ( i - 1 ) ] ) != null ) { lastIndex = i ; break ; } } if ( lastIndex < ( ( items . length ) - 4 ) ) { int length = Math . max ( 4 , ( ( ( lastIndex + 3 ) // 4 ) * 4 ) ) ; TreeItem [ ] newItems = new TreeItem [ length ] ; System . arraycopy ( items , 0 , newItems , 0 , Math . min ( items . length , lastIndex ) ) ; items = newItems ; } } if ( parentItem != null ) parentItem . itemCount = count ; int length = Math . max ( 4 , ( ( ( count + 3 ) // 4 ) * 4 ) ) ; int [ ] newIds = new int [ length ] ; if ( ids != null ) { System . arraycopy ( ids , 0 , newIds , 0 , Math . min ( count , itemCount ) ) ;

Fix:
removeIds [ ( ( ( removeIds . length ) - removeCount ) - 1 ) ] = id ;