简体   繁体   中英

Why RemovingAllObjects, Assigning nil and then initializing NSMutableArray crashes iOS App?

I have to assign countries, states & cities getting in response from JSON, in an NSMutableArray, Which is initialized in Modal Class.

I will have to remove all objects in order to set new states and cities, doing that crashes with error

incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug

then in answer https://stackoverflow.com/a/12050676/5568169 came to know that assigning nil to mutableArray will work, and it worked, But now User can again select another country, So now allocating memory [myMutableArray alloc] init]] , gives me the same error i was getting in starting.

    -(void)fetchStates:(NSString*)idString {

        [registrationModalContactVC.allStateArray removeAllObjects];
        registrationModalContactVC.allStateArray = nil;
        [registrationModalContactVC.allStateDict removeAllObjects];
        registrationModalContactVC.allStateDict = nil;
        registrationModalContactVC.allStateArray = [NSMutableArray new];
        registrationModalContactVC.allStateDict = [NSMutableDictionary new];
}

Kindly help

You should not be dong this :

[myMutableArray alloc] init]

What you mean to do is :

myMutableArray = [[NSMutableArray alloc] init];

我认为您的mutablearray是Pickview的数据源,当您删除所有对象时,Pickview的状态如何。

Removing below code is working, but it may give the same error again

registrationModalContactVC.allStateArray = nil;
registrationModalContactVC.allStateDict = nil;
registrationModalContactVC.allStateArray = [NSMutableArray new];
registrationModalContactVC.allStateDict = [NSMutableDictionary new];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM