Sunday, December 15, 2013

How the function returns the object is instantiated and released?

+ (NSMutableDictionary *) getList: (NSString *) key
{
......
NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithContentsOfFile: key];
return dict;
}



When callingNSMutableDictionary * obj = [self getList: key];
[Obj.release];

Instruments occasionally prompts a memory leak point
NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithContentsOfFile: key];

Consult the problem lie?
Reply:
 + (NSMutableDictionary *) getList: (NSString *) key 
{
/ / Use autorelease
[NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithContentsOfFile: key] autorelease];
return dict;
}

Reply:
 + (NSMutableDictionary *) getList: (NSString *) key 
{
/ / Use autorelease
NSMutableDictionary * dict = [[[NSMutableDictionary alloc] initWithContentsOfFile: key] autorelease];
return dict;
}

Reply:
1, NSMutableDictionary * obj = [self getList: key];
[Obj.release];
Is wrong, certainly given the
Because (getList :) is +

2, the code is released in the pool, you can return autorelease
Or after the call is automatically released
NSMutableDictionary * obj = [Class getList: key];
If there is a normal function of the words, they can not control. Since the end of the function, all references addresses will be automatically released. * Obj is a pointer, no alloc.
Reply:
Use autorelease will complain obj is empty
I now need to maintain lasting obj
Reply:
I'm referring to is when obj is empty window switch is empty after the first load the data is there.
Reply:
obj set class member variables,

obj = [[NSMutableDictionary alloc] init];

obj = [self getlist: key];

Try it.


Reply:
Add this. .


Within the method, remember obj release once.

dealloc, and also release a
Reply:
Best wording:

+ (NSMutableDictionary *) getList: (NSString *) key
{
......
NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithContentsOfFile: key];
return [dict autorelease];
}

Remember do not need to release the external.

No comments:

Post a Comment