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

211 views Asked by At

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

3

There are 3 answers

3
Ezatu. On

i think your mutablearray is a datasource of a pickview , when you remove all objects, how is your pickview's status.

0
ios_Dev On

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];
0
Swapnil Luktuke On

You should not be dong this :

[myMutableArray alloc] init]

What you mean to do is :

myMutableArray = [[NSMutableArray alloc] init];