1

I have a NSMutableArray which is used inside the ASIHTTPRequest process. After the data loading is done, the NSMutableArray stores the info. When I add the data as

[MyArray addObject];

I dont have any errors. However when I insert the data as

[MyArray insertObject:[UIImage imageWithData:data] atIndex:buttonTag];

I have malloc error or index out of range exception issues. I assume this as a thread safety malfunctioning. Any solution for this?

EDITED: in appdelegate.h

 @interface{
  NSMutableArray *imageArray;
 }

in appdelegate.m

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
imageArray = [[NSMutableArray alloc] init];
return YES;
}

In the AsyncImageView.h

@interface{
  AppDelegate *delegate
}

AsyncImageView.m

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {

  delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  [delegate.imageArray insertObject:[UIImage imageWithData:data] atIndex:buttonTag];

 }
6
  • 1
    Are you sure these inserts are happening on multiple threads? Do they really need to be? And are you sure that there are always at least buttonTag number of entries in the array when you insert each image? Commented Aug 8, 2012 at 2:15
  • Do you even have multiple threads? (If so, the solution is to learn how to code in a thread-safe fashion.) Commented Aug 8, 2012 at 2:26
  • Hello Firoze, I'm creating buttons based on the number of images available on the server. when I click on these buttons I need to see the picture respective of that button. So im trying to store the image into the NSMutableArray at the index of that buttontag. Am I doing it wrong? Commented Aug 8, 2012 at 2:28
  • have you alloc your "MyArray" ? Commented Aug 8, 2012 at 2:35
  • Hey @Siddharthan, one thing you may not realize is that NSArray's can't have nil values in them. So if your array has items at index 0,1,2 for example, you can't insert another element at 7 or 48 or something. Is that what you are trying to do? Or again, are you sure that you have at least 'buttonTag' items each time you do an insert? Commented Aug 8, 2012 at 2:37

2 Answers 2

1

It's hard to say without seeing your code, but I doubt this is a threading issue. When you call insertObject:atIndex: you have to be able to guarantee there are at least that many objects in the array already. Look at your code and see where you add objects, and make sure that every scenario leads to you adding enough objects that insertObject:atIndex won't fail.

Hopefully this next fact is obvious to you, but just in case, I'll point out that initWithCapacity: does not add any elements to the array. Many people assume it does, leading to the exact problem you described.

Based on your comment, the solution might be to pre-populate your array with a bunch of NSNull objects, or to use an NSDictionary instead of an array.

EDIT Here's a quick NSDictionary example:

Upon further review, you'll actually need an NSMutableDictionary since you're dynamically updating its contents. You can't use an integer as the key, so you have to "wrap" the integer in an NSNumber object. (Note that you can use any object as a key, not just NSNumbers.)

Store an object like this:

[myDictionary setObject:[UIImage imageWithData:data] forKey:[NSNumber numberWithInt:buttonTag]];

Access it later like this:

myImage = [myDictionary objectForKey:[NSNumber numberWithInt:buttonTag]];

Of course, much more information is available in Apple's documentation or with a quick search for "NSDictionary example."

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

2 Comments

Thanks a lot Husker. It would be really great if you can give me a rough idea on using NSDictionary. Havent really played with it before.
I edited my answer with a quick example. Hopefully that gets you started on the right path.
0

You must insert into the array within the bounds of the array. You are trying to insert past the end.

Apple Docs:

if an array contains two objects, its size is 2, so you can add objects at indices 0, 1, or 2. Index 3 is illegal and out of bounds; if you try to add an object at index 3 (when the size of the array is 2), NSMutableArray raises an exception.

Comments

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.