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

Using results of query multiple times

php mysql result

How do I use the result of a query multiple times

MDM client - server communication

ios apple-push-notifications mdm

After reading and searching, apple MDM client - MDM server interaction seems to be:

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.