[squeak-dev] OrderedCollection>>size fails in debugger

Ben Coman btc at openInWorld.com
Sun Jul 10 16:16:07 UTC 2011


I am writing some code that uses OrderedCollection and was tracing 
through it with the debugger when the debugger itself threw a DNU 
exception.  I narrowed down reproduction of this to executing the 
following line, clicking <Debug> then eight times <Into> :
 >     self halt. OrderedCollection new: 5.

As far as I can determine, the root cause is that 
"OrderedCollectionInspector>>fieldList" calls "object size" which has 
the following implementation:
 >     OrderedCollection>>size
 >        ^ lastIndex - firstIndex + 1

where "lastIndex" is nil (and also "firstIndex" is nil).
To narrow it down further, the error can also be reproduced by executing 
the following:
 > OrderedCollectionInspector openOn: OrderedCollection basicNew.

I fixed this with:
 >     OrderedCollection>>size
 >        lastIndex ifNil: [ ^0 ].
 >        ^ lastIndex - firstIndex + 1

It would be more correct to also have included "firstIndex ifNil: [ ^0 
]" but I assume both conditions occur at the same time, so I left it out 
for performance. 

Not knowing how performance issue are balanced in Squeak, I was 
concerned that the "size" method might be called very often, and that 
even this one extra line might want to be avoided - I assume that  this 
case only occurs in debugging at times when the object has not been 
fully initialized, and this error would not occur from application 
code.  So I went looking for an alternative.  Back in the caller, rather 
than:
 >     OrderedCollection>>fieldList
 >         object ifNil: [ ^ OrderedCollection new].
 >         ^ self baseFieldList ,
 >             (object size <= (self i1 + self i2)
<snip>

this corrected the problem:
 >     OrderedCollection>>fieldList
 >         object ifNil: [ ^ OrderedCollection new].
 >        [ object size ] on: Exception do: [ ^ self baseFieldList ].
 >         ^ self baseFieldList ,
 >             (object size <= (self i1 + self i2)
<snip>
I assume this would also be required for 
OrderedCollection>>selectedObjectIndex. 

However now in the debugger inst-var sub-pane, selecting self shows 
"<error in printString: evaluate "self printString" to debug>, and in so 
doing reveals that the "self size" call fails in:
 >     OrderedCollection>>do: elementBlock separatedBy: separatorBlock
 >         1 to: self size do:
indicating a similar fix is potentially required for the following methods:
    OrderedCollection>>add: newObject afterIndex: index
    OrderedCollection>>add: newObject beforeIndex: index
    OrderedCollection>>at: index ifAbsentPut: block
    OrderedCollection>>collect: aBlock
    OrderedCollection>>copyReplaceFrom: start to: stop with: 
replacementCollection
    OrderedCollection>>makeRoomAtFirst
    OrderedCollection>>makeRoomAtLast
    OrderedCollection>>postCopyFrom: startIndex to: endIndex
    OrderedCollection>>with: otherCollection collect: twoArgBlock
    OrderedCollection>>withIndexCollect: elementAndIndexBlock
which is not as clean as modifying OrderedCollection>>size, and still a 
bit uncertain.

What is the community's thoughts on this?

Finally, if any of these are desirable fixes, I would appreciate using 
my first contribution as a practice for submitting to the Inbox (if 
someone could direct me to some documentation that describes how to do 
this.)

cheers, Ben








More information about the Squeak-dev mailing list