I have been fighting a memory leak, well a little more then one it just happens to mostly be in the same location. I have this issue where I am using a class to store global information that is needed through out the code because it stores the list of food that a user adds to their meal plan. The issue that I have is that one it is a relatively large data structure that I am using. The data structure is a dictionary with keys to five other dictionaries that all have 8 mutable arrays that can be as large as the user wishes due to the fact that can add as much food as their heart desires.
I have set the structure like so
mealInfo = [[NSMutableDictionary alloc] init]
[mealInfo setObject:[NSMutableDictionary dictionary] forKey:"foo"];
[[mealInfo objectForKey:"such"] setObject: [NSMutableArray array] forKey:"bar"];
this is repeated for the complete data structure. This did seem to solve some of my possible memory leaks when I analyzed in xcode. The issue is further then that cause I have an idea what the issue is just not the solution. I have objects created, which create this structure in the local scope of the .m file I am in. I go save this structure in the class where mealInfo is created, not the local scope, with a static object of that class. (I am not using too much code because it is one in a lot of locations and two is way too many lines)
After I save to the static variable of the class I release the local scope, I can not release the mealInfo because when I have done that in the past it actually causes the object to disappear and nil in the class that it was initialized in making it inaccessible to other classes trying to access this information.
One solution I thought of was to have all the saving and storing occur in the class in which it was initialized and just use getters and setters, but was trying to make it more readily accesible by making it an object that can be called from other classes and changed in those classes which becomes a little more complicated and confusing at times. This I was trying to avoid. I will do that if that is the only solution, but would like to get this method to work first.
I hope this is not too confusing. I also hope someone might have a solution to this memory leak. Thank you all for the time at least to read all of this.
It’s a little hard to understand your question… but if
mealInfoitself is your static class variable then you can alloc it once – and once only – by doing the following.Otherwise you have to release it each time you alloc it, or it will leak.