【问题标题】:Closing & reopening window leaks memory关闭和重新打开窗口会泄漏内存
【发布时间】:2011-08-26 03:01:18
【问题描述】:

这里有问题。我正在创建一个MAAttachedWindow,然后在某个事件发生时将其隐藏。代码如下所示:

-(void)toggleDetailShouldShow:(BOOL)show {
    if (show) {
        if (!attachedWindow) {
            NSPoint buttonPoint = NSMakePoint(NSMidX([[someView someImageView] frame]),
                                              NSMidY([[someView someImageView] frame]));
            attachedWindow = [[MAAttachedWindow alloc] initWithView:view 
                                                    attachedToPoint:buttonPoint 
                                                           inWindow:window 
                                                             onSide:12
                                                         atDistance:65.0];
            //setup here
            [attachedWindow setAlphaValue:0.0];
            [[[someView someImageView] window] addChildWindow:attachedWindow ordered:NSWindowAbove];
            [[attachedWindow animator] setAlphaValue:1.0];
        } 
    }
    else {
        if (attachedWindow) {
            [[[someView someImageView] window] removeChildWindow:attachedWindow];
            [attachedWindow orderOut:self];
            [attachedWindow release];
            attachedWindow = nil;            
        }
    }
}

当这被多次触发时,我的内存使用量稳步攀升。这有什么泄漏的原因吗?

【问题讨论】:

  • 具体泄露了什么? MAAttachedWindow?
  • 嗯,这就是我的猜测。 Instruments 根本没有显示泄漏,这就是我遇到障碍的地方。这是我在内存不断增加时触发的唯一代码。这就是为什么我认为是这个窗口泄漏了。
  • 只是半相关的,但我可以建议我的 SFBPopoverWindow 类 - github.com/sbooth/SFBPopovers - 它类似于 MAAttachedWindow 但功能略有不同
  • @sbooth:我几乎考虑过使用您的代码,但不幸的是,与 MAAttachedWindow 相比,屏幕边缘的检测相当不稳定。老实说,即使设置为自动,它也会以错误的方式弹出。
  • @sbooth:其实我刚才又看了一遍,配置错了。切换到您的代码,我没有回头。感谢您分享精彩的课程!

标签: objective-c cocoa memory-leaks


【解决方案1】:

这是使用Heapshot analysis 的完美场合。

你描述的症状和我写那篇文章时在Hex Fiend看到的症状完全一样。

很可能,无论发生什么泄漏(实际上是因为您的应用正在增加内存而导致的泄漏),仍然与应用对象图的其余部分相关联。

【讨论】:

  • 好吧,看起来是 Matt Gemmell 的 MAAttachedWindow 代码正在泄漏。感谢您的提示,它会派上用场的。
猜你喜欢
  • 1970-01-01
  • 2012-04-15
  • 2013-09-20
  • 1970-01-01
  • 2014-09-25
  • 2023-03-15
  • 1970-01-01
  • 2013-05-17
  • 1970-01-01
相关资源
最近更新 更多