NSDictionary functionality question - iphone

So I am trying to save arrays into an NSDictionary on the fly. Let me show you the code and explain what is going on.
for (int x= 0; x <[appDelegate.people count]; x++) {
Person *aPerson = [[Person alloc] init];
aPerson = [appDelegate.people objectAtIndex:x];
if ([appDelegate.groupedBusiness objectForKey:aPerson.business_name] == nil) {
NSMutableArray *newBusiness = [[NSMutableArray alloc] init];
//if the business does not exist in the dict, add the person to the business and add it to dict.
[newBusiness addObject:aPerson];
[appDelegate.groupedBusiness setObject:newBusiness forKey:aPerson.business_name];
[newBusiness release];
newBusiness = nil;
//NSLog(#"%#", appDelegate.groupedBusiness);
} else {
NSMutableArray *existingBusiness= [appDelegate.groupedBusiness objectForKey:aPerson.business_name];
[existingBusiness addObject:aPerson];
//THIS IS THE LINE I AM NOT SURE ABOUT!!!
[appDelegate.groupedBusiness setObject:existingBusiness forKey:aPerson.business_name];
[existingBusiness release];
existingBusiness = nil;
//NSLog(#"%#", appDelegate.groupedBusiness);
}
}
Alright, so the appDelegate has an array of "People" that has a whole bunch of attributes about a person. I am trying to set up a dictionary to sort them by their business names. I am doing this by creating an array and saving it in the dictionary with the business_name as the key. Each iteration of the loop I check to see if the key exists, if it does, pull out the existing array, add the person you are checking, and resave it to the dictionary. However, this does not appear to be happening. Is there some exotic behavior in the NSDictionary class that would prevent that? I have poured over the class web page and can't find anything. Sorry if this is a noobie question, I am still trying to understand the objective-c classes. Thanks!

Why do you release existingBusiness? You are not creating an object, just taking the pointer from an array. When you invoke release, retainCount became 0 and object deallocs.
Just remove the following two lines:
[existingBusiness release];
existingBusiness = nil;
and everything should work fine.

You're way overcomplicating this, not to mention leaking a couple things.
for (Person *aPerson in appDelegate.people) {
NSMutableArray *business = [appDelegate.groupedBusiness objectForKey:aPerson.business_name];
if (!business) {
business = [NSMutableArray array];
[appDelegate.groupedBusiness setObject:business forKey:aPerson.business_name];
}
[business addObject:aPerson];
}

Not an answer, but some coding style issues.
Use fast iteration if you don't need the index:
for (Person *aPerson in appDelegate.people) {
Use convenience constructors; it makes your code more readable (remember to remove the "release" at the end):
NSMutableArray *newBusiness = [NSMutableArray arrayWithObject:aPerson];
Avoid duplicate logic where possible:
NSMutableArray * business = [appDelegate.groupedBusiness objectForKey:aPerson.business_name;
if (!business) {
business = [NSMutableArray array];
}
[business addObject:aPerson];
[appDelegate.groupedBusiness setObject:business forKey:aPerson.business_name];
The "setObject:existingBusiness" call does changes nothing apart from wasting CPU cycles, but in the case above, it makes the code somewhat more readable.

[appDelegate.groupedBusiness setObject:existingBusiness forKey:aPerson.business_name];
[existingBusiness release];
existingBusiness = nil;
This should all be removed. existingBusiness is already in the dict, and it's a mutable object - when you're adding a person to it, this will be reflected in the dictionary as well as it's the same object you're dealing with. Apart from that you have a couple of memory leaks as Daniel points out.

Related

Memory Leak in the below simple code

I have a piece of code & I want to understand the memory leak possibility in this. I have gone through the Apple documentation here!
The code here process a array from parameter & stores the dictionary into a temporary array.
+ (void)setLinkedProfiles:(NSArray *)profileData {
NSMutableArray *returnArray = [[NSMutableArray alloc]init];
if([profileData count] > 0) {
for(NSDictionary *dict in profileData) {
NSDictionary *tempDict = #{
#"verifiedEmail" : ([[dict objectForKey:#"verifiedEmail"] isKindOfClass:[NSNull class]]) ? #"": [dict objectForKey:#"verifiedEmail"],
#"identifier" : [dict objectForKey:#"identifier"],
};
[returnArray addObject:tempDict];
}
}
[SharedApp sharedUserData].linkedProfiles = returnArray;
}
I suspect that the line of code SharedApp sharedUserData].linkedProfiles = returnArray might create a memory leak.
I want to understand why & in which scenario?
Can some one help me in understanding this ?
Thank you for reading & understanding my problem.
To clear things up:
Your line
NSMutableArray *returnArray = [[NSMutableArray alloc]init];
gives you ownership of that array. You are responsible to release it when you're done with it.
So adding
[returnArray release];
as the last line would work. You could use autorelease instead, or even work with an autoreleased array from the beginning, i.e.
NSMutableArray *returnArray = [NSMutableArray array];
linkedProfiles should be a strong reference (i.e. strong, or copy).
Although I strongly suggest to switch to ARC, understanding of the underlying memory management might come handy.

Copy an Array - Bad Design?

I learn objective-C from Stanford iTunes and i wonder how i should copy a NSMutableArray to NSArray without initialization. I mean:
Is this is correct? with "lazy initialization".
-(void)copyAnArray:(NSMutableArray*)listOfElements {
if(privateElementsLists == nil)
privateElementsLists = [[NSArray alloc] initWithArray:listOfElements copyItems:YES];
else
privateElementsLists = listOfElements;
}
is this a bad design?
I want to addobjects to mutable array in one class, and then when i'm finish copy entire NSMutableArray to NSArray.
And another question: Why i have to use copyItems:YES when I use initWithArray? And what's deep copy?
You can copy a mutable array to a new array with initWithArray: or this way:
privateElementsLists = [NSArray arrayWithArray:listOfElements];
then you are creating a new array where each of its elements is the same object that figures in the original array. If you write:
privateElementsLists = [NSArray arrayWithArray:listOfElements copyItems:YES];
then the new array have, for each element, a copy of the element in original array. They are not the same object but a copy. Of course, that objects have to be able to respond to copy.
You can even do this:
privateElementsLists = (NSArray*) listOfElements ;
Then the array is exactly the same as the original one. No new array here. But as you have casted it with NSArray pointer class, you can use it as if it is a NSArray instead of a NSMutableArray. As you know, every NSMutableArray is a NSArray (inherited class).
As Joseph DeCarlo stated, you don't need to copy NSMutableArray to NSArray if the only thing you do is to create the array in one place to use it somewhere else. For example this statement is valid:
NSArray* newArray = [NSMutableArray array];
Or in the code:
-(NSArray*)returnAnArray
{
NSMutableArray* editableArray = [NSMutableArray array];
[editableArray addObject:[[NSObject alloc] init]]; //an exemplary object added to the array
return editableArray;
}
That said, however, in some specific cases casting NSMutableArray to NSArray may not be safe, e.g. if the original array was stored in an instance variable. Adding or removing objects to/from that array may cause a crash if the returned array is enumerated at the same time. For example:
-(void)createArray
{
self->editableArray = [NSMutableArray array]; // instance variable: NSMutableArray* editableArray
}
-(void)addObjectToArray
{
[self->editableArray addObject:[[NSObject alloc] init]];
}
-(NSArray*)getArray
{
return self->editableArray;
}
-(void)enumerateArray
{
for(NSObject obj in [self getArray])
{
// do something with obj
}
}
If addObjectToArray is called at the same time as enumerateArray (e.g. from a background thread) the application will crash because the underlying array is changing while it is being enumerated. It doesn't matter that it was returned as NSArray*. In a case like this you would need to either add #synchronized to synchronize access to the same object by multiple threads, or copy the entire array with arrayWithArray: as suggested. Note, however, that the documentation doesn't say if arrayWithArray: is thread safe so I would add #synchronized around the call to arrayWithArray: anyway.

NSMutableDictionary EXC_BAD_ACCESS

I know there are many questions about this topic but non of them work for me because mine is a little weird.
First of all I create a static singleton class. and declare a variable of NSMutableDictionary
static NSMutableDictionary* mydic
#implementation mySingleton
-(mySingleton*)getInstance
{
static mySingleton *sharedInstance;
#synchronized(self)
{
if(!sharedInstance)
{
sharedInstance = [[mySingleton alloc] init];
mydic = [[NSMutableDictionary alloc] initWithCapacity:1];
}
return sharedInstance;
}
}
-(NSMutableDictionary*)getDictionary
{
return myDic;
}
then I call this NSMutableDictionary from another class like the below.
NSMutableDictionary* singletonDictionary = [[mySingleton getInstance] getDictionary];
MyOtherClass* myclass = [singletonDictionary objectForKey:key];// Key is NSString
// Here I can see whole the values I added to myClass for that key
NSArray *checkKey = [singletonDictionary allKeys];
for(int i = 0; i < [singletonDictionary count]; i++)
{
NSLog(#"%#",[checkKey objectAtIndex:i]);// here I can see my key is there
}
[singletonDictionary removeObjectForKey:key];// here it crashes EXC_BAD_ACCESS
I am gonna get crazy about this problem. If someone has an idea please share it with me.
EDIT :
MyOtherClass * myinstance = [[MyOtherClass alloc] init];
// Fill up the instance with the desired variable here
// Forexample
// myinstance.name = [NSString stringWithFormat:#"myInstanceName"];
.
.
.
[[[mySingleton getInstance] getDictionary] setObject:myinstance forKey:key]// key is an NSString*
[myinstance release];
Thanks for help.
Omer Faruk KURT
So many problems, where to start...?
At least, it seems your getInstance method is not returning anything; it should probably return mySingleton. This could explain your EXEC_BAD_ACCESS, as singletonDictionary is probably nil as things stand.
Your singleton instantiation is wrong too - you need to check if the singleton has already been created, and return it if it has. Otherwise you can reallocate singletons, which is absurd.
Static references are poor design here, better to subclass and declare members in the header file.
This might fix your problems, but you're clearly jumping in at the deep end and you're going to encounter more trouble. I think you need to find good examples of code in texts or online and study those. If you do that pretty soon you'll learn the ropes.
The NSMutableDictionary retains objects added to the dictionary. When the objects are removed they are released. As a result, removing an object from the dictionary accesses the object. If you have previously released the object and it is dealloc'ed, then this can cause an exception to be raised. If you examine the state of the object prior to removing from the dictionary you will likely see that it has already been released and dealloced.

iPhone - Problems with releasing memory - memory leaks

I'm having the code as below.
- (void)viewDidLoad
{
NSArray* myarr = [self createArray];
for (NSString* str in myarr)
{
NSLog(#"%#",str);
}
[myarr release];
}
-(NSArray*)createArray
{
NSArray* arr1 = [[NSArray alloc] initWithObjects:#"APPLE",#"MAC",#"IPHONE",nil];
return arr1;
}
When I "Build & Analyze", its showing two leaks. One at [myarr release] saying, incorrect decrement of the reference count of an object that is owned at this point. and Other at return arr1, saying, Potential leak of an object allocated on line 152 and stored into arr1.
From my above code, the method "createArray" is returning a pointer and I'm releasing it as well. Is my way of coding right or wrong?
From my above code, the method "createArray" is returning a pointer and I'm releasing it as well. Is my way of coding right or wrong?
that depends on how you look at it.
1) the ref counting looks ok
2) the static analyzer flags objc methods based on names, in some cases. so the issue will likely vanish if you rename createArray to newArray, or something named new*. so it expects a convention (the ones used by Apple) to be followed.
therefore, it's the message that's bit shallow, it doesn't really analyze the program, but bases its findings/results on convention -- and not an actual evident issue which a human can read.
If you're just using the array in your viewDidLoad method, then you don't need to alloc an array in there at all. You can just use an autoreleased array returned as 7KV7 suggested. You can return an autoreleased array in your -(void)createArray as well without alloc'ing an object. Here is an example.
- (void)viewDidLoad
{
NSArray* myarr = [self createArray];
for (NSString* str in myarr)
{
NSLog(#"%#",str);
}
}
-(NSArray*)createArray
{
return [NSArray arrayWithObjects:#"APPLE",#"MAC",#"IPHONE",nil];
}
If you don't have to alloc an object to use it, it makes for less, and cleaner code, IMO.
Try this
- (void)viewDidLoad
{
NSArray* myarr = [[NSArray alloc] initWithArray:[self createArray]];
for (NSString* str in myarr)
{
NSLog(#"%#",str);
}
[myarr release];
}
-(NSArray*)createArray
{
NSArray* arr1 = [[NSArray alloc] initWithObjects:#"APPLE",#"MAC",#"IPHONE",nil];
return [arr1 auotrelease];
}
The problem with your code is that
You do not allocate myarr using alloc or new so you do not take ownership of the object. Hence the issue in release.
You allocate arr1 so you take ownership of the object and you return arr1. Hence you do not release it. That is the reason for the leak.

Can't Figure Out How To Fix Memory Leaks on iPhone

I was running Leaks tool and discovered a massive leak in my Dictionary mutableDeepCopy but I can't figure out what's wrong with the code. Any suggestions?
#interface RootViewController : UIViewController{
NSDictionary *immutableDictionary;
NSMutableDictionary *mutableDictionary;
}
Here is the line of code that's highlighted in Instruments
self.mutableDictionary = [self.immutableDictionary mutableDeepCopy];
Here is the method for creating a mutable copy of a Dictionary
#interface NSDictionary(MutableDeepCopy)
-(NSMutableDictionary *)mutableDeepCopy;
#end
Here is method implementation, I've highlighted the code that Leaks saids is leaking 100%
- (NSMutableDictionary *) mutableDeepCopy {
NSMutableDictionary *dictionaryToReturn = [NSMutableDictionary dictionaryWithCapacity:[self count]];
NSArray *keys = [self allKeys];
for(id key in keys) {
id value = [self valueForKey:key];
id copy = nil;
if ([value respondsToSelector:#selector(mutableDeepCopy)]) {
copy = [value mutableDeepCopy];
} else if ([value respondsToSelector:#selector(mutableCopy)]) {
copy = [value mutableCopy]; //This is the Leak
}
if (copy == nil) {
copy = [value copy];
}
[dictionaryToReturn setValue:copy forKey:key];
}
return dictionaryToReturn;
}
You need to analyse this in light of Apple's Memory Management Rules.
Starting with this line:
self.mutableDictionary = [self.immutableDictionary mutableDeepCopy];
I would expect mutableDeepCopy to return an object I own, so at some point I need to release or autorelease it. e.g.
NSMutableDeepCopy* temp = [self.immutableDictionary mutableDeepCopy];
self.mutableDictionary = temp;
[temp release];
or
self.mutableDictionary = [[self.immutableDictionary mutableDeepCopy] autorelease];
So now we need to look at mutableDeepCopy. Because it has 'copy' in the name it needs to returned an "owned" object which, in practice means "forgetting" to release the returned object. You have already failed to do that when you create the returned object in the first line, since dictionaryWithCapacity: gives you an object you do not own. Replace it with
NSMutableDictionary *dictionaryToReturn = [[NSMutableDictionary alloc] initWithCapacity:[self count]];
Now you own it.
It is important that you make your mutableDeepCopy obey the rules because it means you can treat the objects returned from mutableDeepCopy, mutableCopy and copy in exactly the same way. In all three cases you own the object copy that you insert into the array. Because you own it, you must release it or it'll leak as you found out. So, at the end of the loop, you need
[copy release];
That'll stop the leak.
How is your property declared? If is is retain or copy, then this doesn't leak.
Your problem is that the name mutableDeepCopy suggests that it returns a retained object, and not an autoreleased one as it actually does.
Edit:
And at the mutableDeepCopy itself, you need to release the copy variable after adding to the dictionary.
mutableCopy increments the retain count of the object, as does setValue:forKey:. This means that when dictionaryToReturn is dealloc'ed, the object that had mutableCopy called still has a retain count of one.
Try doing this instead:
copy = [[value mutableCopy] autorelease];