English 中文(简体)
• 如何确保其他许多类人所呼吁的C类目标人群的记忆不泄露
原标题:How to ensure no memory leak for Objective-C class that is called by many other class
  • 时间:2009-10-09 02:00:35
  •  标签:

我有以下管制人员,他们将根据国旗和准地财产组合完成不同的任务。 这两种财产的价值将由提及该控制人的许多其他类别确定。 问题是,每个打电话的班级如何分配价值,何时发放,以便不出现记忆泄漏?

@interface SampleController {
    NSMutableArray *param;  
    NSString *flag;
}
@property (nonatomic, retain) NSMutableArray *param;
@property (nonatomic, retain) NSString *flag;
@end


@implementation SampleController
@synthesize param;
@synthesize flag;

- (id)init
{
   param = [[NSMutableArray alloc] initWithCapacity:0];
   flag = @"nothing";
}
@end
问题回答

但这取决于你如何称你的控制者:

  1. in an instance variable of an other object : you have to release it in this object s deallocate methode
  2. in a function : you should release it when you do not need it anymore (retained by another object for example or it finished the job in this function), if you want to return it, just sent the message "autorelease" to it and the NSAutoReleasePool will do the job for you.

为了分配价值,你可以

  1. set the mutable array with the setParam:(*NSMutableArray)theArrayYouWantToReplaceYourArrayWith
  2. access it directly with [[yourSampleController param]addObject:(id)objectYouWantToAdd]...
  3. or more convenient : [yourSampleController.param addObject:(id)objectYouWantToAdd]

添加目标:这里的信息是一个例子,你可以看到在NSMutable Array类参考中修改阵列(移动、分类、......)的方法。

由于你是国家安全局,而不是国家安全委员会,你无法改变你的影响力,但你可以接触。

  1. [yourSampleController getParam]
  2. [yourSampleController param]
  3. yourSampleController.param

If you want to avoid leaks in general, build your project with the Instrument tool in leak mode and look at the objects that are leaked if you found some that are declared in your functions. You can also check the Clang Static Analyzer (free static debugger) which is quite good if you have a lot of files.

我希望我能帮助你。

Julien





相关问题
热门标签