lerosua
lerosua

Reputation: 1

Objective C: Class containing single NSInteger need releasing ?

I have a class which contains single NSInteger property

@interface myTag
    @property (nonatomic) NSInteger tag;
    @property (nonatomic) NSInteger index;
@end

I using it in code like

mytag = [[myTag alloc] init];
int num = (int)mytag.tag; 
[mytag release];
NSLog(@"num = %d",num);

my questions are

  1. In objc,all class run in pointer, that means mytag.tag return pointer ?
  2. So num is invalid ?
  3. Do i really need to call [mytag release], because nsinteger is not really class,just typedef of int.

But if I not call release, the Xcode analyzer warns me about memory leak

Upvotes: 0

Views: 422

Answers (1)

Krishnabhadra
Krishnabhadra

Reputation: 34265

  1. No, mytag.tag returns the value of NSInteger variable tag.
  2. num is valid
  3. You need to call [mytag release] because you alloc'ing it..Simple rule of objective C memory management is whatever you alloc/retain/copy, you need to release.

Also your third question, you are asking about mytag variable, which is not NSInteger, but an instance of myTag class..

A small advice for you, before jumping into projects , please take some time to read objective C memory management basics (which are not so easy, I must say)..Apple documentation is a good place to start..this tutorial is very informative about ARC..

Upvotes: 1

Related Questions