Question

I have a big problem in my application and I'm wondering how to resolve it. I've searched a lot in SO but I cannot find a valid solution. Here the scenario I'm working on.

I have a NON-ARC application and I'm using a bunch of ARC classes within it. These classes belong to GMGridView. These classes have been added to the project with -fobjc-arc directive.

This is the code I'm using (for the sake of simplicity I added only key parts).

Memory management section

- (void)dealloc
{
    [gmGridView setActionDelegate:nil];
    [gmGridView setDataSource:nil];
    [gmGridView release];    

    [super dealloc];
}

ViewDidLoad section

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSInteger topBottomSpacing = 20;
    NSInteger leftRifghtSpacing = 75;
    NSInteger itemSpacing = 5;

    UIView* mainView = [self view];

    GMGridView* gridView = [[[GMGridView alloc] initWithFrame:mainView.bounds] autorelease];    
    gridView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    gridView.backgroundColor = [UIColor clearColor];   
    gridView.style = GMGridViewStyleSwap;
    gridView.itemSpacing = itemSpacing;
    gridView.minEdgeInsets = UIEdgeInsetsMake(topBottomSpacing, leftRifghtSpacing, topBottomSpacing, leftRifghtSpacing);
    gridView.centerGrid = NO;
    gridView.actionDelegate = self;
    gridView.dataSource = self;    
    [mainView addSubview:gridView];

    [self setGmGridView:gridView]; // retain policy
}

DataSource section

- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
{
    CGSize size = [self GMGridView:gridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];

    GMGridViewCell *cell = [gridView dequeueReusableCell];    
    if (!cell) {

        cell = [[[GMGridViewCell alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)] autorelease];

        InternalView *view = [[[InternalView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)] autorelease];
        cell.contentView = view;
    }

    return cell;
}

When I'm using Zombie Objects, the application works well. No errors. But when I disable Zombie Objects the application crashes EXC_BAD_ACCESS in main method. This is quite strange for me since if Zombies are enabled, I would see a detail of that error that happens in main.

The things I'm not pretty sure are the autorelease calls in the code, but I think that If I don't put the objects in an autorelease pool they will leak.

GMGridView* gridView = [[[GMGridView alloc] initWithFrame:mainView.bounds] autorelease];

cell = [[[GMGridViewCell alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)] autorelease];

Investigating a bit I found that if I comment [gmGridView release] in dealloc method, the app stops to crash. So what does this mean? If I don't call release, will the gmGridView leak?

Do you have any suggestions? Thank you in advance.

EDIT

I added some code in - (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index method. I forgot to add it the first time.

The dealloc method of InternalView (of type UIView) seems the root of the problem. Here the code.

- (void)dealloc
{    
    [addButton release]; // it's added to self addSubview, it has also a retain policy
    [imageView release]; // it's added to detView addSubview, it has also a retain policy
    [detView release]; // it's added to self addSubview, it has also a retain policy

    [super dealloc];
}

Commenting [detView release], the crash goes away.

Was it helpful?

Solution

Flex_Addicted,

Judging from your code, we are looking at MRR code (i.e. non-ARC). If it was ARC, you could not have the [super dealloc], the -release nor -autorelease.

Is that what you intend? If so, then you have an early deallocation. I recommend that you convert this class to ARC. ARC, along with the static analyzer, will find early deallocation problems and handle them.

Andrew

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top