Fixing Error EXC_RESOURCE RESOURCE_TYPE_MEMORY limit=200mb in File Provider Extension

2.9k Views Asked by At

In my File Provider Extension I have defined a custom action that displays a ViewController with a collectionView getting data from DiffableDataSource.

Each cell is configured to adjust export settings for a PDF file. In the process of preparing the PDF files to be exported I convert them to images using a renderer. The code I use is this.

if let page = document.page(at: pageIndex) {
        let pageRect = page.getBoxRect(.mediaBox)
        let renderer = UIGraphicsImageRenderer(size: pageRect.size)
        
        var img = renderer.image { context in
            UIColor.white.set()
            context.fill(pageRect)
            
            context.cgContext.translateBy(x: 0.0, y: pageRect.size.height)
            context.cgContext.scaleBy(x: 1.0, y: -1.0)
            context.cgContext.drawPDFPage(page)
        }
        img = MUtilities.shared.imageRotatedByDegrees(oldImage: img, deg: CGFloat(page.rotationAngle))
        let image = img.jpegData(compressionQuality: quality)
        do {
            try? FileManager.default.createDirectory(at: imagePath.deletingLastPathComponent(), withIntermediateDirectories: true)
            try image?.write(to: imagePath)
        } catch {
            fatalError("Unable to write jpg to file, \(error.localizedDescription)")
        }
    }
}

The code works fine in the Simulator and displays the collectionView without any issue. When I test the extension on my device with iOS 16.0 I get the error:

Thread 1: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit=200 MB, unused=0x0)

on the line:

var img = renderer.image { context in

How can I fix this error?

1

There are 1 best solutions below

1
Nasser A On

The error occurs within the context of the File Provider UI custom action (FPUIActionExtensionViewController). This led me to investigate all the code within that context to find any memory leaks or excessive memory usage.

I found a call to the realm database for all objects of a certain type outside the prepare function. I moved the call into the prepare function and limited the call with a .filter on the returned array. That fixed the problem for me.