简体   繁体   English

Struct中的NSDate可能发生内存泄漏

[英]Possible Memory leak with NSDate in Struct

//h file

struct runSTRUCT{
   NSDate *RunDateTime;
} ;

 //m file

struct runSTRUCT run;

- (void)viewDidLoad {
   [super viewDidLoad];
   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   dateFormatter.timeStyle = NSDateFormatterNoStyle;
   [dateFormatter setDateFormat:@"MM/dd/yyyy   HH:mm"];
   run.RunDateTime = [dateFormatter dateFromString:@"12/02/2012   12:22"];
   NSString *dateTimeStr = [dateFormatter stringFromDate:run.RunDateTime];
   [dateFormatter release]; 
}

This all works fine. 一切正常。 Then when I click a button and make a string from the date, it gives me EXC_BAD_ACCESS. 然后,当我单击一个按钮并从日期创建一个字符串时,它给了我EXC_BAD_ACCESS。

-(IBAction)respondButtonPressed:(id)sender{
   NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
   dateFormatter2.timeStyle = NSDateFormatterNoStyle;
   [dateFormatter2 setDateFormat:@"MM/dd/yyyy   HH:mm"];
   NSString *dateTimeStr = [dateFormatter2 stringFromDate:run.RunDateTime]; 
   [dateFormatter2 release]; 
}

When I look in the console and print description, it generally shows 1 of two things: 当我查看控制台并打印描述时,它通常显示两件事中的一件:

  1. The program being debugged was signaled while in a function called from GDB. 正在调试的程序在从GDB调用的函数中发出信号。 GDB has restored the context to what it was before the call. GDB已将上下文恢复到调用之前的状态。 To change this behavior use "set unwindonsignal off" Evaluation of the expression containing the function (CFShow) will be abandoned. 要更改此行为,请使用“set unwindonsignal off”将放弃包含该函数(CFShow)的表达式的评估。 The program being debugged was signaled while in a function called from GDB. 正在调试的程序在从GDB调用的函数中发出信号。 GDB has restored the context to what it was before the call. GDB已将上下文恢复到调用之前的状态。 To change this behavior use "set unwindonsignal off" Evaluation of the expression containing the function (CFShow) will be abandoned. 要更改此行为,请使用“set unwindonsignal off”将放弃包含该函数(CFShow)的表达式的评估。

  2. Or "run.runDateTime" is some random variable 或者“run.runDateTime”是一些随机变量

EDIT: I'm not sure if I put these in the right place because it still doesn't work(I probably didn't). 编辑:我不确定我是否把它们放在正确的位置,因为它仍然不起作用(我可能没有)。

- (void)viewDidLoad {
      [super viewDidLoad];
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
      dateFormatter.timeStyle = NSDateFormatterNoStyle;
      [dateFormatter setDateFormat:@"MM/dd/yyyy   HH:mm"];
      run.RunDateTime = [dateFormatter dateFromString:@"12/02/2012   12:22"];
      NSString *dateTimeStr = [dateFormatter stringFromDate:run.RunDateTime];

     [run.RunDateTime retain];

      [dateFormatter release]; 
}

-(IBAction)respondButtonPressed:(id)sender{
   NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
   dateFormatter2.timeStyle = NSDateFormatterNoStyle;
   [dateFormatter2 setDateFormat:@"MM/dd/yyyy   HH:mm"];
   NSString *dateTimeStr = [dateFormatter2 stringFromDate:run.RunDateTime]; 

   [dateFormatter2 release]; 
}

I also have a couple of NSStrings in the struct and they work fine. 我在结构中也有几个NSStrings,它们工作正常。

You need to retain RunDateTime after you assign/create it from the date formatter. 从日期格式化程序分配/创建后,您需要保留RunDateTime。

 [run.RunDateTime retain];

Make sure to release it in your dealloc! 一定要在dealloc中释放它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM