这个问题关系到这一个,但更简单.[我想我可能接近这些愚蠢问题的结尾,可以开始认真的事业:)].
我有一个retain属性,并设置为这样:
UINavigationController *thing = [[UINavigationController alloc] initWithRootViewController:one];
// thing's retain count is one
navController = thing;
// thing's retain count is still one!
[thing release];
// now retain count is zero, which is wrong
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么保留计数变为零.navController被定义为
@property (nonatomic, retain) UINavigationController *navController;
Run Code Online (Sandbox Code Playgroud)
财产不应该将保留计数增加一个吗?
我有一个问题,即发布视图的次数太多了.虽然理论上很简单,因为我将uiview移动到uiview,这是uiview的一个子类,并且是动画的,所以它不是我可以轻松修复的东西.它只会在一定条件下崩溃10%,即使在这些条件下也只有30%的时间.
换句话说,它有点复杂.有时在我的dealloc方法中,此UIView的保留计数已经为1(在视图发布时会释放),因此不应再次释放.所以我做的是这样的:
if ([mainView retainCount] > 1) {
NSLog(@"released");
[mainView release];
}
Run Code Online (Sandbox Code Playgroud)
与发布的崩溃一致通常被称为,但并非总是如此,并且当我有时期望它崩溃时它几乎发生.我用这个代码检查了泄漏,它从不泄漏.
现在实际的问题......由于保留计数而释放一些东西是错误的吗?我已经尝试了许多不同的方法来解决这个问题,到目前为止,这是唯一可靠且无泄漏的方法.
编辑:如果没有,那么将一个UIView复制到另一个UIView的更好方法是什么?
mainView = newView;
[newView release];
Run Code Online (Sandbox Code Playgroud)
我尝试先释放mainView,然后在newView上调用copy,但这会崩溃.以上也很有效,除非保留计数有时比预期低1,即使它从未在代码中释放任何其他内容.
我用这个代码.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
view = [[UIView alloc] init];
[_window addSubview:view];
[view release];
NSLog(@"count - %d", [view retainCount]);
[self.window makeKeyAndVisible];
return YES;
}
- (IBAction)click{
NSLog(@"count - %d", [view retainCount]);
}
Run Code Online (Sandbox Code Playgroud)
当我点击uibutton时 - 我的视图保留计数= 2.为什么会发生这种情况?
我创建了一组相同的自定义类型的对象.所有对象都具有方法showDeleteButton了hideDeleteButton.我发现当我隐藏删除按钮(删除它)按钮时,按下了retainCounter == 2.
这里的代码:
-(void)showDeleteButton {
if(!isDeleteButtonLoaded) { // Check that method was't triggered twice
UIButton *aDeleteButton = [[UIButton alloc] initWithFrame:CGRectMake(-3, -7, 30, 29)]; // RC == 1
[aDeleteButton setImage:[UIImage imageNamed:@"close_button.png"] forState:UIControlStateNormal];
[self addSubview:aDeleteButton]; // RC == 2
[aDeleteButton addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
deleteButton = aDeleteButton;
[aDeleteButton release]; // RC == 1
isDeleteButtonLoaded = YES;
NSLog(@"delete button retain count (created): %d", [deleteButton retainCount]);
[self setNeedsDisplay];
}
Run Code Online (Sandbox Code Playgroud)
}
-(void)deleteButtonPressed:(id)sender {
[delegate deleteImageAtPath:self.imageFullPath];
Run Code Online (Sandbox Code Playgroud)
}
-(void)hideDeleteButton {
if(isDeleteButtonLoaded) {
NSLog(@"delete …Run Code Online (Sandbox Code Playgroud) 我已经分析了我的项目:这就是结果:

什么意思,怎么解决这个问题?
我将手动管理NSMutableDictionay的内存,而不使用自动释放.并且mutableDictonary中的每个对象都是NSArray,每次我在mutableDictionary中添加一个数组时,我将使用
NSArray *newArray = [[NSArray arrayWithArray:anArray] retain]
[mutableDict setObject:newArray forKey:@"aKey"];
Run Code Online (Sandbox Code Playgroud)
问题是,我怎么能保证没有内存泄漏?我在dealloc中直接使用[mutableDict release]是好的吗?mutableDict的retainCount是否等于其对象的所有retainCounts(保留的数组)的总和?
memory-management objective-c nsmutabledictionary retaincount
据我所知,如果一个对象的保留计数变为0,则会调用dealloc.但是我将保留计数设置为-1.这是什么意思?
我使用以下代码 -
Demo *obj1 = [[Demo alloc] init];
NSString *s = [[NSString alloc] initWithString:@"mithilesh"];
NSString *s1 = [NSString stringWithString:s];
[s release];
object_setInstanceVariable(obj1, [propertyName cString], s1);
//printing retain count
NSLog(@"retain count of name = %i",[obj1.name retainCount]);
Run Code Online (Sandbox Code Playgroud)
输出:
retain count of name = -1
Run Code Online (Sandbox Code Playgroud)
方法stringWithString返回的字符串:何时获取释放?
我写了下面这段代码:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
GameViewController *gameViewController = [[GameViewController alloc]initWithLevelNumber:([levelGroup intValue]*100+indexPath.row) Bonus:NO];
NSLog(@"Retain Counter =%d",gameViewController.retainCount);
[navController pushViewController:gameViewController animated:YES];
[gameViewController release];
NSLog(@"Retain Counter=%d",gameViewController.retainCount);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
两个日志的结果按顺序1和6!这怎么可能?我只调用一次alloc方法并在将控制器推到堆栈后释放.. alloc-> + 1,push-> + 1,release-> -1 = 1或不?
当我将它从堆栈中弹出时,我希望视图控制器是dealloc'd ..
objective-c uinavigationcontroller pushviewcontroller ios retaincount
我们都知道retain方法会为+1 retainCount,释放时会保留-1 retainCount,如果retainCount == 0,则对象dealloc.但是我遇到了一个问题,下面的代码运行得到了荒谬的结果
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property(nonatomic, retain) NSString *name;
@property(nonatomic, assign) NSInteger age;
@end
@implementation Person
- (id)init
{
self = [super init];
if (self) {
self.name = @"name";
self.age = 20;
}
return self;
}
- (void)dealloc
{
NSLog(@"dealloc");
[super dealloc];
}
@end
int main(int argc, const char * argv[])
{
Person *per1 = [[Person alloc] init];
NSLog(@"retainCount = %lu -- 1",[per1 retainCount]);
[per1 release];
[per1 retain];
NSLog(@"retainCount = %lu -- 2",[per1 retainCount]); …Run Code Online (Sandbox Code Playgroud) 我在NSNumber整个应用程序(非ARC)中使用不同的语法.为了更加了解情况,我试着看看如何NSNumber保留s,具体取决于它们的初始化语法.所以我做了以下事情:
NSNumber* a = @1;
NSNumber* b = [[NSNumber alloc] initWithInt:2];
NSNumber* c = [NSNumber numberWithInt:3];
NSLog(@"%d | %d | %d", a.retainCount, b.retainCount, c.retainCount);
Run Code Online (Sandbox Code Playgroud)
这个代码片段是用按钮点击执行的,输出让我感到困惑(重复点击):
73 | 27 | 6
78 | 159 | 22
78 | 160 | 22
78 | 161 | 22
78 | 162 | 22
78 | 163 | 22
85 | 169 | 22
85 | 170 | 22
85 | 171 | 22
85 | 172 | 22
Run Code Online (Sandbox Code Playgroud)
现在这并没有真正的目的(至少在我的情况下不是这样),但我想知道这些 …
通过简单的保留/释放方案查看此代码片段:
#import <Foundation/Foundation.h>
@interface SomeClass : NSObject
@end
@implementation SomeClass
@end
int main(int argc, const char * argv[])
{
SomeClass *aClass = [[SomeClass alloc] init];
NSLog(@"retainCount: %lu", [aClass retainCount]);
[aClass retain];
NSLog(@"retainCount: %lu", [aClass retainCount]);
[aClass release];
NSLog(@"retainCount: %lu", [aClass retainCount]);
[aClass release];
NSLog(@"retainCount: %lu", [aClass retainCount]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是结果输出:
2013-04-29 17:33:50.695 retainCount: 1
2013-04-29 17:33:50.697 retainCount: 2
2013-04-29 17:33:50.697 retainCount: 1
2013-04-29 17:33:50.698 retainCount: 1
Run Code Online (Sandbox Code Playgroud)
最后一个retainCount应为"0"或应用程序崩溃.为什么结果为"1"?!
请不要标记重复: - 我创建了几行
NSString *str = [[NSString alloc] init];
str = @"Test";
NSLog(@"%d",[str retainCount]);
Run Code Online (Sandbox Code Playgroud)
和输出是-1.请解释.
请解决这个问题.
NSString *S1 = @"abc";
//retain count of s1 ??
NSString *S2 = [S1 copy];
//retain count of s2 and s1 ??
NSString *S3 = [S2 copy];
//retain count of s3 and s2 ??
NSString *S4 = [S3 retain];
//retain count of s4 and s3 ??
NSString *S1 = @"xyz";
//retain count of s1, s2, s3 and s4 ??
Run Code Online (Sandbox Code Playgroud)
不建议retainCount因为我认为没有给出确切的结果.
retaincount ×13
objective-c ×10
ios ×6
memory-leaks ×2
copy ×1
ios4 ×1
iphone ×1
nsnumber ×1
nsstring ×1
uiview ×1