Have an interesting issue where there is a class that is referenced in an XIB layout (subclass of UIScrollView) and is not being de-allocated according to Instruments / Allocations and does not break in it's dealloc routine. Let's call it Sclass1.
There is a using class (let's call it Uclass) that has the XIB file and the outlet.
@property (nonatomic, weak) IBOutlet Sclass1* sclass1;
This is hooked properly to the XIB file layout.
Sclass1 is property allocated when the XIB for Uclass is loaded. Uclass does get deallocated and then recreated from time to time and thus we have another instance of Sclass1, but Sclass1 never goes away and can't find another reference to it.
Drill down in Instruments shows the one Malloc and that is it.
fyi, the class gets started with
[UIClassSwapper initWithCoder:]
Give the class a name, which must be the same name as the XIB file (Pokemon). Select UIView as the subclass type, then hit "Next". On the next window, select your target and hit "Create". Connect Pokemon.xib to Pokemon.swift via "File’s Owner" attribute
Create the class that is going to manage the XIB file. Xcode Menu Bar > File > New > File. Select iOS / Source / Cocoa Touch Class. Hit "Next". Give the class a name, which must be the same name as the XIB file (Pokemon). Select UIView as the subclass type, then hit "Next". On the next window, select your target and hit "Create".
In the New File dialog box, select Xamarin.Mac > Cocoa Window with Controller: Enter PreferencesWindow for the Name and click the New button. Double-click the PreferencesWindow.xib file to open it for editing in Interface Builder: Save your changes and return to Visual Studio for Mac to sync with Xcode.
When you switch back to Visual Studio for Mac from Xcode, any changes that you have made in Xcode will automatically be synchronized with your Xamarin.Mac project.
If an object doesn't get deallocated under ARC, it means a strong reference to it exists. Since your property is weak
the object must be owned strongly by something other than the Uclass object (Otherwise it would get deallocated immediately after the XIB has loaded). In the code you've provided it isn't clear what the actual strong owner of this object is, but I assume it could be one (or more) of the following:
UIView
subclass, it may be (strongly) referenced by its superview
if added as one of subviews
. This happens automatically when a XIB file is loaded. If the superview
doesn't get deallocated neither will the SClass
object. You can remove this ownership by calling removeFromSuperview
SClass1
object (i.e. one of the strongly-owned instance variables have a strong reference back to its owner - the SClass1
). Beware that any block using self
directly also keeps a strong reference. Having a strong reference to the block then often leads to a retain-cycle. Save self
to a __weak
var and pass that to the block instead unless you have a good reason not to.__weak
variable.Try finding and removing these strong ownerships. Only after all of them are removed the object can be deallocated.
Since your property is weak and it's still not deallocated, look for strong references to Sclass or it's owner, Uclass. Maybe you are using Uclass(or Sclass) in block directly, without __weak typeof(self) weakSelf dancing and this block creates retain cycle. Also watch for parent-child relations and delegates. Maybe there is delegate which is strong instead of weak or two controllers hold strong references to eachother.
Also, if you want to have more detailed answers, please post more relevant code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With