Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS 6.1 with ARC Class from XIB does not get Deallocated, UIClassSwapper

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:]
like image 803
ort11 Avatar asked Jul 01 '13 15:07

ort11


People also ask

How do I create a UIView class in Xib?

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

How do I create an xib file in Xcode?

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".

How to open Xamarin xib file in Visual Studio for Mac?

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.

What happens when I switch from Xcode to Visual Studio for Mac?

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.


2 Answers

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:

  1. Since the object's class is a 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
  2. A strong ownership cycle (retain-cycle) exists somewhere among ivars of the 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.
  3. A manually created strong reference exists by e.g. adding the object to a container or saving the pointer to a non-__weak variable.

Try finding and removing these strong ownerships. Only after all of them are removed the object can be deallocated.

like image 152
burax Avatar answered Nov 03 '22 03:11

burax


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.

like image 24
Timur Kuchkarov Avatar answered Nov 03 '22 03:11

Timur Kuchkarov