WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

ContiguousArray memory storage

Forums > Swift

I working on a Mac App where I'm using an Array or ContiguousArray to provide memory for a Bitmap. I have a struct that emulates the bitmap pixel

public struct PixelColor {
    public var r, g, b, a: UInt8

    public init(r: UInt8, g: UInt8, b: UInt8, a: UInt8 = 255) {
        self.r = r
        self.g = g
        self.b = b
        self.a = a
    }

}

public extension PixelColor {
    static let clear = PixelColor(r: 0, g: 0, b: 0, a: 0)
    static let black = PixelColor(r: 0, g: 0, b: 0)
    static let white = PixelColor(r: 255, g: 255, b: 255)
    static let red = PixelColor(r: 217, g: 87, b: 99)
    static let green = PixelColor(r: 153, g: 229, b: 80)
    static let yellow = PixelColor(r: 251, g: 242, b: 54)
}

If I initialize it as

Array(repeating: .black, count: WaterfallSettings.columns * WaterfallSettings.rows)

It works as expected, but if I switch it to a ContigousArray, I get a EXC_BAD_ACCESS error. Also, if instead I create it as a two-dimensional array:

Array(repeating: Array(repeating: .black, count: WaterfallSettings.columns), count: WaterfallSettings.rows)

I get similar EXC_BAD_ACCESS error

I think the problem is that the ContiguousArray(repeating: count:) or the Array(repeating: Array(repeating: count:), count:) are initializing the arrays, but using one instance of the .black struct or the array and pointing the other references to it. Once you actually update an element CopyOnWrite cleans things up, but messes up the pointer that my Bitmap is using.

Is there a way to force Array(repeating: count:) to allocate space for all the elements and make separate copies?

TIA, Mark

   

I copied your code into Playgrounds and added the following:

// One dimensional array
var oneDimensionalArray = ContiguousArray(repeating: PixelColor.black, count: 20 * 20)
oneDimensionalArray[47].r = 42   // customize pixel 47
print (oneDimensionalArray[45])  // shows default pixel
print (oneDimensionalArray[47])  // shows customized pixel

// Two dimensional array
var twoDimensionalArray = ContiguousArray(repeating: ContiguousArray(repeating: PixelColor.black, count: 50), count: 20)
twoDimensionalArray[10][10].g = 42  // customize pixel [10][10]
print(twoDimensionalArray[10][10] )

Playground provided reasonable output:

PixelColor(r: 0, g: 0, b: 0, a: 255)
PixelColor(r: 42, g: 0, b: 0, a: 255)
PixelColor(r: 0, g: 42, b: 0, a: 255)

Notice I used the struct's static color with its full name: PixelColor.black

   

I agree with your findings. I think we are seeing copy on write behavior, which is what Swift does. Before the assigment, Swift points to an array where all the elements are references to the single repeating element. Once you do the assignment, Swift copies that element (or the entire array, I'm not sure how to figure that out) and gives it the new value, breaking the link to the other elements.

For "normal" usage this is what we want and the behavior is as expected.

My problem is that I am assigning the memory occupied by the array to the data property of a CGContext. When Swift does the copy on write and moves the data in memory, the CGContext's data property is not updated, so it now points to unallocated memory and crashes.

If there were a way to initialize the array so that all the elements were already their own unique PixelColor struct, assigning a new value to one of them would not cause a copy on write and not move the array in memory, but the only way I can think of to do that initialization would be to hard code each element.

Granted, for my code to work, I am assuming that Swift will never move the memory, and I don't know if I can guarantee that. I've rewritten my code to use use .data property in the CGContext every time I want to update it. That way if it has moved, my code is working on the right memory, but I can't access the Pixel's using X and Y coordinates (the subscripts of the 2d array).

Mark

   

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.