开发者

Repeat alloc to same pointer - what happens?

开发者 https://www.devze.com 2023-01-19 07:44 出处:网络
HI all, What happens if you repeat the following code more than once ? pointer * mypointer = [[object alloc]init];

HI all,

What happens if you repeat the following code more than once ?

pointer * mypointer = [[object alloc]init];

Do you开发者_高级运维 just increase the retain count of that object by one again ?

Thanks,

Martin


You wouldn't increase the retain count - only the retain message does that on an allocated object. Running that exact code more than once would actually error out, since you'd be duplicating the pointer * mypointer type declaration. However, if you had (for example):

pointer * mypointer = [[object alloc] init];
mypointer = [[object alloc] init];

You would have made two instances of object, each at its own position in memory, and you would have lost your handle on the first one (since mypointer now contains a reference to the second instance of object). Effectively, this is a leak.

0

精彩评论

暂无评论...
验证码 换一张
取 消