UA-17470720-3

Jump to content


Photo
- - - - -

SCTableModel's boundObject's dealloc method not called

models sections cells

  • Please log in to reply
2 replies to this topic

#1 tommy@ageet

tommy@ageet

    Starting Out

  • STV 4.0 Pro
  • Pip
  • 2 posts
Reputation: 0
Neutral

Posted 12 May 2016 - 01:45 AM

Hi, 

 

I noticed that the SCTableModel's section and correlating cells seem to be keeping a strong reference to their bound object even after deallocation and consequently, the bound object's dealloc method doesn't get called.

 

Here is a gist of my implementation:

 

///SettingsContainer.h
/*The object to be bound to the SCTableModel*/
@interface SettingsContainer : NSObject

@property(nonatomic, copy) NSString* userId;
@property(nonatomic, copy) NSString* authId;
@property(nonatomic, copy) NSString* password;

@end

///SettingsContainer.m
@implementation SettingsContainer
+ (SCClassDefinition*)classDef {
    return [SCClassDefinition definitionWithClass:[self class]
                                    propertyNames:@[@"userId",
                                                    @"authId",
                                                    @"password"]
                                   propertyTitles:@[@"User ID",
                                                    @"Auth ID",
                                                    @"Password"]];
}

- (void)dealloc{
   //This method is never invoked
}
@end

///SettingsViewController.m
/*Manages the scTabelModel*/
@interface SettingsViewController()
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property(nonatomic, strong) SettingsContainer *settingsContainer;
@property(nonatomic, strong) SCTableViewModel *scTableModel;

@end

@implementation SettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.scTableModel = [SCTableViewModel modelWithTableView:self.tableView]; 
    
    SCObjectSection *accountSection = [SCObjectSection sectionWithHeaderTitle:@"Account"
                                                           boundObject: self.settingsContainer
                                                 boundObjectDefinition: [[self.settingsContainer class] classDef]];

    [self.scTableModel addSection:accountSection];
}

- (void)dealloc{
   //This method is invoked as expected
}
@end

 

As the comments point out, when SettingsViewController is dismissed it's dealloc method is entered as expected, however the SettingsContainer's dealloc method isn't.   Viewing Xcode's memory usage report the graph doesn't appear to rise because of this, however I am relying on this dealloc method to handle some other code cleanup.  

 

Some further investigation showed me that after "sectionWithHeaderTitle:boundObject:boundObjectDefinition" is called, self.settingsContainer's retainCount increases by 4.  I assume this is due the generated section table binding to it and the 3 generated cells ("User ID", "Auth ID", and "Password"), which makes sense.  With this in mind I tried implementing the following code:

 

@implementation SCTableViewModel (cleanup)

- (void)releaseBoundObjects {
    for (int i = 0; i < self.sectionCount; i++) {
        SCTableViewSection *section = [self sectionAtIndex:i];
        for (int j = 0; j < section.cellCount; j++) {
            [section cellAtIndex:j].boundObject = nil;
        }
        
        section.boundObject = nil;
    }
}

///SettingsViewController.m
@implementation SettingsViewController

- (void)dealloc {
    [self.scTableModel releaseBoundObjects];
}

@end

 

Now upon execution of "releaseBoundObjects", my SettingsContainer's dealloc method is successfully entered.

 

 

Is there a better way to handle this kind of situation?



#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 12 May 2016 - 07:39 AM

Hi Tommy,

 

STV needs a strong reference to its bound object(s), since in a lot of cases, our users simply assign objects to STV in 'viewDidLoad' where they do not retain anywhere else. Your 'dealloc' method should get called as soon as STV's own objects are released, which typically happens automatically as soon as the relevant view controller is dismissed.

 

In the case where you'd like to release your objects before STV's objects have been released, your solution seems to be perfectly fine.



#3 tommy@ageet

tommy@ageet

    Starting Out

  • STV 4.0 Pro
  • Pip
  • 2 posts
Reputation: 0
Neutral

Posted 12 May 2016 - 05:52 PM

Hi Tarek,

 

Thanks for your reply.  

 

>Your 'dealloc' method should get called as soon as STV's own objects are released, which typically happens automatically as soon as the relevant view controller is dismissed.

 

Yes, this is what I was expecting but 'dealloc' wasn't ever called.  It was only called after I manually released the section's and cells' bound objects in my viewController's 'dealloc' method.  I was just wondering if there was perhaps another more standard way of doing this but I'll just go with what I have now.  Thanks for your input!







Also tagged with one or more of these keywords: models, sections, cells

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users