Block leaking after Drag and Drop on iOs 13.4.1

Hello developers, I recently faced a problem with leaking std::shared_ptr object captured in block after Drag and Drop on iOs 13.4.1. To investigate this problem I made simple application based on MasterDetail template, adopt master table view and detail view for drag and drop, i used this functions of NSItemProvider :

- (void)registerDataRepresentationForTypeIdentifier:(NSString *)typeIdentifier visibility:(NSItemProviderRepresentationVisibility)visibility loadHandler:(NSProgress * _Nullable (^)(void (^completionHandler)(NSData * _Nullable data, NSError * _Nullable error)))loadHandler


and this one


- (NSProgress *)loadDataRepresentationForTypeIdentifier:(NSString *)typeIdentifier completionHandler:(void(^)(NSData * _Nullable data, NSError * _Nullable error))completionHandler


So after all I rut it on simulator on iOs 13.5, on real device with iOs 13.4.1 and iOs 12.4.1.

I saw that on real device with iOs 13.4.1 only the block uses in function loadDataRepresentationForTypeIdentifier was not destroyed with my std::shared_ptr object captured in this block. on simulator and on real device with iOs 12.4.1 this block was destroyed. I have used memory graph to see what objects hold this block and my shared object, finally I realised there is a cycle of internal blocks refered to each other.


Here is the link to the MasterDetail project with the Drag and Drop, run it on iPad only to check drag and drop, on iPhone you cann't drop on detail view because master view covers detail view.


https://www.dropbox.com/s/svsf1vf8ly50n04/MasterDetail.zip?dl=0


Can anyone help me to solve this problem or this is proble of iOs 13.4.1?

Answered by Vlad Stoyanov in 616306022
On ios 14 it works fine, Apple devs fixed this issue.

That's C++. I'm surprised it even compiled in the first place. You can try changing the file extension to ".mm" so that you have bona-fide Objective-C++ code.


I don't know if that will help. I have no idea if modern C++ std code is compatible with block captures. This is the kind of thing where you would have to have detailed knowledge of the standard, detailed knowledge of how it is implemented in this particular library, and detailed knowledge of how this particular clang compiler handles this particular object in a block context. In other words, it is high risk and you really shouldn't attempt it.


Also, I don't think code like "@(p->c_str());" is going to work. If you want to work in Objective-C, just use Objective-C. Don't add any C++ unless you have no other choice, such as if you depend on a one-of-a-kind C++ library. And these days, pretty much everyone is using Swift. I have no doubt that Apple will continue to support Objective-C, but avoiding Swift is a career-limiting move.

yes, it is C++/obj-c, but it's work fine. Our crossplatform project is written on C++ and we have to work with obj-c on mac os.

Code like "@(p->c_str());" works fine too if pointer is not null 🙂. So here is the screenshot of leaked block with captured std::shared_ptr.

As i mentioned above this problem does not occur on ios 12.4.1 and on simulator with ios 13.5


https://www.dropbox.com/s/djs9fzo4q1n9kn2/Screenshot%202020-05-28%20at%2000.07.32.png?dl=0


Drag beginning


https://www.dropbox.com/s/aifrlmjr7aqwa3p/Screenshot%202020-05-28%20at%2000.18.42.png?dl=0


Drop processing


https://www.dropbox.com/s/io56z4qq2eu64ql/Screenshot%202020-05-28%20at%2000.08.47.png?dl=0

Sorry, but that's a non-starter.

Accepted Answer
On ios 14 it works fine, Apple devs fixed this issue.
Block leaking after Drag and Drop on iOs 13.4.1
 
 
Q