UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

2      

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

2      

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

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.