iPhone - appDidBecomeActive: 和运行循环启动之间数组丢失?

发布于 2024-08-18 04:43:38 字数 1241 浏览 11 评论 0原文

编辑:我在初始化数组后立即进行了一次手动保留数组的调用,并修复了它。持续时间。谢谢。

我将 NSMutableArray 保留在 UIScrollView 子类中,并向其中添加 UIImageView 子类的实例,因为每个实例都作为子视图添加。它需要是一个可变数组,因为我计划最终重新排列它们在数组中的顺序;这个错误在我尝试这样做之前就发生了。我使用 NSLogs 来显示数组的计数为 10,直到在我的 AppDelegate 中调用 applicationDidBecomeActive: 为止,并添加了一个计时器以在运行循环中每 0.001 秒触发一次。第一次发射时,它再也找不到阵列了!我很困惑,几个小时以来一直在试图找出这个问题。

计时器工作正常(即当 printCount: 不访问数组时,它很好),只有当我尝试访问那里的数组时它才会中断。下面对 printCount: 的常规调用有效,但是第一次计时器触发时,这两次对 printCount: 的调用之间可能会发生什么?

- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSTimer *timer = [[NSTimer scheduledTimerWithTimeInterval:0.001f
                                                       target:[viewController view]
                                                     selector:@selector(printCount)
                                                     userInfo:nil
                                                      repeats:YES]
                      retain];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    [[viewController view] printCount];
}

我一直在用调试器无休止地单步调试程序。到目前为止,数组都是正确的,即使其内容发生更改,其内存地址也是相同的。然而,之后它里面就只有一个对象了——它的 10 个元素被一个 UITouchData 实例替换。不知道它从哪里来。我的事件处理方法都没有尝试重新分配数组或更改它。有什么想法可能会出问题吗?我完全迷惑了。

EDIT: I stuck in a single call to manually retain the array right after initializing it, and that fixed it. Dur. Thanks.

I keep an NSMutableArray in a UIScrollView subclass, and I add to it instances of a UIImageView subclass as each is added as a subview. It needs to be a mutable array, as I plan to rearrange their order in the array eventually; this bug is occurring before I even try to do so. I've used NSLogs to show that the array has count 10 up until and including applicationDidBecomeActive: is called in my AppDelegate, and added a timer to fire every 0.001s in the run loop. The first time it fires, it can't find the array anymore! I'm very confused and have been trying to track this down for hours.

The timer's working properly (i.e. when printCount: doesn't access the array, it's fine), it's only when I try to access the array there that it breaks. The regular call to printCount: below works, but the first time the timer fires I What could possibly be happening between these two calls to printCount:?

- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSTimer *timer = [[NSTimer scheduledTimerWithTimeInterval:0.001f
                                                       target:[viewController view]
                                                     selector:@selector(printCount)
                                                     userInfo:nil
                                                      repeats:YES]
                      retain];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    [[viewController view] printCount];
}

I've been endlessly stepping through the program with the debugger. The array is correct right up through this point, and its memory address is the same even after its contents are changed. However, there is only object in it afterwards -- its 10 elements get replaced by a single UITouchData instance. No idea where it comes from. None of my event-handling methods attempt to reassign the array or change it. Any ideas what could be going wrong? I'm completely mystified.

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

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

发布评论

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

评论(5

萝莉病 2024-08-25 04:43:39

-retain数组了吗?

Did you -retain the array?

赠我空喜 2024-08-25 04:43:39

也许你的对象被释放了?如果保留它会发生什么。这有效还是你只是遇到内存泄漏?

Perhaps your object was released? What happens if you retain it. Does that work or you just get a memory leak?

我早已燃尽 2024-08-25 04:43:39

它的 10 个元素被替换为
单个 UITouchData 实例。不知道
它从哪里来。

它总是一个 UITouchData 实例吗?总是? ——因为这看起来像是内存问题的彻底暴露(如果不是每次都一样)。

its 10 elements get replaced by a
single UITouchData instance. No idea
where it comes from.

Is it always a UITouchData instance? Always? -- Cause that would seem like a dead give-away (if its not the SAME every-time) of a memory problem.

笑脸一如从前 2024-08-25 04:43:39

我在初始化数组后立即调用了保留数组,这解决了它。持续时间。

谢谢您的建议。正如此类问题通常会发生的那样,它非常简单,最后屈服并寻求帮助触发了解决方案,让我感到愚蠢。 :D

I stuck in a call to retain to array right after initializing it, and that fixed it. Dur.

Thank you for the suggestions. As usually happens with problems like these, it was very simple and finally giving in and asking for help triggers a solution and makes me feel stupid. :D

橘虞初梦 2024-08-25 04:43:39

视图是否初始化并释放数组?
您确定 NSView 在这期间没有被释放吗?
您可以在每次计时器调用 printCount 时检查 [viewController view] 的内存地址(== printCount 中的 self)是否相同。

Does the view initialize and release the array?
Are you sure that the NSView isn't released in between?
You could check if the memory address of [viewController view] (== self within printCount) is the same every time your timer calls printCount.

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