First:

I just realized that one of my statements was incorrect;

NSString *string2 = [[NSString alloc] initWithFormat:@"mySecondString"];

does not make string2 retainCount to be 2147483647, it only becomes
2147483647 when   inserting @"" instead of @"mySecondString"

And regarding why use retainCount?
Well I am trying to find where is my mistake, since sometimes my
application crashes and I am quite (99.9%) sure that I am releasing a
object that shouldn't be.

Any ideas how to find such a bug?
Thanks in advance.
Regards

Ignacio.


On Thu, May 28, 2009 at 12:14 AM, Sherm Pendley <sherm.pend...@gmail.com> wrote:
> On Wed, May 27, 2009 at 9:59 AM, Ignacio Enriquez <nach...@gmail.com> wrote:
>>
>> 1.- What is the difference between string1 and string2? where
>> NSString *string1 = [NSString stringWithFormat:@"myFirstString"];
>> NSString *string2 = [[NSString alloc] initWithFormat:@"mySecondString"];
>
> You own string2 and must release it when you're done with it.
>
> You do not own string1 and must not release it.
>
>>
>> I thought that string1's memory allocation and release would be done
>> by the system (autoreleased) and string2's memory and release should
>> be done by me (by [string2 release])
>> Am i mistaking?
>
> Yes, you are mistaken. :-(
>
> The memory contract concerns ownership - i.e. what are the calling code's
> responsibilities with respect to retain and/or release. Many objects that
> you do not own are in fact autoreleased, but there is no guarantee of that.
> The only guarantee concerns whether the caller owns the returned object.
> That contract works both ways; the called code is also required to return an
> object that behaves correctly if the caller fulfills its end of the
> contract. *How* the called method does so is an implementation detail that's
> not part of the contract, and something the caller need not care about.
>
>> A funny thing is when doing:
>> NSLog(@"retainCount %i %i", [string1 retainCoung], [string2 retainCount]);
>> I got :
>> "retainCount  2147483647 2147483647"
>>
>> So It seems that both objects are autorelease objects... Why is that?
>>  I thougth that string2 retainCount would be 1.
>
> With no placeholders in your format, NSString is perfectly within its rights
> to return a singleton instance that represents the constant static string
> that's compiled into your binary. For such singletons, the retain count is
> often set to a "special" value that the -release method will recognize as
> meaning "can't touch this." So no, these objects are not autoreleased.
>
> But, it doesn't matter. All that your code needs to do is follow the
> contract, and release string2 when it's done with it.
>
>> 2.- How can I get two simple NSString instances with a retainCount equal
>> to 1
>
> You can't depend on any particular value of retainCount. You shouldn't even
> be looking at it - as you've seen, it's an internal implementation detail
> that can (and does) have values that are only meaningful if you're
> maintaining the target class - in this case, NSString.
>
> Take a step further back - what problem are you having, that led you to
> think that looking at retainCount would help you solve it?
>
> sherm--
>
> --
> Cocoa programming in Perl: http://camelbones.sourceforge.net
>
>
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to