Sunday, December 8, 2013

Memory management issues,

Brother used to be engaged. Net and java, never care what memory overflow problems recently because of the need, contact with objective-c, is this problem done well dizzy, do not know when that retain, When are the release. Anyway, add these programs to hang. . Please bits heroes pointing, for example, the following code is commented out in the place is I think the added retain or release of the place, but actually not so. Can you explain to me about it? Brother here gratitude. . . Code may be written sucks, get used to. Net, I do not know what that note, please have patience to see. . .
 - (BOOL) webView: (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType 
{
NSString * urlString = [[request URL] absoluteString];
if ([urlString hasPrefix: @ "cmd:"]) {
NSRange range = [urlString rangeOfString: "?" @] ;/ / This does not require the release of non-pointer type it?
NSString * cmd = [[urlString substringToIndex: range.location] substringFromIndex: 4] ;/ / Why this variable in the final release will hang?
NSString * paramsString = [urlString substringFromIndex: range.location + range.length];
NSArray * params = [paramsString componentsSeparatedByString: @ "&"];
if ([cmd caseInsensitiveCompare: @ "changeLocation"] == NSOrderedSame) {
NSString * lat, * lng;
for (NSObject * item in params) {
NSString * param = (NSString *) item;
range = [param rangeOfString: @ "="];
NSString * name = [param substringToIndex: range.location];
NSString * value = [param substringFromIndex: range.location + 1];
if ([name caseInsensitiveCompare: @ "lat"] == NSOrderedSame) {
lat = value;
/ / [Lat retain];
} Else if ([name caseInsensitiveCompare: @ "lng"] == NSOrderedSame) {
lng = value;
/ / [Lng retain];
}
/ / [Name release];
/ / [Value release];
/ / [Param release];
}
[Coordinate setText: [NSString stringWithFormat: @ "current latitude is:% @ Longitude is:% @", lat, lng]];
/ / [Lat release];
/ / [Lng release];
}
/ / [Cmd release];
/ / [ParamsString release];
/ / [Params release];
}
return YES;
}



Reply:
Hey, in principle, you alloc or retain, and you release, you do not alloc or retain, and do not release.
Reply:
cited a floor hjy_x reply:
hey, in principle, you alloc or retain, and you release, you do not alloc or retain, you do not release .

You mean this:
NSString * cmd = [[urlString substringToIndex: range.location] substringFromIndex: 4];
Do not need to release too? ? But when I debug cmd observation of retainCount 1 ah. . .
Reply:
The different methods of creating objects from talking about:
1. Use alloc / init to create objects
 NSArray * array = [[NSArray alloc] init]; 
/ / Need to manually release
[Array release];


2.autorelease
 NSArray * array = [[[NSArray alloc] init] autorelease]; 
/ / No need to manually release, but to the auto-release pool


3 class method
 NSArray * array = [NSArray array]; 
/ / No need to manually release, has been set to automatically release


Reply:
reference to the third floor yiyaaixuexi reply:
this from talking about the different ways to create objects:
1. Use alloc / init to create objects
C / C + + code
NSArray * array = [[NSArray alloc] init];
/ / Need to manually release
[Array release];


2.autorelease
C / C + + code
NSArray * array = [[[NSArray alloc] init] autorel ......


Thank you, that I do not need to retain and release here, is not it? ?
Reply:
Do not. . .

Generally, NSString alloc temporary variables and they do not retain, declared out of the need to manually release the

The system will default to automatically add them inside, belong to the system management, rather than developers

Another point to note is that almost all of the object pointer (not a pointer is not discovered, it may be my lack of experience), pay attention to the release point of the memory space, generally there is no problem. Just know that at the memory of their own claims management, or their own management systems on the OK
Reply:
references, 5th Floor a21064346 reply:
not. . .

Generally, NSString alloc temporary variables and they do not retain, declared out of the need to manually release the

The system will default to automatically add them inside, belong to the system management, rather than developers

Another point to note is that almost all of the object pointer (not a pointer is not discovered, it may be my lack of experience), pay attention to the release point of the memory space, generally there is no problem. Just know that at the memory of their own claims management, or their own management systems on the OK ......


NSRange range = [urlString rangeOfString: @ "?"];
This is also a pointer to it? ? Pointer not to add an asterisk Why? ?
Reply:
object alloc, init, new needs created release, retain objects need to manually release, is not required to release the basic data types, remember almost, I can see the specific blog. It depends on the version you use Xcode, iphone mac development and general development is different. But above that principle will not change.
Reply:
LZ brother suggested a look book! Another signature is very domineering brother LZ!

No comments:

Post a Comment