0

I have a very weird situation with NSMutableArray when trying to run a project on an ios 8.4 simulator. The project is non-arc and there's a lot of code, but the summary of the problem would be as follows:

  • i have an NSMutableArray *months instance variable
  • there is a method "populate" that populates this array
  • there is a method "monthCount" (just an alias of [months count])

Code for monthCount:

NSLog(@"months.count = %i", [months count]);
return [months count];

Here's where it all goes weird: in the "populate" method i'm creating dictionaries and adding them to the "months" array. After adding a new object i do the following:

NSLog(@"count after adding: %i", [months count]);
NSLog(@"monthCount returned: %i", [self monthCount]);

For some reason, only [months count] works from the "populate" method. The [self monthCount] always returns "1". Here's something stranger: The monthCount method sees the count of months correctly (that is the NSLog contained in this function outputs the correct count), but after i return and print out the value in the "populate" method - it becomes "1".

I've also used the debugger to test if "populate" and "monthCount" methods are actually accessing the same array and they do. The same code also runs perfectly on my ios7 device (so perhaps it's just a simulator thing, but i can't test on a device right now). Does anyone have any clue as to what's going on here?

10
  • seems like a cache issue in your simulator, try after resetting you simulator. Commented Aug 18, 2015 at 6:00
  • Resetting didn't help. Too bad i can't yet try it on the device itself. Commented Aug 18, 2015 at 6:04
  • 1
    I would suggest posting your code, you are leaving out an important detail and don't realize it. I assume months is a property on your class, how was it declared...strong or copy? Please post your populate and monthCount in their entirety. Commented Aug 18, 2015 at 6:05
  • YA please post mostly all your code, no matter if Question gets bigger in length. Commented Aug 18, 2015 at 6:15
  • 1
    If the few examples of code you've provided are any indication at all, your codebase is probably raddled with warnings (e.g., you should be seeing warnings for your formatters alone in your NSLog statements). Show your code. What other problems await our perusal? Commented Aug 18, 2015 at 6:27

2 Answers 2

1

Sorry for the trouble, everyone. It was a late night stupid-as-hell error. I've just checked to see if maybe monthCount return gets casted into something it shouldn't and found that the declaration of the method is as follows:

-(BOOL)monthCount;

I guess older compilers somehow managed to return integers with this declaration (although i don't understand how).

Sign up to request clarification or add additional context in comments.

Comments

0

Add an Exception breakpoint. Change the MutableArray into a normal NSArray. Run the code, you will get an error (exception) index out of bounds. Find the code where this occured from the exception breakpoint and voila. you will find the code where the count gets decreased!

[EDIT]: As per your current description, if your mutableArray is a property then make it threadsafe by adding "atomic" keyword. Some other thread might be accessing your array while the control is in that state. Making it atomic will ensure the array gets manipulated one thread at a time.

6 Comments

I'm not sure how this would help, since i won't be able to add anything to NSArray. Also, the value gets lost in the return statement. If i call [months count] again ... and again and again ... and even after the loop - the count is still OK. It would seem like a threading issue, the only problem is ... everything's on the main thread.
Is your array count coming low or more? and are you adding prior to that situation when you are getting the array count mismatch?
Here's how it's happening: a) adding object to array; b) immediadly checking count (OK); c) immediately checking count with "monthCount" (wrong count); d) checking the count again normally (OK)
you are checking the same instance, what is [self monthCount]?
Sorry for the trouble, it was a very stupid coding mistake. There's an answer below for details
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.