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 793853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:13:16+00:00 2026-05-14T22:13:16+00:00

memory management still gives me grief. This time it is an NSDate iVar that

  • 0

memory management still gives me grief. This time it is an NSDate iVar that I init using

NSDate *myNSDate = [[NSDate date] firstDayOfMonth];

with a method call to

- (NSDate *)firstDayOfMonth {
    NSDateComponents *tmpDateComponents = [[NSCalendar currentCalendar]
                                            components:NSYearCalendarUnit | NSMonthCalendarUnit | NSEraCalendarUnit | NSWeekCalendarUnit | NSWeekdayOrdinalCalendarUnit
                                            fromDate:self];
    [tmpDateComponents setDay:1];
    [tmpDateComponents setHour:0];
    [tmpDateComponents setMinute:0];
    [tmpDateComponents setSecond:0];
    return [[NSCalendar currentCalendar] dateFromComponents:tmpDateComponents];
}

At the end of the init call the retain count is at 1 (Note the iVar is not defined as a property).

When I step into the viewWillAppear method the myNSDate has vanished. I tried to do an explicit retain on it, but that only lasts until I update the iVar using the above method again.

I though – ok – I add the retain to the return of the function, but that makes the leak analyser throw up an error.

What am I doing wrong?

  • 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-14T22:13:17+00:00Added an answer on May 14, 2026 at 10:13 pm

    Your method firstDayOfMonth is correct as it is given in your question. However, the return value of that method is an autoreleased date, which means that if you don’t retain the return value somewhere else, it will disappear.

    So you need to do something like this (assuming your ivar is named firstDayOfMonth:

    - (id) init {
      if (self = [super init...]) {
        ...
        [self setFirstDayOfMonth:[[NSDate date] firstDayOfMonth]];
      }
      return self;
    }
    
    - (void) setFirstDayOfMonth:(NSDate *)newFirstDay {
      [firstDayOfMonth release];
      firstDayOfMonth = [newFirstDay retain];
    }
    
    - (void) dealloc {
      [firstDayOfMonth release];
      ...
      [super dealloc];
    }
    

    In this, you’re explicitly retaining the return value (but also making sure to release the old value so you’re not leaking memory). Now your date object will live until you set a new date, or the object is deallocated and the date is destroyed in the dealloc method.

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

Sidebar

Related Questions

I am still new to this Memory Management stuff (Garbage Collector took care of
I'm still learning about Objective-C memory management. I'm trying to implement several simple classes
I try to understand the memory management in ObjectiveC and still some things are
I'm still new to Cocoa and don't know much about memory management. I read
I understand Objective-C memory management but I'm using Core Graphics functionality such as CGRect,
Still new to the memory management in iPhone apps, I have a newbee question.
I'm still trying to find my way through memory management for the iPhone SDK,
I'm still shaky on the subtler aspects of memory management, and I have a
I'm still trying to wrap my head around iphone memory management. I have checked
I'm still wrapping my head around some of the nuances of memory management in

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.