Re: Why does Swift crash my CFBitVector ?
Re: Why does Swift crash my CFBitVector ?
- Subject: Re: Why does Swift crash my CFBitVector ?
- From: Bavarious <email@hidden>
- Date: Sun, 06 Jul 2014 06:47:56 -0300
On 6 Jul 2014, at 06:14, Gerriet M. Denkmann <email@hidden> wrote:
> class CrashVector
> {
> let lengthInBits : CFIndex = 133 // must be > 128
> let badIndex : CFIndex = 129 // 128 ≤ badIndex < lengthInBits
>
> func makeCrash()
> {
> let bitVector = CFBitVectorCreateMutable( kCFAllocatorDefault, lengthInBits )
> let bitCount = CFBitVectorGetCount( bitVector )
> println("got BitVector with \(bitCount) bits") // why does it say 0 instead of 133 ?
>
> println("makeCrash will set bit \(badIndex) in BitVector of size \(lengthInBits)") // why does this crash ?
> CFBitVectorSetBitAtIndex( bitVector, badIndex, 1 )
> }
> }
>
> What am I doing wrong?
You’re confusing capacity (the second argument to CFBitVectorCreateMutable(), which means the *maximum* number of values in that vector) with count (the *actual* number of values in the vector). The documentation for CFBitVectorCreateMutables() mentions that
‘The bit vector starts empty and can grow to this number of values [its capacity]’
Since the bit vector starts empty, its count is 0.
Note that CFBitVectorSetBitAtIndex() can only set values in indexes up to count - 1, which is why you’re getting a crash. Before setting that bit at badIndex, you need to make sure that the count is at least badIndex+1. You can do this by calling CFBitVectorSetCount() and, in your case, I suppose you want to set count to lengthInBits:
CFBitVectorSetCount(bitVector, lengthInBits)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden