How do I resolve memory leaks that occur when I use the UIActivityViewController?

Asked 2 years ago, Updated 2 years ago, 97 views

I am developing an application with swift.
Memory leaks when using the UIActivityViewController to display activity dialogs.
I'm in trouble because I don't know how to solve the leak.Please tell me how to solve this problem.
I'll put the code at the end.(Partial rewriting)

The action is to send the output CSV file via AirDrop or mailer as shown in the activity dialog.
I have attached the ss that I checked with the instruments, which I ran on iphone(ios8.4) and ipadmini(ios9.3.4).I went to the screen where each dialog was displayed and opened and closed the dialog 5 times.

Run on iphone

↑ On the iphone, NSMutableArray seems to be leaking in _UIAlertControllerCommmonInit.There are two leaks in this ss, but sometimes they leak as many times as they open and close.

↓ For ipad ss, I will put 3 sheets with different stack trace contents.(Only the IsImage and IsContact parts are different like IsPDF.)
On ipad, NSCFSstring is leaking like crazy in "CSStringCopyCFSString".Furthermore, I am concerned about the fact that the heap is high every time it opens and closes (is there anything left even if I close it?).Please let me know if there are any causes or workarounds for this as well.Thank you for your cooperation.

Run with ipadmini(1/3)

Run with ipadmini (2/3)

Run with ipadmini (3/3)

// Button Press Action (CSV File Creation + Transfer)
funtapButton(){

    // Output CSV file
    letstrFullPath: String=outputCSVFile("sample.csv")
    print("csvpath:" + strFullPath)

    // (Output Success) Activity Dialog Display
    if strFullPath!="{
        _ad=MyAd()// Load Ad

        let body: String = NSLocalizedString ("MSG_SEND", comment: "body wording")
        let file = NSURL (fileURLWithPath:strFullPath)
        let handler: (Bool->Void) = { [unknown self] (completed) in
            if completed == true {
                self._ad?.show(self)
                print("CSV transmission complete!")
            }
        }
        dispatchDialog(self, items: [body, file], btn:btnAction, arrow:.Up, handler:handler)

    // (Failed Output) Error Message Display
    } else{
        dispatchErrMsg(self, strMsg:NSLocalizedString("MSG_FILE_ERR", comment: "Error Occurred"))

    }
}

/// Display Activity Dialog
class func dispDialog (vc: UIViewController, items: [AnyObject], btn: UIBarButtonItem, arrow: UIPoverArrowDirection=.Unknown, handler: (Bool->Void)!=nil) {
    let dialog = UIAactivityViewController (activityItems: items, applicationActivities:nil)

    // Activity Type Not Used
    let excluded = [
        UIActivityTypeCopyToPasteboard,
        UIActivityTypeMessage,
    ]
    dialog.excludedActivityTypes=excluded

    // Set post-completion actions
    if handler!=nil{
        dialog.completionWithItemsHandler={(activityType:String?,completed:Bool,returnItems:[AnyObject]?,error:NSError?)in
            print("activityType:\(activityType)")
            handler(completed)
            return
        }
    }

    // For iPad And Universal Device
    iflet popCtrl=dialog.popoverPresentationController{
        popCtrl.sourceView=vc.view
        popCtrl.permittedArrowDirections=arrow
        popCtrl.barButtonItem=btn
    }

    vc.presentViewController(dialog, animated:true, completion:nil)
}

Added image.
Add iphone autoreleasepool

swift memory-leaks

2022-09-30 16:00

1 Answers

Objective-C reduces leaks by tightening blocks that cause memory leaks in @autoreleasepool, so

autoreleasepool{
    dispatchDialog(self, items: [body, file], btn:btnAction, arrow:.Up, handler:handler)
}

Also, why don't you combine the displayDialog function with autoreleasepool?


2022-09-30 16:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.