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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:00:27+00:00 2026-05-31T21:00:27+00:00

I know this is fairly fundamental stuff. I have a class with a function

  • 0

I know this is fairly fundamental stuff.

I have a class with a function that returns the name of the month; I’m not sure how to release a value that I want to return to prevent leaks.

In the class this value is declared:

static NSDateFormatter *formatter = nil;
if (formatter == nil) {
    formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MMMM yyyy"];
}

Things happen, and then:

if([exampleDate isEqualToDate:
    [[self creationDate] laterDate:exampleDate]])
{ //earlierDate returns the earlier date
    return(@"Examples");
}else{
    return([formatter stringFromDate:[self creationDate]]);
}

I’ve tried using autorelease but I’m using it incorrectly because I get a crash when I try to release it. I’ve also tried assigning the return value to a string, but I have the same crashing problems. Sorry for asking a question that is so fundamental, but I’d appreciate knowing how to release this properly, while still returning the value – and understanding how it works.

  • 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-31T21:00:28+00:00Added an answer on May 31, 2026 at 9:00 pm

    there are no leaks in the return part. Since stringFromDate: is not copy, mutableCopy, retain, alloc or new it returns already an autoreleased object.

    Your NSDateFormatter will leak when the class is deallocated. If you don’t want it to leak you should create a @property for it and release it in dealloc.
    Local static objects will always leak.
    I would not use such objects outside of singletons. Each time you create one of your classes you will leak an NSDateFormatter. Using @property is better in almost every case.

    I’ve seen hacks like this but in my opinion they solve a problem you shouldn’t have in the first place:

    - (NSString *)someDateFromString:(NSString *)str {
        static NSDateFormatter *formatter = nil;
        if (formatter == nil) {
            formatter = [[NSDateFormatter alloc] init];
        }
        if (str == nil) {
            [formatter release];
            formatter = nil;
            return nil;
        }
        // do something
        return date;
    }
    
    - (void)dealloc {
        [self someDateFromString:nil]; // will release the static date formatter.
        [super dealloc];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fairly sure that this would not be possible, but just want to
I know this is fairly basic, but I'm still stuck. So I have a
I have a fairly large chunk of code that produces/returns an arraylist of search
I have this fairly simple function to compute the mean of elements of a
I know this is fairly subjective, but I'm diving into testing and learning about
I know this is a fairly contentious issue amongst programmers, but when developing I
I know this is largely an opinion, but I'm interested if you have one
I know this can be done and i have seen it done using some
I know this might be a long shot, but here it goes. I have
I know this is a fairly common error with URL encoding. The problem I

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.