Seeing memory leaks from PHPickerViewController
I've recently integrated the PHPickerViewController per Apple's recommendation for accessing photos within your app. However, testing for memory leaks, I've noticed the picker is producing many memory leaks. I'm running the PHPickerDemo and see the following leaks while profiling:
Image from Instruments Memory Leaks
Any thoughts on how to solve this or is this something we have to wait for Apple to fix?
See also questions close to this topic
- Does apple allow Sub4Sub apps in the app store?
-
How do I format an empty (NDEFFormatable) NFC Tag to NDEF format?
I'm trying to write NDEF message to an empty DESfire v2 card. I tried this code:
guard case .miFare(let miFare) = tag else {continue} session.connect(to: tag) { [weak self] error in if let _ = error { return } let data = "test data".data(using: .utf8)! let payload = NFCNDEFPayload.init(format: .unknown, type: "T".data(using: .utf8)!, identifier: Data.init(count: 0), payload: data, chunkSize: 0) let message = NFCNDEFMessage(records: [payload]) let apdu = NFCISO7816APDU(instructionClass: 0, instructionCode: 0x1B, p1Parameter: 0, p2Parameter: 0, data: Data(), expectedResponseLength: 16) miFare.sendMiFareISO7816Command(apdu) { (apduData, sw1, sw2, error) in print("\(error)") miFare.writeNDEF(message) {[weak self] error in if let localError = error { print(localError) return } else { print("ok") } } } }
but I've got an error because it's not NDEF formatted:
Error Domain=NFCError Code=401 "Stack Error" UserInfo={NSLocalizedDescription=Stack Error, NSUnderlyingError=0x283fc2040 {Error Domain=nfcd Code=37 "Tag Not NDEF formatted" UserInfo={NSLocalizedDescription=Tag Not NDEF formatted}}}
Is there any way to format this card to NDEF format using Apple's CoreNFC?
-
Save Data Offline Then Automatically Upload It To Firestore Upon Connection - Swift Xcode
I'm creating an app that allows the user to create a project, take photos and create labels in that project (using structs), then create a PDF later on. I've got to figure out how to save this data without connection, then backup it up to the server later. I want the app to be able to work both online and offline - whether the user has cell service or not. I've seen many apps where when you load them up, if you have no signal, you can select "Run Offline" or "Continue Offline."
How should I save the data locally until the user comes back into cellular signal and the data can be backed up to Firebase Firestore and Storage? I was considering using UserDefaults to save the data in a cache essentially, then whenever the phone is back within signal, the app automatically runs through this cache and backs everything up to the server. Is this the correct way to accomplish what I'm needing done? Is there a better way?
In the end, the data MUST be able to be saved whether connected to the internet or not - online or offline. I just need to figure out the best way to save the data until it can be uploaded to the server when back online. I want the data the user is creating to be safe in case something happened to the phone or to the app (maybe their phone shut off or there was a random app crash). By the way, the app is project based... so there's multiple files (projects) in other words. They may need to set up multiple files (projects) while offline and access one of them later on while still offline. It needs to put all of these offline projects in a container to be automatically backed up to the server upon returning to signal. These files (projects), with all the data in them, MUST be safe at all times until backed up - even if the user closes the app.
Please leave a detailed answer. I'm a beginner, as you can tell, and need specifics. If you reference a certain way of doing something, kindly leave a link to a tutorial or significant documentation, or explain it thoroughly.
Thanks!
Christian
-
Do I need to clean up unmanaged code after obtaining an IStorage object from OLE32?
I obtain an
IStorage
object from OLE32'sStgCreateStorageEx
function. Here is its declaration:Imports Microsoft.VisualStudio.OLE.Interop <System.Runtime.InteropServices.DllImport("ole32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> Private Shared Function StgCreateStorageEx( <MarshalAs(UnmanagedType.LPWStr)> ByVal pwcsName As String, ByVal grfMode As UInt32, ByVal stgFmt As UInt32, ByVal grfAttrs As UInt32, ByRef pStgOptions As SStgOptions, ByVal pSecurityDescriptor As IntPtr, <[In]()> ByRef riid As Guid, <[Out]()> ByRef ppObjectOpen As IStorage) As Int32 End Function
The caller sets the flags and the structure member, then obtains a new
IStorage
object in anoStorage As IStorage
object. So far all is well.The documentation kindly reminds us:
An application must release its IStorage pointers when it is done with the storage object to deallocate memory used.
Since the object was created by COM and consumes memory, and since we all don't like memory leaks, the standard thing to do would be to properly implement
IDisposable
and dispose of the caller'soStorage
object, via theMarshal.ReleaseComObject(oStorage)
method. But...I happened to come across this document Marshal.ReleaseComObject Considered Dangerous, by the Visual Studio developers. Only that...
I am not sure if I understand the causes thoroughly. However, it seems to be a clear advice to stay away from
ReleaseComObject
altogether. But...The document stems from 2010. Is this still the proper advice, more than a decade later? And...
How would I otherwise prevent memory leaks?
-
Flutter: memory leak with isolate (compute)
I see memory leak every time I call compute() in my Flutter app, which is basically just a convenient way to spawn an isolate as far as I understand. My app memory footprint grows and never goes down after GC. I've reduced my code to just calling an empty function - and the issue is still reproducible:
// run my _encodeJpeg in isolate await compute(_encodeJpeg, null); Future<void> _encodeJpeg(_JpegEncodeParams params) async { // I've made this function empty just to see if the memory leak is still reproducible - yes it is! }
Note that if I don't call compute() - there is no memory leak in my app. So it is definitely compute() that cause the issue.
Then I've created a new starter Flutter app (default one with button and counter) - and call compute() from that app the same way and it works fine! No memory leaks. Weird, right?!
There is something in my app that cause memory leak on compute(), while the app itself (if I don't call compute) doesn't have any memory leaks. I've created my app using some of the previous versions of Flutter, but now I compile it using the latest Flutter (see below).
Could there be something in my app that needs to be updated manually to make compute() work as expected?
UPDATE: I don't see this issue using library such as Computer (https://pub.dev/packages/computer):
final computer = Computer(); await computer.turnOn(workersCount: 1); await computer.compute<void, _JpegEncodeParams>(_encodeTempJpeg, param: ...); computer.turnOff();
flutter doctor:
[√] Flutter (Channel stable, 1.22.5, on Microsoft Windows [Version 10.0.18363.1256], locale en-US) • Flutter version 1.22.5 at C:\sdk\flutter • Framework revision 7891006 (6 weeks ago), 2020-12-10 11:54:40 -0800 • Engine revision ae90085a84 • Dart version 2.10.4
-
WinForms ToolStripComboBox Leaks Memory (Font & Region)
Create one form (Set as a startup form) having ToolStrip with ToolStripButton and ToolStripComboBox. On the button, click open an instance of this form and close the new instance. Observe the GDI Objects before opening the form and after Opening & Closing the form. Looks like a bug but how to deal with it?
Task Manager & GDIView can be used to observe the results.
Edit When we open the form Font count goes up by 7 and on close, the count goes down by 6. 1 is left and never collected. Event If I invoke GC.Collect() with forced parameters or wait for serval minutes that one Font object is never collected. If my application is running for hours and different editors (Forms) having toolbar are opened then closed it keeps growing.
-
Push view controller over PHPickerViewController
Is that possible to push vc over the PHPickerViewController?
I'm trying to do that like this with no luck:
var configuration = PHPickerConfiguration() configuration.filter = .any(of: [.images, .livePhotos]) photoPickerController = PHPickerViewController(configuration: configuration) photoPickerController.delegate = self present(self.photoPickerController, animated: true) func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { //Push segue performSegue(withIdentifier: "showAddPost", sender: self) }
-
PHPickerViewController add video editing screen aka (UIImagePickerController.allowsEditing = true)
I would like to edit/compress video after selection like it was in
UIImagePickerController
withallowsEditing = true
.Still the new
PHPickerViewController
doesn't have this property andApple
says that there is no such property anymore here. But there are apps in App Store that pushing "allowsEditing controller" after selecting asset fromPHPickerViewController
Example of app with PHPickerViewController integrated with allowEditing
Here is my PHPickerViewController implementation:
func openImagePicker() { if #available(iOS 14, *) { var configuration = PHPickerConfiguration() configuration.preferredAssetRepresentationMode = .automatic let picker = PHPickerViewController(configuration: configuration) // Set the delegate picker.delegate = self // Present the picker present(picker, animated: true) } else { // Fallback on earlier versions imagePicker?.photoGalleryAsscessRequest() } } extension EditController: PHPickerViewControllerDelegate { @available(iOS 14, *) func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { self.dismiss(animated: true) } }
-
Adding a selection confirmation to PHPickerViewController
I'm implementing the new
PHPickerViewController
as follows:func importMedia() { var config = PHPickerConfiguration() config.selectionLimit = 1 //config.filter = PHPickerFilter.images let pickerViewController = PHPickerViewController(configuration: config) pickerViewController.delegate = self self.present(pickerViewController, animated: true, completion: nil) }
And:
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { picker.dismiss(animated: true) { // Handle resultant media } }
Is there any way to get some form of confirmation dialog to the user when they select a photo/video? As it stands, tapping media just instantly dismisses the
PHPickerViewController
and handles the selected media accordingly, whereas with my previousUIImagePickerController
implementation, after tapping a photo/video it would then show a preview dialog with a "cancel" and "choose" button at the bottom. I assume this may have something to do with usingpicker.allowsEditing = true
for that implementation - does something similar exist withPHPickerViewController
?I guess I could add a
UIAlertController
in the delegate method, before handing the selected media, but it would probably be quite clunky...