Instruments (Leaks) and NSDateFormatter

后端 未结 3 1163
故里飘歌
故里飘歌 2021-01-13 23:57

When I run my iPhone app with Instruments Leaks and parse a bunch of NSDates using NSDateFormatter my memory goes up about 1mb and stays even though these NSDates should be

相关标签:
3条回答
  • 2021-01-14 00:13
    [df setDateFormat:@"EEE, d MMM yyyy H:m:s z"]; // Remove the `z`
    

    868 Kb will be permanently allocated on iPhone OS 2.2.1 or 3.1.2 (more to come) after a single invocation to "dateFromString" when the z option is used.

    A complete article with source code and log file can be read at http://thegothicparty.com/dev/article/nsdateformatter-memory-leak/

    0 讨论(0)
  • 2021-01-14 00:15

    There may be a problem with NSDateFormatter parsing date strings with time zones because when I changed the formatter pattern to remove the timezone portion the problem disappeared.

    I changed it from this:

    [df setDateFormat:@"EEE, d MMM yyyy H:m:s z"];
    

    To this:

    [df setDateFormat:@"EEE, d MMM yyyy H:m:s"];
    

    But now the problem is the dates don't get the right timezone so I'll have to have to determine the timezone myself.

    0 讨论(0)
  • 2021-01-14 00:16

    What do you mean by discarding the NSDateFormatters? Do you release them when you are done with them?

    df = [[NSDateFormatter alloc] init]; // allocates memory
    

    Your code allocates memory but you need to call

    [df release];
    

    when you are done with them. When you allocate (or copy) object its reference count is incremented by one. When you release object (you send release message to it) the reference count goes down by one. When the reference count reaches 0 the object is deallocated.

    If you don't send it release message it will stay in memory and you have memory leak.

    0 讨论(0)
提交回复
热议问题