开发者

NSString release problem

While testing some code, I meet a problem. Hear is my code and Log.

(IBAction) DynamicBtnClicked:(id)sender 
{
 NSString *strLog = [[NSString alloc] initWithString:@"SubView Index is..."];
 NSLog(@"initialized strLog address is = %p, retainCount = %d", strLog, [strLog retainCount]);
 if ([self.view.subviews count] > 0) {
     for (int i = 0 ; i < [self.view.subviews count] ; i++) {
         UIView *tmpView = [self.view.subviews objectAtIndex:i];
         strLog = [strLog stringByAppendingFormat:@"%d view`s index = %d, tag = %d", i, i,[tmpView tag]];
         NSLog(@"appended strLog address is = %p, retainCount = %d", strLog, [strLog retainCount]);
     }
 }

 NSLog(@"after appended strLog address is = %p, retainCount = %d", strLog, [strLog retainCount]);    
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:strLog delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
 [alert show];
 [alert release];

 NSLog(@"after using strLog address is = %p, retainCount = %d", strLog, [strLog retainCount]);
 [strLog release];
}

and below is Log

2011-06-03 14:36:11.038 MakeViewUsingCode[3918:40b] initialized strLog address is = 0x45c4, retainCount = 2147483647
2011-06-03 14:36:11.039 MakeViewUsingCode[3918:40b] appended strLog address is = 0x9c028d0, retainCount = 1
2011-06-03 14:36:11.040 MakeViewU开发者_StackOverflowsingCode[3918:40b] appended strLog address is = 0x9c021b0, retainCount = 1
2011-06-03 14:36:11.041 MakeViewUsingCode[3918:40b] after appended strLog address is = 0x9c021b0, retainCount = 1
2011-06-03 14:36:11.081 MakeViewUsingCode[3918:40b] after using strLog address is = 0x9c021b0, retainCount = 3
2011-06-03 14:36:11.087 MakeViewUsingCode[3918:40b] *** -[CFString release]: message sent to deallocated instance 0x9c021b0
dlopen(/Developer/Library/Xcode/PrivatePlugIns/DebuggerFoundation.ideplugin/Contents/Resources/DebuggerIntrospectionSupport.dylib, 0x0000000A)
dyld: loaded: /Developer/Library/Xcode/PrivatePlugIns/DebuggerFoundation.ideplugin/Contents/Resources/DebuggerIntrospectionSupport.dylib
Current language:  auto; currently objective-c

I know, there is a memory leak in my code but that is not a real problem.(it was on purpose.)

Real problem is, when I run my code, EXC_BAD_ACCESS runtime error occurs.

In my code, I send a message to receiver to release once but the Log shows me the reason for error is that the instance of NSString object is already deallocated!

If I send a message to receiver to release twice, where is first position?

Can anyone tell me where it is?

Thanks.


Do not rely on retainCount much. They can be misleading. Just stick to memory management rules and you will do just fine. The problem here is this line,

strLog = [strLog stringByAppendingFormat:@"%d view`s index = %d, tag = %d", i, i,[tmpView tag]];

After you do this, strLog points to an autoreleased string. You haven't claimed ownership on this string but still you release it at the end of the method. So imagine it being created once and released twice. That's your problem. If you just want to append the string to the instance you've created, you will have to use an NSMutableString.


When you assign that new string to strlog the first time in the loop, you're losing the reference to the first string.

NSString *strLog = [[NSString alloc] initWithString:@"SubView Index is..."];
for (int i = 0 ; i < [self.view.subviews count] ; i++) {
     // since you're not releasing the current reference, it's replaced with 
     // the following reference and thus leaked
     strLog = [strLog stringByAppendingFormat:@"%d view`s index = %d, tag = %d", i, i,[tmpView tag]];
}

I believe that stringByAppendingFormat returns an autoreleased NSString. If this is the case, I suspect the error you're seeing is because, you're releasing the last reference to strLog twice (the implicit autorelease when the object is created and the explicit release at the end of the method).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜