iPhone 中的 NavigationController / TableView 内存泄漏

发布于 2024-10-06 20:22:06 字数 4256 浏览 0 评论 0原文

最近,我完成了我的第一个 iPhone 应用程序,它运行得很好......除了当我使用仪器进行了一次密集的会话一段时间后没有泄漏时,突然得到了大约 1.3Kb 的值。在堆栈跟踪中,它们都指向我在将 UIViewControler 推送到导航控制器后释放 UIViewControler 的确切行(我将将此代码发布到更下方)。泄漏似乎是在浏览 ViewControlers 并再次返回几次后发生的。我知道这是一个非常普遍的问题,但我希望有人能指出我正确的方向。我花了几天时间试图解决这个问题,它开始让我发疯!

我的应用程序是一个基于导航的应用程序,简而言之,它的工作原理如下:

从 UIViewControler 开始,按下按钮时,它会转到另一个包含 TableView 的 UIViewControler。 从这里开始,根据填充 TableView 的数组的内容,它可以转到包含 PickerView 的 UIViewControler,然后转到最后一个 UIViewControler,或者直接转到最后一个 UIViewControler,绕过带有 PickerView 的 UIViewControler。

以下是堆栈跟踪显示泄漏发生的代码:

SoundConfiger *third = [[SoundConfiger alloc] initWithNibName:@"SoundConfiger" bundle:[NSBundle mainBundle]];
    [third setTitle:@"Sound Link"];
    third.myData = myData;
    third.selectedRow = row;
    third.currentChoise = nil;
    [self.navigationController pushViewController:third animated:YES];
    [third release];    <---- Instruments says the leak is here on this line.

难道导航控制器现在不应该负责这个 UIViewcontroler 吗?我没有在其他任何地方调用retain onthird,它只存在于这个函数中。

我有一堆来自 UIkit 和 QuartsZone 的泄漏。我有其中一些的屏幕截图,在堆栈跟踪中,所有代码都指向相同的代码片段,在同一个函数中,在同一个对象中,如上所述在跟踪中的某个点:

这是图像的链接因为我还无法发布图像:链接不再有效

这是列表中第一个 GeneralBlock-16 的完整堆栈跟踪。粗体部分(第 29 行)是上面的代码片段:


0 libSystem.B.dylib malloc

1 CoreFoundation -[__NSArrayM insertObject:atIndex:]

2 CoreFoundation -[__NSArrayM addObject:]

3 UIKit -[UIView(UIViewGestures) addGestureRecognizer:]

4 UIKit -[UISwitch _commonInit]

5 UIKit -[UISwitch initWithCoder:]

6 UIKit UINibDecoderDecodeObjectForValue

7 UIKit UINibDecoderDecodeObjectForValue

8 UIKit -[UINibDecoderdecodeObjectForKey:]

9 UIKit -[UIView initWithCoder:]

10 UIKit UINibDecoderDecodeObjectForValue

11 UIKit -[UINibDecoderdecodeObjectForKey:]

12 用户界面工具包-[UIRuntimeConnection initWithCoder:]

13 UIKit UINibDecoderDecodeObjectForValue

14 UIKit UINibDecoderDecodeObjectForValue

15 UIKit -[UINibDecoder debugObjectForKey:]

16 UIKit -[UINib instantiateWithOwner:options:]

17 UIKit -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:]

18 UIKit - [UI视图控制器_loadViewFromnibamed:Bundle:]

19 Uikit- [UiviewController LoadView]

20 Uikit- [UiviewController]

21 Uikit- [UiviewController contentsCrollview]

[

22 UIKIT- _LayoutViewController:] 24 Uikit-

UinavigationController _startTransition:FromViewController :toViewController:]

25 UIKit -[UINavigationController _startDeferredTransitionIfNeeded]

26 UIKit -[UINavigationController PushViewController:transition:forceImmediate:]

27 UIKit 0x6ea9b5

28 UIKit -[UINavigationController PushViewController:animated:]

29 陷阱 -[SelectEventTypeviewcontroler ChooseThisOne] /Users/ chriswyllie/Documents/Booby Trap/Booby Trap/Classes/SelectEventTypeviewcontroler.m:91

30 CoreFoundation -[NSObject(NSObject) PerformSelector:withObject:withObject:]

31 UIKit -[UIApplication sendAction:to:from:forEvent:]

32 UIKit -[UIApplication sendAction:toTarget:fromSender:forEvent:]

33 UIKit -[UIControl sendAction:to:forEvent:]

34 UIKit -[UIControl(Internal) _sendActionsForEvents:withEvent:] 35

UIKit -[UIControl TouchesEnded:withEvent:]

36 UIKit -[UIWindow _sendTouchesForEvent:]

37 UIKit -[UIWindow sendEvent:]

38 UIKit -[UIApplication sendEvent:]

39 UIKit _UIApplicationHandleEvent

40 GraphicsServices PurpleEventCallback

41 CoreFoundation CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION

42 CoreFoundation __CFRunLoopDoSource1

4 3 CoreFoundation __CFRunLoopRun

44 CoreFoundation CFRunLoopRunSpecific

45 CoreFoundation CFRunLoopRunInMode

46 GraphicsServices GSEventRunModal

47 GraphicsServices GSEventRun

48 UIKit -[UIApplication _run]

49 UIKit UIApplicationMain

50 诱杀装置 main /Users/chriswyllie/Documents/Booby Trap/Booby Trap/main.m:14

51 诱杀装置启动


提前感谢您的帮助,我希望我只是在做一些愚蠢的事情,我需要一双新的眼睛来看待它。如果您需要更多信息,请告诉我。

Recently I have finished my first Iphone app and it works grate.... except for when i did a intense session using instruments to find leaks after awhile with no leaks, all of a sudden get about 1.3Kb worth. In the stack trace they all point to the exact line that I release a UIViewControler on after pushing it to the Navigation controller (Ill post this code a bit further down). The Leak seem to happen after a few time navagating through the ViewControlers and back again. I know this is a very generally question but I hope someone could point me in the right direction. I have spent a few days trying to solve this and it is starting to dive me crazy!

My app is a Navigation based one, here is how it works in a nutshell:

Starts with a UIViewControler which when a button is pressed goes to another UIViewControler containing a TableView.
From here depending on the contents of the array that populates the TableView it could go to a UIViewControler containing a PickerView then to the last UIViewControler or just go straight to the last UIViewControler bypassing the one with the PickerView.

Here is the code that the stack trace says the Leak is on:

SoundConfiger *third = [[SoundConfiger alloc] initWithNibName:@"SoundConfiger" bundle:[NSBundle mainBundle]];
    [third setTitle:@"Sound Link"];
    third.myData = myData;
    third.selectedRow = row;
    third.currentChoise = nil;
    [self.navigationController pushViewController:third animated:YES];
    [third release];    <---- Instruments says the leak is here on this line.

Shouldn't the NavigationController be responsible for this UIViewcontroler now? I have not called retain on third anywhere else and it only exists in this function.

I have a Bunch of Leaks from UIkit and QuartsZone. I have a screen shot bellow of some of them, in the stack traces all point to the same snippet of code, in the same function, in the same object as stated above at some point in the trace:

Here is a Link to the image as I can't post images yet: Link no longer valid

Here is the compleat stack trace from the first GeneralBlock-16 in the list. The one in bold (line 29) is the code snippet above:


0 libSystem.B.dylib malloc

1 CoreFoundation -[__NSArrayM insertObject:atIndex:]

2 CoreFoundation -[__NSArrayM addObject:]

3 UIKit -[UIView(UIViewGestures) addGestureRecognizer:]

4 UIKit -[UISwitch _commonInit]

5 UIKit -[UISwitch initWithCoder:]

6 UIKit UINibDecoderDecodeObjectForValue

7 UIKit UINibDecoderDecodeObjectForValue

8 UIKit -[UINibDecoder decodeObjectForKey:]

9 UIKit -[UIView initWithCoder:]

10 UIKit UINibDecoderDecodeObjectForValue

11 UIKit -[UINibDecoder decodeObjectForKey:]

12 UIKit -[UIRuntimeConnection initWithCoder:]

13 UIKit UINibDecoderDecodeObjectForValue

14 UIKit UINibDecoderDecodeObjectForValue

15 UIKit -[UINibDecoder decodeObjectForKey:]

16 UIKit -[UINib instantiateWithOwner:options:]

17 UIKit -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:]

18 UIKit -[UIViewController _loadViewFromNibNamed:bundle:]

19 UIKit -[UIViewController loadView]

20 UIKit -[UIViewController view]

21 UIKit -[UIViewController contentScrollView]

22 UIKit -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:]

23 UIKit -[UINavigationController _layoutViewController:]

24 UIKit -[UINavigationController _startTransition:fromViewController:toViewController:]

25 UIKit -[UINavigationController _startDeferredTransitionIfNeeded]

26 UIKit -[UINavigationController pushViewController:transition:forceImmediate:]

27 UIKit 0x6ea9b5

28 UIKit -[UINavigationController pushViewController:animated:]

29 Booby Trap -[SelectEventTypeviewcontroler ChooseThisOne] /Users/chriswyllie/Documents/Booby Trap/Booby Trap/Classes/SelectEventTypeviewcontroler.m:91

30 CoreFoundation -[NSObject(NSObject) performSelector:withObject:withObject:]

31 UIKit -[UIApplication sendAction:to:from:forEvent:]

32 UIKit -[UIApplication sendAction:toTarget:fromSender:forEvent:]

33 UIKit -[UIControl sendAction:to:forEvent:]

34 UIKit -[UIControl(Internal) _sendActionsForEvents:withEvent:]

35 UIKit -[UIControl touchesEnded:withEvent:]

36 UIKit -[UIWindow _sendTouchesForEvent:]

37 UIKit -[UIWindow sendEvent:]

38 UIKit -[UIApplication sendEvent:]

39 UIKit _UIApplicationHandleEvent

40 GraphicsServices PurpleEventCallback

41 CoreFoundation CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION

42 CoreFoundation __CFRunLoopDoSource1

43 CoreFoundation __CFRunLoopRun

44 CoreFoundation CFRunLoopRunSpecific

45 CoreFoundation CFRunLoopRunInMode

46 GraphicsServices GSEventRunModal

47 GraphicsServices GSEventRun

48 UIKit -[UIApplication _run]

49 UIKit UIApplicationMain

50 Booby Trap main /Users/chriswyllie/Documents/Booby Trap/Booby Trap/main.m:14

51 Booby Trap start


Thanks for your help in advance, I am hope I am just doing something silly and I need a fresh set of eyes to see it. Let me know If you need more info.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2024-10-13 20:22:06

好吧,我是一个工具:)

我没有在每个 viewControler 中正确处理 IBOutlet。

我没有使用 @property 和保留来声明每个 IBOutlet,而是在 viewDidUnload 方法中使用“self.someLabel = nil”(释放并将 IBOutlet 设置为 nil)来发出内存警告,然后最终在 dealloc 中释放。

因此工具。哈哈,

我所做的是声明“IBOutlet UILabel *someLabel;”在没有@property的标头中,这导致了上面看到的泄漏,它们不会立即发生,需要一段时间,当他们进行堆栈跟踪时,每件事都没有多大帮助。

我发现泄漏的唯一方法就是从我的应用程序中删除所有内容,我的意思是所有内容,直到泄漏消失。

感谢您的帮助 Ishu Gupta,我很高兴我们取得了成果。

以下是展示如何正确执行 IBOutlet 的相关链接:如果我不这样做会发生什么不保留IBOutlet?

Well I am a Tool :)

I was not handeling the IBOutlets correctly in each viewControler.

I was not declaring each IBOutlet using @property with a retain, using "self.someLabel = nil" (to release and set to the IBOutlets to nil) in the viewDidUnload method for a memory warning and then finally releasing in in dealloc.

thus tool. lol

What I was doing was declaring was "IBOutlet UILabel *someLabel;" in the header with no @property and this was causing the leaks seen above, they don't happen right away it takes a while and when they do the stack trace and every thing dose not help to much.

They only way I found what was leaking was striping everything out of my app and I mean everything until the leak went away.

Thanks for your help Ishu Gupta, I am glad we had a outcome.

Here are the relevant link to show how to do IBOutlets correctly: What happens if I don't retain IBOutlet?

起风了 2024-10-13 20:22:06

SoundConfiger *third = [[[SoundConfiger alloc] initWithNibName:@"SoundConfiger" bundle:[NSBundle mainBundle]] autorelease];

并删除

[第三次发布];这条线。

您只能修复因代码而产生的泄漏(忘记释放任何对象或一些错误的事情)。但有时你会遇到额外的泄漏,这些泄漏存在于 iphone sdk 中。事实上,iPhone 的某些功能或属性存在泄漏,因此只有当您更改这些功能或属性的用途时才能修复这些问题。
还有一件事,如果 1.3 KB 泄漏不会随着应用程序操作而增加,那也不错。

SoundConfiger *third = [[[SoundConfiger alloc] initWithNibName:@"SoundConfiger" bundle:[NSBundle mainBundle]] autorelease];

and remove

[third release]; this line.

You can fix only those leaks which comes because of your code(forget release any object or some wrong things). But some time you get extra leaks these are present in iphone sdk. Actully some of functions or properties of iPhone having leaks.so these can be fixed only when you change the use of these.
And one thing more 1.3 KB leak is not bad if it will not be increase with app operations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文