-2

I am really confused with the code,my question is why I am getting value of the variables after releasing.

NSNumber *a=[[NSNumber alloc]initWithInt:2];
NSNumber *b=[[NSNumber alloc]initWithInt:3];
b=a;

[a release];
[b release];
NSLog(@"a=%@",a);
NSLog(@"b=%@",b);

OUTPUT: a=2 b=2

when I am allocating the veriables retain count increments by 1,I have no other code where retain count increments.So my question is after release message retain count will be 0 and the objects will be deallocated and I should not get the value. ////////////////////////////////////////////////////////////////// Hi all I found an answer but the logic behind it is not clear to me,If I put "I" after the integer number it gives the desired output.Will you please help.

NSNumber *a=[[NSNumber alloc]initWithInt:2I];
NSNumber *b=[[NSNumber alloc]initWithInt:3I];
b=a;

[a release];
[b release];

NSLog(@"a=%@",a);
NSLog(@"b=%@",b);

OUTPUT: a=0 b=0

4

1 に答える 1

0

オブジェクトに対して解放メッセージを送信すると、オブジェクトは実際にはメモリから削除されません。release メッセージは、参照カウントを 1 だけ減らすだけです。参照カウントがゼロの場合、オブジェクトはフリーとしてマークされます。次に、システムはそれをメモリから削除します。この割り当て解除が発生するまで、オブジェクトにアクセスできます。オブジェクトを解放しても、ポインタに nil を割り当てない限り、オブジェクト ポインタはオブジェクトを指します。

于 2013-08-07T05:40:24.997 に答える