Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6107693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:12:57+00:00 2026-05-23T14:12:57+00:00

I have a class that has a property: @property (nonatomic, assign) id customDatePicker Based

  • 0

I have a class that has a property:

@property (nonatomic, assign) id customDatePicker

Based upon user choice I will need a UIDatePicker or a UIPicker so I typecast the ivar and retain it and subsequently dealloc it. Is this the correct way to manage the property’s memory?

// init snippet
if (useUIDatePicker) {
  customDatePicker = (UIDatePicker *)[[[UIDatePicker alloc] initWithFrame:frame] retain];
} else {
  customDatePicker = (UIPickerView *)[[[UIPickerView alloc] initWithFrame:frame] retain];
}

- (void)dealloc {
  [customDatePicker release];
  [super dealloc];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:12:57+00:00Added an answer on May 23, 2026 at 2:12 pm

    No.

    When you declare the property as assign, you should not be retaining the object. The assign option is used for non-object variables and for situations where having a retained property would create a cycle, with both objects retaining each other. Declaring a property as assign means you will not be managing the memory of the object; you should neither call retain nor release on it.

    You are also over-retaining the picker object. Retaining an object creates a claim on the object; you don’t want it to disappear until you say you are done with it. You relinquish a claim, allowing an object to be deleted, by calling release. When you call alloc, that creates the same kind of claim as calling retain. So this line:

    [[[UIDatePicker alloc] initWithFrame:frame] retain];
    

    creates two claims, one for alloc and one for retain. Later, you only call release once, which means that you will always still have one claim on this object, and it will have turned into a memory leak.

    What you should do is:

    @property (nonatomic, retain) id customDatePicker
    
    
    if (useUIDatePicker) {
        customDatePicker = [[UIDatePicker alloc] initWithFrame:frame];
    } else {
        customDatePicker = [[UIPickerView alloc] initWithFrame:frame];
    }
    

    Now you have only one claim on the picker because you used alloc.

    (You don’t need to cast the assignment; when you are assign to a generic pointer you can use any kind of object.)

    Also take a look at the Apple Memory Management docs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that has a property that I need to stub. I
I have a class called Question that has a property called Type. Based on
I have a property that has (nonatomic, assign) as attributes. In a method, I
I have an entity class that has a property with an underlying db column
I have a Menu class that has a IQueryable property called WebPages. In the
I have come across a class that has an immutable property: MyObject[] allObjs The
I have a class (A web control) that has a property of type IEnumerable
I have an Obj-C 2.0 class that has an NSMutableArray property. If I use
I have a class, called DateField , that has a string Value property. If
Say I have a tableview class that lists 100 Foo objects. It has: @property

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.