标签: dealloc

什么时候发布的对象最终被破坏

当你在Objective-C中释放一个对象时(假设它的释放计数为1),它的释放计数减少到0并调用dealloc方法.对象是在那里被破坏然后在[super dealloc]之后被破坏了,还是被添加到池中并在池被耗尽时被销毁?

我假设释放的对象在dealloc结束时被销毁(当调用[super dealloc]时)我知道自动释放变量被添加到池中,只是想确定正常释放对象会发生什么.

干杯 - 加里 -

memory pool objective-c dealloc

3
推荐指数
2
解决办法
1794
查看次数

这两种dealloc方法之间有什么区别吗?

所以我重写了dealloc方法,因为该对象是一个由另一个对象组成的复合对象.

我原来有这种dealloc方法:

-(id) dealloc; // Override to release the Rectangle object’s memory 
{
    [rect release];
    [super dealloc];
    return self;
}
Run Code Online (Sandbox Code Playgroud)

看完这本书后,我看到了另一个答案:

{
   [rect release];
   return [super dealloc];
}
Run Code Online (Sandbox Code Playgroud)

只是想知道两者是否同样有效.

谢谢,

缺口

memory-management objective-c dealloc

3
推荐指数
1
解决办法
338
查看次数

在自定义UIViewController上调用[super dealloc]时的iPhone EXC_BAD_ACCESS

我很茫然!这是仅在特定条件下发生的那些讨厌的错误之一,但我无法直接链接条件和结果.

我的应用程序有一个分页的UIScrollView,其中每个页面的视图来自一个MyViewController,一个UITableViewController的子类.为了最小化内存使用量,我卸载那些当前不可见的控制器.这是我的"清洁"方法:

- (void) cleanViewControllers:(BOOL)all {

    if (all) { 
    // called if some major changes occurred and ALL controllers need to be cleared

        for (NSInteger i = 0; i < [viewControllers count]; i++)
            [viewControllers replaceObjectAtIndex:i withObject:[NSNull null]];
    }
    else if ([viewControllers count] > 2) {
    // called if only the nearest, no longer visible controller need to be cleared
        NSInteger i = pageControl.currentPage - 2;
        if (i > -1) [viewControllers replaceObjectAtIndex:i withObject:[NSNull null]];
        i = pageControl.currentPage + 2;
        if (i …
Run Code Online (Sandbox Code Playgroud)

iphone dealloc

3
推荐指数
1
解决办法
2755
查看次数

如何确保在应用程序终止时调用dealloc代码?

根据NSObject的文档:

重要说明:请注意,当应用程序终止时,可能不会向对象发送dealloc消息,因为进程的内存在退出时会自动清除---仅仅允许操作系统清理资源比调用所有内存管理更有效方法.

这很好,但是如果我的对象需要在dealloc上做一些事情,比如将状态保存到磁盘或记录某些东西?如何确保调用代码?

cocoa destructor objective-c dealloc

3
推荐指数
2
解决办法
569
查看次数

删除Dealloc中的Observer

解决的 -事实证明,传递nilremoveObserver:forKeyPath:失败,但只有在手动内存管理.它在垃圾收集模式下工作正常.Apple文档并没有说它需要非零值,所以我假设它是一个bug.

我有一个对象,它将自己添加为自身的观察者,通过[self addObserver:self forKeyPath:等等.在我的-dealloc方法中(注意我使用的是保留计数而不是垃圾收集器)我调用[self removeObserver:self forKeyPath:nil];哪个应该有效.但是,我收到以下错误:

Cannot remove an observer <Snapshot 0x10047a6d0> for the key path "(null)" from <Snapshot 0x10047a6d0> because it is not registered as an observer.
Run Code Online (Sandbox Code Playgroud)

现在,如果我删除该行,因此它不会自行删除,我收到此消息:

An instance 0x100193580 of class Snapshot was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. …
Run Code Online (Sandbox Code Playgroud)

cocoa objective-c key-value-observing key-value-coding dealloc

3
推荐指数
1
解决办法
3791
查看次数

块和异步回调,释放对象-需要将块设为零

这里有一个类似的问题,并不能完全解释我想要什么:Objective C Blocks as Async-callbacks&BAD ACCESS

我有一个视图控制器,它使用异步回调调用服务。回调使用一个块来完成,该块引用视图控制器上的变量以填充它们。

看起来像这样:

- (void) loadData {
    __block MyViewController *me = self;
    [self.service executeWithCompletion:^(NSArray *result, NSError *error) {
        if (!error) {
            me.data = result;  
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我取消分配视图控制器,则回调将严重访问“ me”。

使“ me”为NULL的最简单方法是什么?如果我将其作为iVar,那么它会带回循环引用...我认为呢?

我想我缺少明显的东西。

谢谢

asynchronous block objective-c dealloc nszombie

3
推荐指数
1
解决办法
4269
查看次数

当调用navigationController popViewControllerAnimated时,不会调用viewController的dealloc

我有一个MyViewController,它基于UIViewController,我使用它像下面的代码:

MyViewController *nextViewController = [[MyViewController alloc] init]; [self.navigationController pushViewController:nextViewController animated:YES]; [nextViewController release];

在具有用户事件的MyViewController中,具有以下代码:

[self.navigationController popViewControllerAnimated:YES];

现在,我发现,MyViewController的dealloc不会被调用,但是,当我将App切换到后台时,例如,传递home按钮,dealloc方法已被调用!这个大问题!当用户转到MyViewController并且一次又一次地返回时,会有很多MyViewController不会被释放,而且只有当App转到后台时才会释放大量内存.

所以,有人可以帮我解决这个问题,谢谢!

memory-leaks dealloc navigationcontroller ios popviewcontrolleranimated

3
推荐指数
1
解决办法
3304
查看次数

从iOS 6开始,目标C选择器是否仍在使用中?

我是新的iOS开发人员.我还需要dealloc在iOS 6中发布我的属性吗?

如果没有,我的保留财产如何发布?它是自动完成的吗?

需要一些指导.对不起,如果这是一个愚蠢的问题..

例如,当我这样做时:

- (void)dealloc
{
    [super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud)

我收到类似的消息:

ARC forbids explicit message send of 'dealloc'
Run Code Online (Sandbox Code Playgroud)

llvm dealloc ios automatic-ref-counting ios6

3
推荐指数
1
解决办法
2060
查看次数

gestureRecognizer shouldReceiveTouch持久存在于解除分配的视图中导致崩溃

我有一个相当简单的UITableView,它在栈上推送一个新视图.新视图有一个这样初始化的gestureRecognizer

@synthesize swipeGestureLeft;


    - (void)viewDidLoad
{
        swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(toggleViewLeft)];
        swipeGestureLeft.numberOfTouchesRequired = 1;
        swipeGestureLeft.delegate=self;
        swipeGestureLeft.direction = (UISwipeGestureRecognizerDirectionLeft);
        [self.view addGestureRecognizer:swipeGestureLeft];
}
Run Code Online (Sandbox Code Playgroud)

我也称为委托方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    if (viewShown==1) {
        return NO;
    }
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

在我所拥有的dealloc方法中

- (void)dealloc {
    NSLog(@"I AM IN DEALLOC");


    swipeGestureLeft.delegate=nil;
    [self.view removeGestureRecognizer:swipeGestureLeft];
    swipeGestureLeft=nil;



}
Run Code Online (Sandbox Code Playgroud)

在我的.h文件中

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>
Run Code Online (Sandbox Code Playgroud)

现在,当我回到我的表视图时,视图被取消分配(我可以看到因为NSLog触发)现在当我尝试向下滑动我的表视图时,应用程序崩溃:

[MyViewController gestureRecognizer:shouldReceiveTouch:]: message sent to deallocated instance 
Run Code Online (Sandbox Code Playgroud)

如何确保在取消分配视图后不调用委托方法.

uitableview dealloc uigesturerecognizer ios

3
推荐指数
1
解决办法
2604
查看次数

当控制器被释放时,键值观察者仍然在其中注册

我在代码中添加了一个观察者,然后在dealloc和viewWillDisappear中将其删除但是我仍然收到错误说明

***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'类的MyController2的实例0x167e5980被释放,而键值观察者仍然注册了它.

Current observation info: <NSKeyValueObservationInfo 0x16719f90> (
<NSKeyValueObservance 0x16719fb0: Observer: 0x167e5980, Key path: dataContainer.report, Options: <New: YES, Old: YES, Prior: NO> Context: 0x0, Property: 0x1677df30>
)'
Run Code Online (Sandbox Code Playgroud)

我创建了一个控制器,MyController并从中派生出一个新的控制器MyController2.现在我加入了KVO MyController2.

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addObserver:self forKeyPath:@"dataContainer.report" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
Run Code Online (Sandbox Code Playgroud)

然后在observeValueForKeyPath中: -

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {

    id oldC = [change objectForKey:NSKeyValueChangeOldKey];
    id newC = [change objectForKey:NSKeyValueChangeNewKey];

    if([keyPath isEqualToString:@"dataContainer.report"]) {
        if (oldC != newC) {
            //Remove Observer …
Run Code Online (Sandbox Code Playgroud)

memory objective-c key-value-observing dealloc ios

3
推荐指数
1
解决办法
6334
查看次数