-1

Possible Duplicate:
Objective C alloc/release error

When I try the code below it crashes with BAD_ACCESS if I release newString. What am I missing here?

As far as I know, when I use "copy" as an accessor I got the ownership of the object, so even should I release the other referencing pointer, retain count should still stay 1+, so that object should still be in memory?

P.S. This is a singleton class if it makes a difference.

.h file

NSString *originalString;
@property (nonatomic,copy) NSString *originalString;

.m file

@synthesize originalString;
...
NSString *newString = [[NSString alloc] init...];
self.originalString = newString;
[newString release];  //this lines makes it crash

I've also tried

self.originalString = [newString copy];

with no luck..

Community
  • 1
  • 1
Spring
  • 11,333
  • 29
  • 116
  • 185
  • Please don't repost the same question. If you're unhappy with the way you stated it the first time, you can always edit your original question. – Caleb Aug 03 '11 at 21:43

5 Answers5

1

Have you tried using retain instead of copy?

http://wiki.cs.unh.edu/wiki/index.php/Objective_C_Property_Attributes#Setter_Semantics

I'm not really sure whether or not this would actually work if copying doesn't, but using retain instead of copy should cause the reference count to the string to increment by 1 when assigned to the property, bringing it up to 2 from the 1 it was when the string was alloced, so when you use [newString release]; on the line after the property assignment, the string should end up with reference count 1 and thus continue to exist.

JAB
  • 20,783
  • 6
  • 71
  • 80
  • There should be no difference between retain and copy in this case, and `copy` is usually more appropriate for an NSString value anyway. – BJ Homer Aug 03 '11 at 21:16
  • @BJ Homer: It's been a while since I worked with Objective-C, but it seemed like something that would be simple enough to test, and if it does work he might want to figure out why one works but the other doesn't. – JAB Aug 03 '11 at 21:23
  • @JAB tnx this is the long explaination of problem http://stackoverflow.com/questions/6928028/objective-c-alloc-release-error-in-singleton-class/6929553#6929553 – Spring Aug 03 '11 at 21:33
  • @JAB retain also did not work – Spring Aug 03 '11 at 21:35
  • @BJ Homer: Ah, and I see from Caleb's comment on filipe's answer why there's no difference in this case. – JAB Aug 04 '11 at 13:17
1

[newString relase]; //this lines makes it crash

relase probably isn't the name of a method. Did you mean release?

Assuming the above was just a typographical error in your question, and not an error in your code, then your code is correct, and you'll need to provide more information about the error. Pasting the actual code of the method in question would be helpful in that case.

BJ Homer
  • 48,806
  • 11
  • 116
  • 129
  • 1
    Yeah, if you really did spell it "relase" and not "release" then you'll get a crash due to an "unrecognized selector". – Hot Licks Aug 03 '11 at 21:19
  • @BJ Homer tx this is the long explaination of problem http://stackoverflow.com/questions/6928028/objective-c-alloc-release-error-in-singleton-class/6929553#6929553 – Spring Aug 03 '11 at 21:33
1

this code looks correct, are you sure the crash isn't somewhere else?


EDIT: as noted in the comments, NSString being immutable won't cause the copy to allocate a new object. I've edited the answer for the mutable case, just in case someone stumbles into this later and doesn't read the whole thing. Now back with our regular programming.


Don't know if this might be the problem, but note that, if you were using a mutable object like NSMutableString, with copy you would not increment the retain count, you would effectively create a new object, so what would happen:

NSMutableString* newString = [[NSMutableString alloc] init..]; // allocate a new mutable string
self.originalString=newString; // this will create yet another string object
// so at this point you have 2 string objects.

[newString relase];  //this will in fact dealloc the first object
// so now you have a new object stored in originalString, but the object pointed
// to by newString is no longer in memory.
// if you try to use newString here instead of self.originalString,
// it can indeed crash.

// after you release something, it's a good idea to set it to nil to
// avoid that kind of thing
newString = nil;
filipe
  • 3,370
  • 1
  • 16
  • 22
  • tx this is the long explaination of problem http://stackoverflow.com/questions/6928028/objective-c-alloc-release-error-in-singleton-class/6929553#6929553 – Spring Aug 03 '11 at 21:33
  • 1
    Except that NSString is immutable, so `copy` just retains the string and returns the same pointer. – Caleb Aug 03 '11 at 21:38
0

You may consider to migrate your codes to Xcode 4.1 with ARC, then you would not need to worry about retain and release of objects.

David.Chu.ca
  • 37,408
  • 63
  • 148
  • 190
  • ARC isn't available in Xcode 4.1. You're looking for Xcode 4.2, which isn't publicly released yet. – BJ Homer Aug 03 '11 at 21:22
  • So Apple finally got around to implementing fully automatic reference counting for iOS apps written in Xcode? – JAB Aug 04 '11 at 13:14
  • Like other feature and technology, Apple has taken time to work out this new strategy. It is better than garbage collector, fast and efficient. You will find more from WWDC 2011 videos. – David.Chu.ca Aug 05 '11 at 03:14
0

Have you tried just adding autorelease to your *newString init? Then just take out the manual release all together.

NSString *newString = [[NSString alloc] init...] autorelease];
Bill Burgess
  • 14,054
  • 6
  • 49
  • 86