0

I'm using an external library written in C that applies filters to an image. It receives the original image pixels as an array of float values and writes the new image float values on another array. One specific filter creates a mask to be used by a sharpen filter, and I don't know why but it only works with smaller images, while bigger images (a million pixels more or less) cause the application to crash, giving an EXC_BAD_ACCESS error right after executing the wrapper that calls the external lib function. Is there anything wrong with my code, which creates the parameters that will be passed to the external lib, or is the problem likely in the external library?

    func allocateMaskArgs() { //method to allocate mask parameters in memory, to be used by sharpen filter

        let size = originalImageMatrix.params[0] * originalImageMatrix.params[1] //height of the image multiplied by width

        if maskBuffer != nil {
            self.maskBuffer.deallocate()
        }
        maskBuffer = UnsafeMutablePointer<UnsafeMutablePointer<Float>?>.allocate(capacity: 2)

        let constantPointer: UnsafeMutablePointer<Float>?
        constantPointer = UnsafeMutablePointer<Float>.allocate(capacity: 1)
        constantPointer!.advanced(by: 0).pointee = 4.0 //this is the intensity value of the mask, it should always be 4
        maskBuffer.advanced(by: 0).pointee = constantPointer

        let maskArrayPointer: UnsafeMutablePointer<Float>? //this is where the mask created by createMask() should be stored by the external lib function
        maskArrayPointer = UnsafeMutablePointer<Float>.allocate(capacity: size)
        maskBuffer.advanced(by: 1).pointee = maskArrayPointer
    }

    func createMask() { //creates sharpen mask and stores in maskBuffer
        var input_params: [Int] = [self.originalImageMatrix.params[0], self.originalImageMatrix.params[1]]
        var output_params: [Int] = [self.newImageMatrix.params[0], self.newImageMatrix.params[1]]

        self.imagingAPI.applyFilters(self.originalImageMatrix.v!, input_params: &input_params, output_image: self.newImageMatrix.v!, output_params: &output_params, filter_id: 11, args: self.maskBuffer)
    }

The external library function is accessed through this wrapper function:

- (void) applyFilters: (float *) input_image input_params: (long *) input_params output_image : (float *) output_image output_params : (long *) output_params filter_id : (int) filter_id args : (float**) args;
georgebp
  • 193
  • 3
  • 17
  • try to unwrap `self.originalImageMatrix.v` instead of force-unwrapping it and see if it returns nil – timbre timbre Nov 13 '19 at 17:49
  • it doesn't return nil, it unwraps the array normally – georgebp Nov 13 '19 at 18:15
  • What is the console output when you get EXC_BAD_ACCESS? And please show the definition of `originalImageMatrix` and `newImageMatrix`, and the code how you allocate `v`. – OOPer Nov 13 '19 at 19:51
  • No output, only the EXC_BAD_ACCESS message: https://i.ibb.co/dt882k2/Captura-de-Tela-2019-11-13-s-17-27-10.png – georgebp Nov 13 '19 at 20:57
  • I believe the allocation to v is correct because I can convert the image matrix to CGImage and display it normally, and I can also use other filters passing the same v pointers to the library. From what I've been informed by the makers of the library, it creates auxiliary arrays for doing the math. Is it possible that iOS memory management is changing memory positions as more memory is needed, and that's causing a conflict with the library function? – georgebp Nov 13 '19 at 21:02
  • I should also point out that a similar application is being created for Android that also uses the library, and it doesn't have this problem, though the Android devices we use for testing have a lot more memory than the iOS devices. – georgebp Nov 13 '19 at 21:03

0 Answers0