0

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

Community
  • 1
  • 1
ios_Dev
  • 95
  • 1
  • 10
  • does use `removeAll` or `array = [[NSMutableArray alloc] init]` alone also cause crash? – Tj3n Dec 28 '16 at 07:25
  • where are you using this array – Prashant Tukadiya Dec 28 '16 at 07:25
  • sir i am using this array in a picker view, Actually there are three picker view, one for country and then for state and then for city. – ios_Dev Dec 28 '16 at 07:27
  • Add part of your code that crashes, to the question. – Swapnil Luktuke Dec 28 '16 at 07:28
  • So on selecting different country another api would be called and i will have to send the updated array to the picker view – ios_Dev Dec 28 '16 at 07:28
  • The problem is more likely related to `registrationModalContactVC`, make sure it is not nil and `allStateArray` is not nil also – Tj3n Dec 28 '16 at 07:34
  • @Tj3n yes sir, i am just using the sharedInstance of registrationModalContactVC, So it can not be nil, it is working fine with all other controllers, Okay one thing i want to mention is that in registrationModal.m file `- (id)init { self = [super init]; if ( self ) { // here i have initialized all the array } return self; }`, Does it make some issue – ios_Dev Dec 28 '16 at 08:18
  • then it doesnt look any problem to me, try remove the `= nil;` line and use either `removeAll` or `array = [[NSMutableArray alloc] init]` and see if it still crash or not – Tj3n Dec 28 '16 at 08:29
  • okay one question, if `array = [[NSMutableArray alloc] init]` is already done in modal class, technically what happens if i do it again in my controller class – ios_Dev Dec 28 '16 at 08:31
  • [registrationModalContactVC.allStateArray removeAllObjects]; registrationModalContactVC.allStateArray = nil; I don’t know why you are using two method of deallocation (removeallobject and nil) you can only use nil and when you are sending data in array to another view controller you have to initialise your delegate first. – neha mishra Dec 28 '16 at 08:51
  • @nehamishra Kindly check the link i gave in Question. – ios_Dev Dec 28 '16 at 08:53
  • [http://stackoverflow.com/a/27655503/5568169] – ios_Dev Dec 28 '16 at 08:54
  • As per my concern you should only use removallobject then initialise value in array like this- [registrationModalContactVC.allStateArray removeAllObjects]; registrationModalContactVC.allStateArray = newArray; (here new array is an array with data that has been initialised with data previously) – neha mishra Dec 28 '16 at 09:08
  • @nehamishra Could you explain _data that has been initialised with data previously_ – ios_Dev Dec 28 '16 at 09:15
  • Error was **to keep save from " malloc: *** error for object 0x14a3fa00: pointer being freed was not allocated** but on removing `registrationModalContactVC.allStateArray = nil;` and `registrationModalContactVC.allStateArray = [NSMutableArray new];`, its working fine as if now. But the same was giving error shown above. – ios_Dev Dec 28 '16 at 09:23
  • 1
    What I can understand your question You want to send array data from your helper class to a view controller before passing the data you want to clear object if array has already contained. Now in my answer, I am storing data from service helper class to an array (newArray) which is previously allocated on init method or viewdidload method. – neha mishra Dec 28 '16 at 09:23
  • You should not make the array become nil, just simply recreate it again with `array = [[NSMutableArray alloc] init]`, nothing will happen – Tj3n Dec 29 '16 at 07:25
  • @Tj3n yes i removed and it worked that but kindly answer including the error i said in link, someone else may be benefitted – ios_Dev Dec 29 '16 at 07:28
  • Probably because you are trying to assign new value when it's releasing ram for the array, thus create the error, just guessing based on the error – Tj3n Dec 29 '16 at 07:49

3 Answers3

0

You should not be dong this :

[myMutableArray alloc] init]

What you mean to do is :

myMutableArray = [[NSMutableArray alloc] init];
Swapnil Luktuke
  • 10,385
  • 2
  • 35
  • 58
0

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

Ezatu.
  • 9
  • 1
  • No, i am just passing the array as a paramter to the pickerView method and it will show the items in array. I think the problem may be in parsing the data from JSON, i am retrying it – ios_Dev Dec 28 '16 at 08:50
  • some times the the state array is just showing blank, i mean the it is all about where i initialize array – ios_Dev Dec 28 '16 at 08:51
  • you can nslog(@"%@", registrationModalContactVC.allStateArray); – Ezatu. Dec 28 '16 at 08:59
0

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];
ios_Dev
  • 95
  • 1
  • 10