David Karlsson
David Karlsson

Reputation: 9716

Everytime a WEPopover is presented in iOS app the memory allocations grow

I am having some issues with my app, I am currently using the WEPopover library (ARCified) to create some custom popovers. However, everytime i present a popover and it's corresponding view (consisting of a view and a tableview populated with an array) the allocations in instruments grow, even if im only re-adding the same popover again and again it grows. What am I missing, or is this normal behaviour, see code below. Should I instead make the table view weak or something?

ThemesPopOverViewController.h

#import <UIKit/UIKit.h>
#import "AppDelegate.h"


@interface ThemesPopOverViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{
    NSArray *themes;
}
@property (nonatomic, retain) UITableView *tableView;
@end

ThemesPopOverViewController.m

#import "ThemesPopOverViewController.h"
@interface ThemesPopOverViewController ()
@end

@implementation ThemesPopOverViewController
@synthesize tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
      themes = [[NSArray alloc] initWithObjects:kRegexHighlightViewThemeArray];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 0, self.view.frame.size.width - 40, 350)];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.view addSubview:self.tableView];
}

-(void) viewDidAppear:(BOOL)animated{
    int item = [themes indexOfObject:theDelegate.codeView.currentTheme];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:0];
    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;    //count of section
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //NSLog(@"%d",themes.count);
    return [themes  count];    //count number of row from counting array hear cataGorry is An Array
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:MyIdentifier];
        UIView *bgColorView = [[UIView alloc] init];
        [bgColorView setBackgroundColor:[UIColor colorWithRed:25/255.0f green:185/255.0f blue:152/255.0f alpha:1.0f]];
        [cell setSelectedBackgroundView:bgColorView];
    }
    [cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:16]];
    cell.textLabel.text = [themes objectAtIndex: indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%d", indexPath.row);
    [theDelegate.codeView setHighlightThemeFromString:[themes objectAtIndex:indexPath.row]];
    [theDelegate.codeView setNeedsDisplay];
    themes = nil; 
    [theDelegate removePop];  
}
@end

I add the popover from:

-(void) settingAct:(UIButton *)sender{
    if (!popover) {
        ThemesPopOverViewController *newView = [[ThemesPopOverViewController alloc] initWithNibName:@"SettingPopOverViewController" bundle:[NSBundle mainBundle]];
        self.popover = [[WEPopoverController alloc] initWithContentViewController:newView];
        [self.popover setContainerViewProperties:[self improvedContainerViewProperties]];
        [self.popover setPopoverContentSize:CGSizeMake(128, 360)];
        [self.popover presentPopoverFromRect:CGRectMake(sender.center.x+12, sender.center.y, 0, 20)
                                      inView:self.window.rootViewController.view
                    permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown|
                                              UIPopoverArrowDirectionLeft|UIPopoverArrowDirectionRight)
                                    animated:YES];

        newView = nil;
    }else{
        [self removePop];
    }
}

And the remove method in "theDelegate" for dismissing popovers:

-(void) removePop{

    [self.popover dismissPopoverAnimated:YES];
    self.popover = nil;
}

Baseline = no popovers, Heapshot1 = added popover, Heapshot2 = popover dissmissed, Heapshot3 = added same popover again, Heapshot4 = popover dissmissed.

Snapshot    Timestamp   Heap Growth # Persistent
- Baseline -    00:07.464.624   1.59 MB 25041
Heapshot 1  00:11.759.060   50.34 KB    787
Heapshot 2  00:16.278.744   0 Bytes 0
Heapshot 3  00:24.312.874   30.39 KB    608
Heapshot 4  00:28.361.293   288 Bytes   7

Upvotes: 0

Views: 207

Answers (1)

Prabhu Natarajan
Prabhu Natarajan

Reputation: 869

In Your SettingsAct button~action~method do the code like this

 if (self.popoverController)
{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
else
{
    ThemesPopOverViewController *newView = [[ThemesPopOverViewController alloc] initWithNibName:@"SettingPopOverViewController" bundle:[NSBundle mainBundle]];
    self.popover = [[WEPopoverController alloc] initWithContentViewController:newView];
    [self.popover setContainerViewProperties:[self improvedContainerViewProperties]];
    [self.popover setPopoverContentSize:CGSizeMake(128, 360)];
    [self.popover presentPopoverFromRect:CGRectMake(sender.center.x+12, sender.center.y, 0, 20)
                                  inView:self.window.rootViewController.view
                permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown|
                                          UIPopoverArrowDirectionLeft|UIPopoverArrowDirectionRight)
                                animated:YES];
}

it works fine for me :)

Upvotes: 0

Related Questions