[squeak-dev] OrderedCollection>>size fails in debugger
Levente Uzonyi
leves at elte.hu
Mon Jul 11 20:00:14 UTC 2011
On Mon, 11 Jul 2011, Ben Coman wrote:
> Levente Uzonyi wrote:
>> On Mon, 11 Jul 2011, Ben Coman wrote:
>>> 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).
>> 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>
>> Yes, OrderedCollectionInspector should be changed, because that's what
>> causes the problem. I'd simply create an #objectSize method for it and use
>> that instead of object size.
>> Levente
>>> cheers, Ben
>
> Do you not like leaving the line "[ object size ] on: Exception do: [ ^ self
> baseFieldList ]" where it is ?
I don't. :)
>
> Was your thought to create #objectSize in OrderedCollection or
> OrderedCollectionInspector? At first I assumed you meant
OrderedCollectionInspector.
> OrderedCollection>>objectSize since OrderedCollectionInspector would still
> not know whether lastIndex was nil, and would seem to require the same
> exception handling. But perhaps you meant that
> OrderCollectionInspector>>objectSize should just wrap the exception handling,
> and so would be...
>
> OrderCollectionInspector>>objectSize
> [ object size ] on: Exception do: [ ^ 0 ].
> ^ object size.
Kinda, but see what Nicolas wrote. Using his version (besides not
evaluating "object size" twice for initialized objects) uses the stack
optimally - no extra pushes, just use what's on the top. Also catching all
Exceptions is unwelcome, you should only catch errors.
>
> OrderCollectionInspector>>fieldList
> object ifNil: [ ^ OrderedCollection new].
> ^ self baseFieldList ,
> (self objectSize <= (self i1 + self i2)
> ifTrue: [(1 to: self objectSize)
> collect: [:i | i printString]]
> ifFalse: [(1 to: self i1) , (self objectSize - (self i2-1) to:
> self objectSize)
> collect: [:i | i printString]])
>
> Although I wonder if a cleaner implementation that exception handling could
> be...
>
> OrderCollection>>hasSize
> lastIndex ifNil: [ ^false ].
> firstIndex ifNil: [ ^false ].
> ^true.
>
> OrderCollectionInspector>>objectSize
> object hasSize ifTrue: [^ object size ] ifFalse: [ ^0 ].
I think the other solution is better, because it doesn't extend/modify
OrderedCollection.
Levente
>
>
>
>
>
>
>
>
More information about the Squeak-dev
mailing list
|