English 中文(简体)
保留财产时的放弃
原标题:Behavior of retained property while holder is retained

我是来自C++世界的开端目标方案家。 我认为,很难理解国家统计局目标提供的记忆管理:

我有以下几类:

@interface User : NSObject
{
  NSString* name;
}

@property (nonatomic,retain) NSString* name;

- (id)   initWithName: (NSString*) theName;
- (void) release;

@end

@implementation User

@synthesize name

- (id) initWithName: (NSString*) theName
{
    if ( self = [super init] )
    {
        [self setName:theName];
    }
    return self;
}

- (void) release
{
    [name release];
    [super release];
}

@end

在考虑以下法典时,我不能理解保留的结果:

NSString* name = [[NSString alloc] initWithCString:/*C string from sqlite3*/];
// (1) name retainCount = 1
User* user = [[User alloc] initWithName:name];
// (2) name retainCount = 2
[whateverMutableArray addObject:user];
// (3) name retainCount = 2
[user release];
// (4) name retainCount = 1
[name release];
// (5) name retainCount = 0

At (4), the retain count of name decreased from 2 to 1. But that s not correct, there is still the instance of user inside the array that points to name ! The retain count of a variable should only decrease when the retain count of a referring variable is 0, that is, when it is dealloced, not released.

最佳回答

页: 1 <代码>用户 其<代码>dealloc将打电话至>。

如果您改名为release,优先于dealloc,则保留的款项将按您的期望计算。

问题回答

http://www.ohchr.org。 因此:

- (void) release

应:

- (void) dealloc

您正在组合<代码> 姓名/代码>和<编码> 用户/代码> 记忆管理—每个物体都有自己的记忆。

页: 1 不适用<代码>NSObjects>release的方法,因此我假定不会按预期运作。 更改<代码>release至>dealloc>。 用户

在定点之后,我们可以审视一下你的其他问题。





相关问题
Duplicate a table row with UITableViewCellEditingStyleInsert?

I have an application based on the Core Data Books example, and I m coming to the conclusion that I need to give the user the ability to duplicate a row in the table - a set of data - and then let ...

Copy/Paste in Windows Forms with custom controls

I am writing a small application in C# using Windows Forms. I want to let my users copy and paste data around the application and there are some custom controls, for example one is a colour picker. ...

Copying only non-existent files in ant

I m deploying my project to a web-server to be deployed with java Web Start. However, Web Start uses modification date to figure out whether to download the resources again (by default). What I want ...

Copying one FlowDocument to Second FlowDocument

How can i copy the contents of one FlowDocument to another FlowDocument below is what i tryed foreach (var blk in fd1.Blocks) { fd2.Blocks.Add(blk); } fd1 is FlowDocument1 and fd2 is ...

emacs command to append to ring

How can I make an emacs command to copy text (to the kill ring) by appending? (Why is there no such built-in command?) Appending Kills mentions C-M-w (`append-next-kill ) which allows me to append ...

about get value from sqlite

Code Sample: NSString *str= [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 1)]; Test *t=[[Test alloc] init]; t.str=[str copy]; // why use "copy" here? [str release];

Insert into an STL queue using std::copy

I d like to use std::copy to insert elements into a queue like this: vector<int> v; v.push_back( 1 ); v.push_back( 2 ); queue<int> q; copy( v.begin(), v.end(), insert_iterator< queue&...

热门标签