I spent a lot of time trying hundreds of things, and searching on the internet for my problem, but I didn't find anythig, so I hope you guys will be able to help me :).
So, I have an NSMutableDictionary that I populate with NSArrays when I parse an XML document. My problem is, I want to empty it before I release it because it appears to cause leaks !
Best solution, to my eyes, is : [myDict removeObjectsForKeys : [myDict allKeys]] but the app crashes and I get the following message in the console :
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
I really can't figure why... any ideas ?
EDIT
Important point I forgot (I guess...), is that the dictionary I'm calling is in my application Delegate, with appDelegate = [[UIApplication sharedApplication] delegate];. (appDelegate defined in my .h with myAppDelegate class type).
Then, I initialize it like this : appDelegate.myDict = [[NSMutableDictionary alloc] init];
Is it wrong ?
What's weird then, is that when I do :
[appDelegate.myDict release];
NSLog( #"%#", appDelegate.myDict);
I get the dictionary filled...
EDIT 2
I just figured out that all my appDelegate variables are not released when I call [appDelegate.myVariable release] ... Wtf am I doing wrong ? This is driving me crazy ><
When you release a dictionary or array it releases all the elements inside it. If you're getting leaks they're not due to a failure to remove the objects.
Of course, in your case part of your problem appears to be that you don't know where the error is occurring. -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0) indicates that you were executing objectAtIndex on an (empty) NSArray, and that doesn't happen (directly) if you do [myDict removeObjectsForKeys : [myDict allKeys]] on an NSDictionary. So you need to figure out where that error is really coming from.
What about this:
// myDict should be NSMutableDictionary
[myDict removeAllObjects];
This should hep you figure out which key (or keys) are pointing to the misbehaving arrays.
for ( NSString *key in [myDict allKeys]) {
NSLog(#" key - %#", key);
[myDict removeObjectForKey: key];
}
NSLog(#" the problem is not with the dictionary...");
You don't need to empty an NSMutableDictionary before releasing. Any objects retained by the dictionary will sent a release message when the dictionary is dealloced.
It sounds as though your arrays are being retained elsewhere, causing the leak. Are you sure you've implemented dealloc methods correctly for all your objects (view controllers, model objects, etc)?
Related
I'm new to ios programming.
I'm trying to get an access to variables in a dictionary with modern objective-c syntax like the code below.
But, none of these works.
The first line and second line fails when running ios simulator.
The third one cause an error when I press a button on the screen.
//NSDictionary *myData = #{#"name":#(nameField.text), #"birthdate":#(birthField.text), #"sex":#(selectedSex)};
//NSDictionary *myData = #{#"name":#nameField.text, #"birthdate":#birthField.text, #"sex":#selectedSex};
NSDictionary *myData = #{#"name":nameField.text, #"birthdate":birthField.text, #"sex":selectedSex};
This error message appears when I try the third line above.
2013-10-02 09:12:56.160 LifeTimer[18603:11303] *** Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary
initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
How can I fix this problem?
I think I'm misunderstanding something very basic.
Unfortunately you can not define dictionaries with nils by using literals syntax. The same was for old ways of defining NSDictionary through methods like initWithObjectsAndKeys:, initWithObjects:forKeys:count: and others. Nils are not allowed till you will not wrap them into an NSNull that confirms NSCopying protocol which is mandatory for both keys and values of NSDictionary.
[[myDict objectForKey:#"id"] intValue] assuming id is an int if the value is a string you don't even need the last method: [myDict objectForKey:#"name"]
I'm getting the following error when removing from my NSMutableArray
-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x1cdced10
2011-07-13 00:33:14.333 MassText[1726:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x1cdced10'
However right before I remove, I print out the array and the index. Neither are nil and I have no reason to believe why this error would be happening. Any ideas?
I had this problem. Mine was that I accidentally used type casting like this.
NSMutablearray * myarray = [[NSMutableArray alloc] init];
myarray =(NSMutableArray*) [mydictionary allkeys];
This will work for some time.. but if you are in a tight and large loop this tend to fail.
I changed my code to
NSMutableArray * myarray= [[NSMutablearray alloc] initWithArray:[mydictionary allKeys]];
The object is an NSArray, not an NSMutableArray.
You are calling removeObjectAtIndex on a NSArray instance. We can see clearly by your crash log.
At this point, four smart people (not including myself) have pointed out that you're sending -removeObjectAtIndex: to an object that thinks it's an immutable array. This would be a good time to start wondering why the array is immutable when you previously thought it was mutable. If you post some code that shows how the array is created, someone here will probably be able to show you what's going wrong.
One way that you can end up with an immutable array when you thought you had a mutable one is to copy a mutable array. For example, you might have a property:
#property (copy) NSMutableArray *myArray;
Perhaps you then create a mutable array, add some objects, and assign it to your property:
NSMutableArray *tempArray = [NSMutableArray array];
[tempArray addObject:#"You say goodbye"];
[tempArray addObject:#"I say hello"];
self.myArray = tempArray;
Now, does tempArray point to a mutable array or an immutable array? I haven't tested recently, but I'm pretty sure that you get an immutable array. You definitely get an immutableArray if you say:
NSMutableArray *foo = [tempArray copy];
So, start looking for places in your code where your array pointer is reassigned. After all, if your pointer really did point to a mutable array, it'd be awfully hard to explain the exception that you're getting.
The error says that you are trying to call the removeObjectAtIndex selector on an NSArray, which won't respond to that selector.
Make sure the array is really an NSMutableArray, not an NSArray.
I had the same problem, and it was because of the use of the copy method. I made one on my own returning a NSMutableArray* and it worked.
NSString *anError = nil;
id plist;
plist = [NSPropertyListSerialization propertyListFromData:rawCourseArray mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&anError];
if (anError != nil){
[anError release];
}
The above code causes a memory leak every time I call it. I am releasing the error but still there is a leak. I haven't seen any resolution to this issue. I posted this already and most respond that this is not a leak. But see here in the leak performance tool:
I need this fixed because eventually my app crashes. Any ideas? Many thanks
I had the same problem. Used propertyListWithData:options:format:error: with same result. Used NSDictionary initWithContentsOfFile with same result:
Leaked Object # Address Size Responsible Library Responsible Frame
NSCFNumber,19 < multiple > 304 Bytes MediaToolbox FigRemote_CreatePropertyListFromBinaryPListData
FWIW - I only have dictionaries and arrays and strings in the pList, no numbers.
The issue is not the error object (or string in your method). The issue is not plist above, since it should be autorelease. In the end, I gave up and reimplemented using JSON and:
http://stig.github.com/json-framework/api/index.html
==> no more leak.
Best,
Fred
I've had the same problem before.
Basically this is because your error variable got released without initiating it. So what you can do is try to init your NSString *error to an empty string before releasing it. Or not releasing it at all.
I have the following piece of code and when I use Instruments/Object Allocations, it tells me that there is a leak there (which goes down to sqlite3MemMalloc). Is there something that I should release?
if (![managedObjectContext save:&error]) {
NSLog(#"Error while saving.");
}
The save works well and doesn't trigger an error.
The leak is most likely in one of the managed objects being saved and it just shows here. If you look at the stack in Instruments you can probably see the leaking object. Since it only shows up at save, it's probably in validation code.
Do you have any subclasses of your NSManagedObject instances?
When you set a value into your NSManagedObject instances do you then release your ownership of them? For example if you were do to the following code:
NSString *someString = [[NSString alloc] initWithString:#"Blah"];
[myManagedObject setValue:someString forKey:#"stringValue"];
You would be leaking memory because you are still owning that NSString. That is what TechZen is referring to above.
I'm relatively well versed in CoreData and have been using it for several years with little or no difficulty. All of a sudden I'm now dumbfounded by an error. For the life of me, I can't figure out why
insertNewObjectForEntityForName:inManagedObjectContext:
is all of a sudden returning some sort of strange instance of NSNumber. GDB says the returned object is of the correct custom subclass of NSManagedObject, but when I go to print a description of the NSManagedObject itself, I get the following error:
*** -[NSCFNumber objectID]: unrecognized selector sent to instance 0x3f26f50
What's even stranger, is that I'm able to set some relationships and attributes using setValue:forKey: and all is good. But when I try to set once specific relationship, I get this error:
*** -[NSCFNumber entity]: unrecognized selector sent to instance 0x3f26f50
Has anyone ever encountered anything like this before? I've tried clean all targets, restarting everything, even changing the model to the relationship in question is a to-one instead of a to-many. Nothing makes any difference.
I've encountered the "unrecognized selector sent to instance 0x..." error before in a situation where the object I expect to be at the memory address "pointer" has been replaced by something else. Take this situation:
NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];
NSString *someString = [NSString stringWithString:#"some string"]; // autoreleased object returned
[pool drain];
[pool release];
/*
some other code executes
*/
// since the string behind the someString variable has been autoreleased at this point, the memory that someString points to may be occupied by some other data type. the following may through an EXC_BAD_ACCESS error, or it may try and execute the selector on whatever is occupying that memory space
int stringLength = [someString length];
This example is painfully straightforward and my semantics may be a bit off here, but could it be possible that this is what is happening in your case in a more convoluted way? Maybe try:
[[NSEntityDescription insertNewObjectForEntityForName:#"entityName" inManagedObjectContext:managedObjectContext] retain]
and see what happens?