2012-01-17 104 views
0

我正在开发iOS 5的大规模应用程序,在xcode中使用ARC。除了当我试图释放其中一个接口时,系统似乎运行良好。我使用名为WhirlyGlobe的框架在第一个视图控制器中创建3D交互式地球仪。我是否错过了ARC?

当我切换视图控制器(我有4之间),我注意到用于视图控制器与地球仪的内存没有被释放。所有其他的视图控制器(只使用简单的视图和图像)释放他们的内存很好 - 但全球保持常驻或看起来如此。在导航回全球时,由于“glsmLoadTextureLevelBuffer”中的1mb分配,我获得了近10mb的内存跳跃。

要继续我的问题 - 有什么更多的我可以做,与ARC活跃,帮助释放我的对象?我注意到我的viewDidUnload和dealloc的方法并不是在所有的调用,我可以得到什么火的唯一方法是使用viewDidDisappear(这是不理想的很明显) - 见下文:

- (void)clear 
{ 
[[NSNotificationCenter defaultCenter] removeObserver:self]; 

if (self.layerThread) 
{ 
    [self.layerThread cancel]; 
    while (!self.layerThread.isFinished) 
     [NSThread sleepForTimeInterval:0.001]; 
} 

self.glView = nil; 
self.sceneRenderer = nil; 

if (theScene) 
{ 
    delete theScene; 
    theScene = NULL; 
} 
self.theView = nil; 
self.texGroup = nil; 

self.layerThread = nil; 
self.earthLayer = nil; 
self.vectorLayer = nil; 
self.labelLayer = nil; 
self.interactLayer = nil; 

self.pinchDelegate = nil; 
self.panDelegate = nil; 
self.tapDelegate = nil; 
self.longPressDelegate = nil; 
self.rotateDelegate = nil; 
} 

- (void)viewDidDisappear:(BOOL)animated { 
    NSLog(@"dealloc - viewDidDisappear"); 
    [self clear]; 
} 

我m设置我不再需要的所有零。这是最佳做法吗?

全球设置代码: [super viewDidLoad];

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

// Set up an OpenGL ES view and renderer 
EAGLView *ev = [[EAGLView alloc] initWithFrame:CGRectMake(0, 0, 824, self.view.frame.size.height)]; 
self.glView = ev; 
self.sceneRenderer = [[SceneRendererES1 alloc] init]; 
UIColor *whiteC = [UIColor whiteColor]; 
[sceneRenderer setClearColor:whiteC]; 
glView.renderer = sceneRenderer; 
glView.frameInterval = 2; // 60 fps (2) 
[self.view addSubview:glView]; 
self.view.backgroundColor = [UIColor blackColor]; 
self.view.opaque = YES; 
self.view.autoresizesSubviews = YES; 
//glView.frame = self.view.bounds; 
glView.frame = CGRectMake(275, GLOBE_HEIGHT_FIX, 768, SCREEN_HEIGHT+STATUS_BAR_HEIGHT); // was 260 x 
glView.backgroundColor = [UIColor whiteColor]; 
self.view.backgroundColor = [UIColor whiteColor]; // red for debug 

// Create the textures and geometry, but in the right GL context 
[sceneRenderer useContext]; 

self.texGroup = [[TextureGroup alloc] initWithInfo:[[NSBundle mainBundle] pathForResource:@"bdGlobe_info" ofType:@"plist"]]; 

// Need an empty scene and view 
theScene = new WhirlyGlobe::GlobeScene(4*texGroup.numX,4*texGroup.numY); 
self.theView = [[WhirlyGlobeView alloc] init]; 
[theView setFarPlane:5.0]; 
[theView setHeightAboveGlobe:GLOBE_HEIGHT_VIEW]; 
if (globeShouldAnimate) glView.alpha = 1.0; 

// Need a layer thread to manage the layers 
self.layerThread = [[WhirlyGlobeLayerThread alloc] initWithScene:theScene]; 

// Earth layer on the bottom 
self.earthLayer = [[SphericalEarthLayer alloc] initWithTexGroup:texGroup]; 
[self.layerThread addLayer:earthLayer]; 

// Set up the vector layer where all our outlines will go 
self.vectorLayer = [[VectorLayer alloc] init]; 
[self.layerThread addLayer:vectorLayer]; 

// General purpose label layer. 
self.labelLayer = [[LabelLayer alloc] init]; 
[self.layerThread addLayer:labelLayer]; 

self.interactLayer = [[InteractionLayer alloc] initWithVectorLayer:self.vectorLayer labelLayer:labelLayer globeView:self.theView 
                 countryShape:[[NSBundle mainBundle] pathForResource:@"10m_admin_0_map_subunits" ofType:@"shp"] 
                 oceanShape:[[NSBundle mainBundle] pathForResource:@"10m_geography_marine_polys" ofType:@"shp"] 
                 regionShape:[[NSBundle mainBundle] pathForResource:@"10m_admin_1_states_provinces_shp" ofType:@"shp"]]; 
self.interactLayer.maxEdgeLen = [self.earthLayer smallestTesselation]/10.0; 
[self.layerThread addLayer:interactLayer]; 

// Give the renderer what it needs 
sceneRenderer.scene = theScene; 
sceneRenderer.view = theView; 

// Wire up the gesture recognizers 
self.panDelegate = [PanDelegateFixed panDelegateForView:glView globeView:theView]; 
self.tapDelegate = [WhirlyGlobeTapDelegate tapDelegateForView:glView globeView:theView]; 
self.longPressDelegate = [WhirlyGlobeLongPressDelegate longPressDelegateForView:glView globeView:theView]; 

// Kick off the layer thread 
// This will start loading things 
[self.layerThread start]; 
+2

你在应用程序上运行泄漏仪器吗?我的第一个倾向将是检查WhirlyGlobe的泄漏。 – 2012-01-17 01:19:35

+0

我已经运行泄漏仪器 - 无论如何,每次全球部分打开时,只有1mb内存分配的巨大跳跃......如果我弹出视图控制器并设置,整个部分不应该被释放它为零? – 2012-01-17 01:26:59

+4

您可以使用此分配工具。使用堆镜头,您可以在应用程序的生命周期中的各个点上标记堆,并比较构成每个快照点处内存当前分配的对象图。这应该有助于缩小保留的内容和由谁来保存的内容。 – 2012-01-17 01:33:11

回答

3

您可以使用此分配工具。使用堆镜头,您可以在应用程序的生命周期中的各个点上标记堆,并比较构成每个快照点处内存当前分配的对象图。这应该有助于缩小保留的内容和由谁来保存的内容。

0

这是轶事,但可能会有类似的情况。

我刚刚遇到了这样的情况,即使它们是以静态方式访问(例如,[SingletonThing instance].thing),因为autorelease池没有耗尽,我的对象在ARC中仍然没有被释放。这个原因是一个奇怪的无尽循环,它运行在自己的线程上。为封闭代码单独放置一个@autorelease块。

在另一方面,即使你的(或库)对象中的一个具有UIView作为一个子视图,我觉得 UIViewController中永远不会viewDidUnload,因此从来没有的dealloc。我必须根据经验来检查。

0

确保您的-(void)viewDidDisappear:(BOOL)animated被调用。

如果使用

[self.view addSubview:yourViewController.view];

[yourViewController.view removeFromSuperview];

然后viewDidDisappear:viewDidAppear:将不会被调用

当您使用这些回调只会被调用 在IOS 5 或presentModalViewController:dismissViewControllerAnimated:在IOS 5 或dismissModalViewControllerAnimated: 或使用UINavigationController提出并解散你的viewController

0

我发现这个问题使用堆镜头(感谢马克·亚当斯)之后 - 原来我没”不会让一些代表弱实体,所以在更改视图控制器时不会被释放。默认强壮的代表保持居民身份。

感谢所有的建议,他们都帮助我指出了正确的方向:)

+0

触摸或与全球互动现在崩溃(因为我将所有代表转换为弱实体)......只是将所有代表设置为零可以接受吗? – 2012-01-19 00:42:40