NSZombieEnabled prevents application crashes

So, I debugged myself like crazy using NSZombiesEnabled and NSZombies in tools. However, when running the application using zombies, it seems to solve my problem. When I run the application without NSZombiesEnabled or NSZombies in tools, it crashes. Any idea on how to deal with this?

So the problem is that I release something twice, but cannot find where I do it. Enabling NSZombieEnabled will not help, because the program works fine without telling me where I finished the release.

So, I think I know where it crashes, I have this globalArray Singleton class that I create:

extern NSString * const kClearDataSource; @interface AHImageDataSource : NSObject + (AHImageDataSource *)sharedDataSource; - (void) clearDataSource; - (void) addObject:(id) object; - (void) addObject:(id)object atIndex:(int) index; - (int) count; - (id) objectAtIndex:(int) index; @end NSString * const kClearDataSource = @"clearDataSource"; @interface AHImageDataSource() { NSMutableArray * imageDataSource_; } @property (nonatomic, retain) NSMutableArray * imageDataSource_; @end @implementation AHImageDataSource @synthesize imageDataSource_; + (AHImageDataSource *)sharedDataSource { static AHImageDataSource *_sharedClient = nil; static dispatch_once_t oncePredicate; dispatch_once(&oncePredicate, ^{ _sharedClient = [[self alloc] init]; }); return _sharedClient; } - (id)init { self = [super init]; if (!self) { return nil; } NSMutableArray * temp = [[NSMutableArray alloc] initWithCapacity:200]; self.imageDataSource_ = temp; [temp release]; return self; } -(void) clearDataSource { if ([self.imageDataSource_ count] > 0){ [self.imageDataSource_ removeAllObjects]; } } - (void) addObject:(id) object { [self.imageDataSource_ addObject:object]; } - (void) addObject:(id)object atIndex:(int) index { [self.imageDataSource_ insertObject:object atIndex:index]; } - (int) count { return [self.imageDataSource_ count]; } - (id) objectAtIndex:(int) index { if (index >= 0 && index < [self.imageDataSource_ count]){ return [self.imageDataSource_ objectAtIndex:index]; } return nil; } - (void) dealloc { [super dealloc]; [imageDataSource_ release]; } @end 

at one point in the code, I'm trying to delete all the objects in the array, and then add some things. When this happens, a failure has occurred.

This part of the code crashes a second time:

  NSArray *arr = [response valueForKey:@"data"]; if ([arr count] > 0){ [[AHImageDataSource sharedDataSource] clearDataSource]; } for (NSDictionary * data in arr){ AHInstagramImageData * imgData = [[AHInstagramImageData alloc] initWithData:data]; [[AHImageDataSource sharedDataSource] addObject:imgData]; [imgData release]; } 
+4
source share
3 answers

You should definitely not run [super dealloc] first in your -dealloc method. This should be the last.

+3
source

Go go Product β†’ Analysis. The displayed messages will give you a solution or idea.

+1
source

Your application crashes when an object that has been freed is sent a message. NSZombiesEnabled prevents your application from crashing because it holds onto all freed objects (and therefore all this leaks). It will print a message to the console when a message is sent to the freed object (which usually crashes your application). Something influenced the "message" bar sent to the freed object "foo" (or something like that). This does not actually pause your application.

When you have passed a point where you know that your application usually crashes, check the console log for a message similar to the one above.

+1
source

Source: https://habr.com/ru/post/1413621/


All Articles