开发者

Repeat alloc to same pointer - what happens?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜