开发者

UILabel changing during runtime, managing memory for changing strings

开发者 https://www.devze.com 2023-01-24 02:26 出处:网络
I have a couple labels I am using as a HUD for the player during a game. I am updating these labels frequently, so that the player has up-to-date information. The problem is is that I\'ve been using

I have a couple labels I am using as a HUD for the player during a game. I am updating these labels frequently, so that the player has up-to-date information. The problem is is that I've been using

uiLabel.text = [NSString stringWithFormat:@"%3.0f", value];

to pass the new value that the label should have. I have noticed, however, that I have something of a soft-memory leak here. As I'm doing this update multiple times a second and this creates a string that is set to autorelease, I'm ending up taking more memory than I need. And keeping it, as the view is not going away.

I also tried to alloc and release strings explicitly, such as:

NSString* value = [[NSString alloc] initWithFormat: @"%3.0f", value];
uiLabel.text = value;
[valu开发者_运维问答e release];

However, I find that this seems to cause the same thing, but faster, though I don't know why. In this situation I would have thought there should never be strings sitting around waiting to be released at all, since I'm so explicitly dismissing them.

Can anyone see what I'm doing here that I obviously am failing to see? Is there a better/more preferred way to handle this? Some cursory searching didn't turn up much for me.


You're not doing anything out of the ordinary. Even with:

uiLabel.text = [NSString stringWithFormat:@"%3.0f", value];

the autorelease pool gets drained every time your code returns control to the run loop (so at least as often as you see the UI updating). If you see growing memory allocations, you should look elsewhere.

0

精彩评论

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