1

Context: I have already done the following
1. Watched the relevant videos for UICollectionView from WWDC 2012. Unfortunately they do not discuss much about the decoration views.
2. Implemented a collection view with decoration view. I subclassed the UICollectionViewFlowLayout to include the decoration view.
3. I have read the UICollectionView programming guide from Apple.
4. I understand that the Decoration views are controlled solely by the Layout object.

Problem:
1. When I analyze the code with Instruments I find that my decoration views are causing a memory leak.
2. On further analysis of the code I found that the decoration view is not being reused as I would expect. Every time the decoration view is needed a new one is created.
3. When the collection view deallocs only the last created decoration view gets dellocated and all other decorations views leak.
4. I do not understand how the decoration view will be reused when there is no deque method for it.

Questions:
1. Do we have to manually manage the removal and addition of decoration views from the collection view?

I am confused as to how is the decoration view works in general. Any pointers for this would help. The code for my layout object is as below. In the code I am simply putting a 10 points wide bar at the top of my collection view. Everytime the bar is scrolled off the screen and then brought back into the screen, a new decoration view is allocated. When the collection view is deallocated only the last allocated decoration view gets deallocated. Rest all leak.

#import "CollectionViewFlowLayoutSubclass.h"
#import "CollectionViewDecorationView.h"

#define DECORATION_VIEW_KIND    @"DecorationViewShelf"

@implementation CollectionViewFlowLayoutSubclass

- (void)awakeFromNib
{
    [super awakeFromNib];    
    [self registerClass:[CollectionViewDecorationView class] forDecorationViewOfKind:DECORATION_VIEW_KIND];
}

- (void)prepareLayout
{    
    [super prepareLayout];
}

- (CGSize)collectionViewContentSize
{
   return [super collectionViewContentSize];    
}

- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray* larrayAttributes = [super layoutAttributesForElementsInRect:rect];

    NSMutableArray* larrayMutableAttributes = [larrayAttributes mutableCopy];    

    CGRect lrectFrame = CGRectMake(0, 0, 320, 10);

    if (CGRectIntersectsRect(rect, lrectFrame))
    {
        UICollectionViewLayoutAttributes* lobjLayoutAttributes =

        [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:DECORATION_VIEW_KIND

                                                                    withIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];

        lobjLayoutAttributes.frame = CGRectMake(0, 0, 320, 10);

        [larrayMutableAttributes addObject:lobjLayoutAttributes];
    }
    else
    {
        NSLog(@"Rect %@ does not intersect %@", NSStringFromCGRect(rect), NSStringFromCGRect(lrectFrame));
    }

    return [NSArray arrayWithArray:larrayMutableAttributes];

}

@end
praveen
  • 815
  • 2
  • 9
  • 12
  • Not sure, but perhaps it has something to do with using the same index path for all decoration views? – omz Jun 01 '13 at 14:20
  • No that does not seem to be the case. I modified the code so that that particular layout attribute is added once and only once after the allocation of the layout object. Even after doing that the decoration view keeps getting allocated multiple times. And ultimately all instances except the last one leak. – praveen Jun 03 '13 at 09:40
  • Same problem. Ever find a fix? – gdavis Feb 06 '14 at 01:54
  • No we could not find any solution and assumed that it was a bug on Apple's side. – praveen Feb 07 '14 at 06:46

0 Answers0