开发者

Should I write this release?

开发者 https://www.devze.com 2023-02-10 14:51 出处:网络
-- example.h -- @property ( copy ) NSString *string; @property ( retain ) Object *object; -- example.m --

-- example.h --

@property ( copy ) NSString *string;
@property ( retain ) Object *object;

-- example.m --
( void ) do {
    /开发者_运维百科/ I have used 'string' and 'object' using their setter method several times.
}

( void ) dealloc {
    [ string release ]; // Should I write this code?
    [ object release ]; // Should I write this code?
}

They didn't use alloc, copy, new. But they are pointing latest objects that were made in their setter method and not released. I really want to know about this situations though it is not very important.


You did not @synthesize these @propertys, so your code will not work. - Add a @synthesize directive like so:

@synthesize string, object;

Yes, you should send them the release message in -dealloc in this case, because you're using copy and retain, which both obtain ownership of the receiver.

- (void) dealloc {
   [string release];
   [object release];
   [super dealloc];
}

ALSO DO NOT FORGET TO INVOKE [super dealloc] AT THE BOTTOM OF YOUR -dealloc METHOD!!


yes, you should release both in this example.


Yes, because your NSString *string is a copy property and your NSObject *object is a retain property. By using their setters, your instance copies and retains the objects you assign to them respectively.


Because you @synthesized your properties, remember assigning the property to nil will release it for you, and ensure that code cannot continue to use it.

- (void) dealloc {
  string = nil;
  object = nil;
  [super dealloc];
}

If you had instance variables (iVars), then you should use release since there is no setter accessor that will release it for you.

0

精彩评论

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