10

converting NSDate to NSString creates a memory leak can anyone help.

Here is my code:-

NSDate *today = [[NSDate alloc]init];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
dateString = nil;
dateString =[[NSString alloc]initWithString:[df stringFromDate:today]];
[df setDateFormat:@"EEEE       MMM dd yyyy"];
[dateButton setTitle:[df stringFromDate:today] forState:UIControlStateNormal];
[df release];
[today release];
Kyle Howells
  • 3,008
  • 1
  • 25
  • 35
leena
  • 689
  • 2
  • 7
  • 15
  • 2
    Please use the code formatting when putting code in your question – Nick Bull Jul 01 '11 at 10:42
  • If you switch over to ARC (automatic reference counting) your leak will go away. Specifically, the compiler will handle the releases for you. Not sure how easy it is to switch over, though - I haven't tried it yet. – Tyler Jul 01 '11 at 10:44
  • I'd **really** recommend staying away from ARC. I tried to but switched back quite quickly. You still need to tell it what is import (needed to keep in memory) but now you have to do it with '__strong's rather then 'retains'. Also it's quite limiting. For example if you use OpenAL you type cast an NSURL to a CFURLRef. You're not allowed to do that with ARC. – Kyle Howells Jul 01 '11 at 10:57
  • For EVERY alloc you NEED a RELEASE. – elp Jul 01 '11 at 11:05
  • @paska ... or, if it's only used inside that method, an AUTORELEASE. – Kyle Howells Jul 01 '11 at 11:11

7 Answers7

8

As you aren't releasing anything the code creates a memory leak.

NSDate *today = [NSDate date]; //Autorelease
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; //Autorelease

[df setDateFormat:@"yyyy-MM-dd"]; // 2017-09-28
dateString = [[df stringFromDate:today] retain];

[df setDateFormat:@"EEEE       MMM dd yyyy"];  // Thursday Sep 28 2017
[dateButton setTitle:[df stringFromDate:today] forState:UIControlStateNormal];

For more details, you can refer to Apple's documentation.

Kyle Howells
  • 3,008
  • 1
  • 25
  • 35
  • Thinking the same things...don't make it hard on yourself to keep track of your objects that need to be released if you will be getting rid of them in scope anyways. – bdparrish Jul 01 '11 at 10:44
  • Yeah, if your going to release an object within the same method then use autorelease so you don't have to remember to (and so avoid possible memory leaks) – Kyle Howells Jul 01 '11 at 10:48
  • As long as you are simplifying the code, you might as well get rid of the line that says `dateString = nil`. It does nothing given the following line. – JeremyP Jul 01 '11 at 10:50
  • True. I could also remove the [string retain]; but I'm taking it (because of the = nil line) that it's declared in the .h file and so is needed throughout the rest of the class. – Kyle Howells Jul 01 '11 at 10:54
  • thanks for the code but still i am getting leak at dateString = [[df stringFromDate:today]retain]; and variable dateString i am using at several places. – leena Jul 01 '11 at 12:40
  • @user806647 is dateString needed elsewhere in that file? And is dateString declared in the .h file? If so then after it is used for the last time put this in. [dateString release], dateString = nil; – Kyle Howells Jul 01 '11 at 12:42
  • @user806647 where else is dateString used in the app? Put this in the -(void)dealloc method & before you change it value to something else. [dateString release], dateString = nil; – Kyle Howells Jul 01 '11 at 14:20
6

Use

NSDate *today = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
dateString = [df stringFromDate:today];
[df release]
Nithin
  • 6,435
  • 5
  • 43
  • 56
6

Using your code...

NSDate *today = [[NSDate alloc]init]; 
NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"yyyy-MM-dd"]; 
dateString = nil; 
dateString = [[NSString alloc]initWithString:[df stringFromDate:today]];

...you need to release a lot of obj because nothing is in autorelease.

[today release];  -> alloc
[df release]      -> alloc
[dateString release]; -> alloc

Or change to:

NSDate *today = [NSDate date];
NSDateFormatter *df = [NSDateFormatter initWithDateFormat:@"yyyy-MM-dd"];
dateString = [df stringFromDate:today];

with no one release/alloc!

elp
  • 8,021
  • 7
  • 61
  • 120
3

The leak is in the DateFormatter that is not being released. This should fix the leak :

[df release];
werner
  • 859
  • 4
  • 8
3

Also, try using ...

[NSDate date]

instead of ...

NSDate* today = [[NSDate alloc] init];

that is a lot of alloc/initing that you are doing there as well... you don't need to alloc/init the NSString either.

bdparrish
  • 3,216
  • 3
  • 37
  • 58
3

You Shoud noy alloc or init NSDate object

Try this Code

NSDate *today = [NSDate date];
NSDateFormatter *dt = [[NSDateFormatter alloc]init];
[dt setDateFormat:@"yyyy-mm-dd"];
NSString *str =[NSString stringWithFormat:@"%@",[dt stringFromDate:today]];
NSLog(@"%@",str);
[dt release];

Happy Coding

Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
1

Yet another response:

NSDateFormatter  *df = [NSDateFormatter initWithDateFormat:@"yyyy-MM-dd"];
NSString *dateString = [df stringFromDate:[NSDate date]];

using ARC, all autoreleased.