Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
<p>In my swift project, I have two classes that work together to hold Pixel values of an image to be able to modify red, green, blue and alpha values. An <code>UnsafeMutableBufferPointer</code> holds lots of bites that are comprised of the Pixel class objects.</p>

<p>I can interact with that the class that holds the <code>UnsafeMutableBufferPointer&lt;Pixel&gt;</code> property. I can access all of the properties on that object and that all works fine. The only problem I'm having with the <code>UnsafeMutableBufferPoint&lt;Pixel&gt;</code> is trying to loop through it with my Pixel object and it keeps crashing with the Thread 1: <code>EXC_BAD_ACCESS (code=EXC_I386_GPFLT)</code> exception.</p>

<pre><code>init!(image: UIImage)
{
    _width = Int(image.size.width)
    _height = Int(image.size.height)

    guard let cgImage = image.cgImage else { return nil }

    _width = Int(image.size.width)
    _height = Int(image.size.height)
    let bitsPerComponent = 8

    let bytesPerPixel = 4
    let bytesPerRow = _width * bytesPerPixel
    let imageData = UnsafeMutablePointer&lt;Pixel&gt;.allocate(capacity: _width * _height)
    let colorSpace = CGColorSpaceCreateDeviceRGB()

    var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue
    bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue &amp; CGBitmapInfo.alphaInfoMask.rawValue
    guard let imageContext = CGContext(data: imageData, width: _width, height: _height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { return nil }
    imageContext.draw(cgImage, in: CGRect(origin: CGPoint.zero, size: image.size))

    _pixels = UnsafeMutableBufferPointer&lt;Pixel&gt;(start: imageData, count: _width * _height)
}
</code></pre>

<p>This function is the part that is crashing the program. The exact part that is crashing is the for loop that is looping through the rgba.pixels. rgba.pixels is the UnsafeMutableBufferPointer.</p>

<pre><code>func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
    let image: UIImage = info[UIImagePickerControllerEditedImage] as! UIImage!

    let rgba = RGBA(image: image)!

    for pixel in rgba.pixels
    {
        print(pixel.red)
    }

    self.dismiss(animated: true, completion: nil);
}
</code></pre>

<p>This is the constructor where I create the <code>UnsafeMutableBufferPointer&lt;Pixel&gt;</code>. Is there an easier way to do this and still be able to get the RBGA values and change them easily.</p>

<p>The Pixel class is a <code>UInt32</code> value this is split into four <code>UInt 8</code> values.</p>

<p>Am I using the wrong construct to hold those values and if so, is there a safer or easier construct to use? Or am I doing something wrong when accessing the Pixel values?</p>