Added CAGradientLayer, getting this in my UIView dealloc: : message sent to deallocated instance

Here there,

I have a custom UIView. This view acts as a activity indicator but as label above the UIActivityIndicatorView. In the init, I add a CAGradientLayer. I allocate and initialize it and insert it at index 0 as a sublayer of the UIView layer property. In my dealloc method was called, I received a message in the console:

- [CALayer release]: message sent to deallocated instance.

My code:

@interface LabelActivityIndicatorView ()
    UILabel *_label;
    UIActivityIndicatorView *_activityIndicatorView;
    CAGradientLayer *_gradientLayer;

@implementation LabelActivityIndicatorView

- (void) dealloc
    [_label release];
    [_activityIndicatorView release];

    //even tried to remove the layer
    [_gradientLayer removeFromSuperLayer];
    [_gradientLayer release];

    [super dealloc];

// init
- (id) initWithFrame:(CGRect)frame
    if ( (self = [super initWithFrame:frame]) )
        // init the label

        // init the gradient layer
        _gradientLayer = [[CAGradientLayer alloc] init];
        [_gradientLayer setBounds:[self bounds]];
        [_gradientLayer setPosition:CGPointMake(frame.size.width/2, frame.size.height/2)];

        [[self layer] insertSublayer:_gradientLayer atIndex:0];

        [[self layer] setNeedsDisplay];
    return self;


Anyone have any ideas. Since I'm allocating and initializing the gradient layer I'm responsible for releasing it. I should be able to alloc and init and assign to some ivar. Perhaps I should create a property with retain on it.



You probably should NOT alloc/init your layer. Use the convenience method layer:

    _gradientLayer = [[CAGradientLayer layer];

Then let the layer you add it to take ownership of it. When you release the view that superlayer belongs to, your layer will get released.

I've never kept an owning reference to layers that I create. I let the parent layer own it, and it works beautifully.

Need Your Help

How to add activity indicator to navbar?

objective-c cocoa-touch uinavigationbar uiactivityindicatorview

Hey, I want to display an activity indicator on the right corner of my navbar when my user changes selection in picker wheel. I have no idea how to add activity indicator there I am only able to add

Ruthlessly compressing large images for the web

image optimization photoshop

I have a very large background image (about 940x940 pixels) and I'm wondering if anyone has tips for compressing a file this large further than Photoshop can handle? The best compression without s...

ASP.NET: Problem with event handlers for dynamically created controls

.net events

I've got this problem with dynamically created TextBox.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.