我想知道你-retainCount到目前为止在什么情况下使用过,最后使用它可能发生的问题.
谢谢.
有没有办法快速将对象的保留计数注销到Xcode的控制台?如果没有,那么下一个最佳选择是什么?
在一本书中,说如下:
那么你怎么知道对象何时拥有,以及由谁拥有?请考虑以下示例:
Run Code Online (Sandbox Code Playgroud)NSString *str = [[NSString alloc] initWithString:@”Hello”]; NSString *str2 = str;在此示例中,您使用
alloc关键字forstr,因此您拥有str.因此,您需要在不再需要时释放它.但是,str2只是指向str,所以你不拥有str2,这意味着str2当你完成使用它时你不需要释放.
我认为所有权是按对象而不是变量或指针...所以我们不能说我们"拥有str"或"拥有str2"......我们拥有一个对象,它由任何一个str或者指向str2,如果我们使用[str release]或者[str2 release],它完全一样.
另一种描述是:
例如,考虑上一节中使用的示例:
Run Code Online (Sandbox Code Playgroud)NSString *str = [[NSString alloc] initWithString:@”Hello”]; NSString *str2 = str; [str release]; [str2 release]; //---this is not OK as you do not own str2---尝试发布
str2将导致运行时错误,因为您无法释放不属于您的对象.
[str2 release]如果以前调用过,我们实际上可以使用 [str release].如果我们这样做,那么该行[str release]会导致错误,因为现在 …
有人知道在调试模式下如何检查对象的保留计数?我试图添加一个表达式,[objInstance retainCount]但它没有用.我也在控制台中尝试了打印对象 PO [objInstance retainCount],但它再次无效.
#define kTestingURL @"192.168.42.179"
...
NSString *serverUrl = [[NSString alloc] initWithString:
[NSString stringWithFormat:@"http://%@", kTestingURL]];
NSLog(@"retain count: %d",[serverUrl retainCount]);
Run Code Online (Sandbox Code Playgroud)
为什么保留计数为2而不是1?
我有一个EXC_BAD_ACCESS在main(),这里是我的代码:
int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"TestBedAppDelegate");
[pool release];
return retVal;
}
@interface TestBedAppDelegate : NSObject <UIApplicationDelegate>
@end
@implementation TestBedAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[TestBedViewController alloc] init]];
[window addSubview:nav.view];
[window makeKeyAndVisible];
}
@end
- (void) action: (id) sender
{
[self highRetainCount];
}
@implementation TestBedViewController
- (void) highRetainCount
{
UIView …Run Code Online (Sandbox Code Playgroud) 在这里我得到了一些丑陋的代码:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy"];
NSDate *date = [NSDate date];
NSString *textWithYear = [NSString stringWithFormat:@"text and year %@", [dateFormatter stringFromDate:date] ];
[dateFormatter release];
NSLog(@"%i", [dateFormatter retainCount]); // returns 1 !
Run Code Online (Sandbox Code Playgroud)
如您所见,保留计数器返回1,我想这意味着该对象未被释放.如果我将该字符串更改为
[dateFormatter release], dateFromatter = nil;
Run Code Online (Sandbox Code Playgroud)
保留计数器返回0,这应该是因为它无法计算nil的保留:)
有什么东西我不了解保留计数器,或者这个对象真的没有发布?当我release第二次发送它(努力获得零保留计数)时,它会预期粉碎:)
还有一个问题:如果dateFormatter真的被释放了,为什么当我调用[dateFormatter retainCount]时它不会崩溃?
我对检查对象的保留计数有一点疑问:
请找到下面的代码,它在释放对象内存后显示retainCount为1.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect contentRect = [cell.contentView bounds];
UIImageView *thumbView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:IMAGE_CELL_BACKGROUND]];
thumbView.frame = contentRect;
cell.backgroundView=thumbView;
[thumbView release];
}
UIImageView *image=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"left_arrow.png"]];
**NSLog(@"Image retain count %d",[image retainCount]);**
image.frame=CGRectMake(290, 12.5, [UIImage imageNamed:@"left_arrow.png"].size.width, [UIImage imageNamed:@"left_arrow.png"].size.height);
image.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:image];
[image release];
**NSLog(@"Image retain count-- after %d",[image retainCount]);**
// Configure the …Run Code Online (Sandbox Code Playgroud) 请不要标记重复: - 我创建了几行
NSString *str = [[NSString alloc] init];
str = @"Test";
NSLog(@"%d",[str retainCount]);
Run Code Online (Sandbox Code Playgroud)
和输出是-1.请解释.
objective-c ×6
iphone ×4
retaincount ×4
ios ×3
cocoa ×2
nsstring ×2
cocoa-touch ×1
debugging ×1
null ×1
reference ×1
retain ×1
swift ×1