【问题标题】:I am getting the leak in the following code我在以下代码中得到了泄漏
【发布时间】:2010-09-25 12:48:41
【问题描述】:
我得到 100% 的泄漏。不知道对象返回后如何释放
你能解释一下如何释放分配的 Titles 对象的过程吗?
-(Titles *)listTiles
{
Tiles* tile = [[Tiles alloc] init];
tile.googleTile_X = (int)tileX;
tile.googleTile_Y = (int) pow(2, aZoom) - 1- tileY ;
tile.zoomLevel = aZoom;
return tile;
}
【问题讨论】:
标签:
iphone
objective-c
memory-management
memory-leaks
【解决方案1】:
您正在发送-alloc,,但未能向您创建的对象发送-release 或-autorelease。
阅读 Apple 关于内存管理的介绍性文档。
【解决方案2】:
总的来说,这取决于,但在那种特殊情况下,我相信你可以使用return [tile autorelease]。
P.S.:请正确格式化您的代码。
【解决方案3】:
-(Titles *)listTiles
{
Tiles* tile = [[[Tiles alloc] init] autorelease];
tile.googleTile_X = (int)tileX;
tile.googleTile_Y = (int) pow(2, aZoom) - 1- tileY ;
tile.zoomLevel = aZoom;
return tile;
}