1
@property (nonatomic,copy) NSString *orginalString;

..
NSString *tmpString =[[NSString alloc] init];
self.orginalString=tmpString;
[tmpString release];
NSString *newString =self.orginalString;  

What happens here to newString?, is this correct what I am doing?

orginalString retain count 1 at first, and when it is referenced with another pointer "newString" its retain count will be 2? do I need to say "self.orginalString=nil" in the end? there are serious memory leaks but dont know it is something related with this.

Spring
  • 11,333
  • 29
  • 116
  • 185

2 Answers2

4
  NSString *tmpString =[[NSString alloc] init];

tmpString is allocated and initialized

  self.orginalString=tmpString;

the string pointed to by tmpString is copied over to self.originalString (because the property is declared copy);

  [tmpString release];

the string pointed to by tmpString is released, correctly; nothing happens to the string pointed to by self.orginalString;

  NSString *newString =self.orginalString;  

a new pointer is created and initalized to point at the same string pointed to by self.orginalString; nothing happens to self.orginalString retain count; it's just a second pointer pointing to the same object;

at this point, if you don't release somewhere self.orginalString, it will be leaked.

When you are dealing with memory management in OBjective C, my suggestion is not try and reason in terms of "retain count"; retain count is just the mechanism that is used by the ObjC runtime to keep track of objects; it is too low-level and there are too many other objects around to increase or decrease the retain count, so that you immediately loose count.

The best way, IMO, is reasoning in terms of ownership: when an object wants ownership of another, it will send a retain; when it has done with it, it sends release. Ownership is a local concept to a class, so it is easy to track down.

So, when you do:

   NSString *newString =self.orginalString;  

newString is just a pointer to a not-owned object; you do not need to balance that assignment with a release; on the contrary, if you do:

   NSString *newString = [self.orginalString retain];

you are making yourself responsible for releasing the object when you have done with it.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • tnx you said it will leak if I dont release, is it because I said NSString *newString =self.orginalString? and I already release it in dealloc, also do I have to nillify it in my methods? – Spring Aug 04 '11 at 08:42
  • 1
    no, that assignment `NSString *newString =self.orginalString` will not require you to release once more the object. You need to release `self.orginalString` in the `dealloc` method, though, because when you assigned to it, you got ownership for the object. If you do in `dealloc`, it is already enough. – sergio Aug 04 '11 at 08:49
  • tnx I need to reset its value in the code, so nillify it in some other method, does it have consequences? – Spring Aug 04 '11 at 08:56
  • You can set the value of a property to `nil` as many times as you like. – sergio Aug 04 '11 at 08:58
  • if I am also allocating and initing that property in some method and then I am nillifying it, then the object does still in the memory and allocated? or it is there but its value is null – Spring Aug 04 '11 at 09:01
  • the pointer value is nil; the object may exist or not depending on whether there are other objects that have retained it. Only after all the objects that retained the string have released it, the string will be deallocated. – sergio Aug 04 '11 at 09:05
  • I would b so glad if you can also help me on this question which I am banging my head to walls http://stackoverflow.com/questions/6928028/objective-c-alloc-release-error – Spring Aug 04 '11 at 09:23
  • 1
    I think that Kendall Helmstetter Geln's answer is correct about `theXML`. If you have more problems, please, at lease update the code and your findings after applying his suggestion (using autorelease when `alloc] init` `theXML`. – sergio Aug 04 '11 at 09:43
  • @sergio let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2134/discussion-between-xdeveloper-and-sergio) – Spring Aug 04 '11 at 15:16
1

You should visit this link. Actually, we should not check memory leaks with retain count, atleast for NSString.

To check memory leaks, always use Instruments coming with Xcode.

Community
  • 1
  • 1
Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55