相关疑难解决方法(0)

何时使用-retainCount?

我想知道你-retainCount到目前为止在什么情况下使用过,最后使用它可能发生的问题.

谢谢.

memory-management objective-c retaincount

110
推荐指数
5
解决办法
2万
查看次数

Swift:如何注销保留对象的数量?

有没有办法快速将对象的保留计数注销到Xcode的控制台?如果没有,那么下一个最佳选择是什么?

iphone reference retain ios swift

16
推荐指数
2
解决办法
1万
查看次数

在Objective-C中,所有权是对象,而不是变量或指针?

在一本书中,说如下:

那么你怎么知道对象何时拥有,以及由谁拥有?请考虑以下示例:

NSString *str = [[NSString alloc] initWithString:@”Hello”];  
NSString *str2 = str;
Run Code Online (Sandbox Code Playgroud)

在此示例中,您使用alloc关键字for str,因此您拥有str.因此,您需要在不再需要时释放它.但是, str2只是指向str,所以你不拥有str2,这意味着str2当你完成使用它时你不需要释放.

我认为所有权是按对象而不是变量或指针...所以我们不能说我们"拥有str"或"拥有str2"......我们拥有一个对象,它由任何一个str或者指向str2,如果我们使用[str release]或者[str2 release],它完全一样.

另一种描述是:

例如,考虑上一节中使用的示例:

NSString *str = [[NSString alloc] initWithString:@”Hello”]; 
NSString *str2 = str;
[str release];
[str2 release]; //---this is not OK as you do not own str2---
Run Code Online (Sandbox Code Playgroud)

尝试发布str2将导致运行时错误,因为您无法释放不属于您的对象.

[str2 release]如果以前调用过,我们实际上可以使用 [str release].如果我们这样做,那么该行[str release]会导致错误,因为现在 …

objective-c

11
推荐指数
1
解决办法
1394
查看次数

如何在调试时检查保留计数

有人知道在调试模式下如何检查对象的保留计数?我试图添加一个表达式,[objInstance retainCount]但它没有用.我也在控制台中尝试了打印对象 PO [objInstance retainCount],但它再次无效.

iphone debugging objective-c ios retaincount

9
推荐指数
2
解决办法
1万
查看次数

为什么NSString保留计数2?

#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?

iphone cocoa-touch objective-c nsstring

4
推荐指数
2
解决办法
882
查看次数

main()中的EXC_BAD_ACCESS

我有一个EXC_BAD_ACCESSmain(),这里是我的代码:

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)

cocoa objective-c ios

2
推荐指数
1
解决办法
259
查看次数

释放对象:[obj release]; 是不够的,需要[obj release],obj = nil;?

在这里我得到了一些丑陋的代码:

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]时它不会崩溃?

null memory-management objective-c retaincount

0
推荐指数
1
解决办法
143
查看次数

保留iPhone SDK中的计数问题

我对检查对象的保留计数有一点疑问:

请找到下面的代码,它在释放对象内存后显示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)

iphone cocoa memory-management

-1
推荐指数
1
解决办法
316
查看次数

分配后RetainCount为-1​​?

请不要标记重复: - 我创建了几行

NSString *str = [[NSString alloc] init];
str = @"Test";
NSLog(@"%d",[str retainCount]);
Run Code Online (Sandbox Code Playgroud)

和输出是-1.请解释.

nsstring retaincount

-1
推荐指数
1
解决办法
202
查看次数