Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.
I have several objects that have NSMutableArrays which will contain other objects that contain ... etc.
When I delete the highest object in the hierarchy, I have memory leaks in all of the [NSObject init] method. They all are identical and look like the following:
1. You're doing something really wrong if you are releasing the items in an array while they are still in the array.
2:
arrayOfObject2 = nil;
...ok, so arrayOfObject2 is now nil, so what exactly does this do after that:
[arrayOfObject2 release];
3. That init method makes me cry.
Instead of simply pointing out my errors, could you explain what I'm doing wrong? I'm sorry for being relatively new at this. I haven't seen much code for creating your own object, so I tried to do it on my own.
No, first you explain why you are doing what you are doing. I know you haven't seen any examples of an init method like that, and I'm reasonably certain that you have seen init methods in other cases (hint: view controllers) that should give you an idea of what needs to happen.
Being new is no excuse. You knew enough to write the code that you have, and you know enough to use instruments. That's a cop out. If you can't explain why you did something, THAT is the first problem that needs to be addressed.
I made that init the way I did because when I tried to add objects to that array in the code, it did not work because the array had not be initialized, so I initialized it. That makes sense to me. I'm not sure what else I'm supposed to do there to prevent you from crying.
No, what I'm saying is that if the array is going to die at this point, it will automatically take care of removing the items for you. I guess it doesn't hurt anything to clear it out yourself, but it's not necessary.
But otherwise, your init method is now fine, and your dealloc method is now fine. So if you are still leaking, you have issues elsewhere.
You don't need removeAllObjects at all. What exactly is reporting the leak and with what message? Instruments, or is just Xcode giving you a warning? Are you retaining the array anywhere else?
No, what I'm saying is that if the array is going to die at this point, it will automatically take care of removing the items for you. I guess it doesn't hurt anything to clear it out yourself, but it's not necessary.
But otherwise, your init method is now fine, and your dealloc method is now fine. So if you are still leaking, you have issues elsewhere.
Is your dealloc method being called now?
Yes. Now, how do I safely remove all of these objects? What I was doing was starting at the top and working my way down to the lowest object. I deleted all of the objects at the lower level and worked my way back up.
When you put an object in an NSArray, the array will retain it, so you should release it (if you allocated it prior) after adding it to the array. When you release an array it will send a release message to all of its objects as well so that will automatically remove them.
There are no multidimensional NSArrays. The closest thing would an an array of arrays. So following logic, the top array would send release to the arrays it holds and they would in turn send release to objects in them. This should be easy to deduce.
Replies
2:
...ok, so arrayOfObject2 is now nil, so what exactly does this do after that:
3. That init method makes me cry.
SlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeBeing new is no excuse. You knew enough to write the code that you have, and you know enough to use instruments. That's a cop out. If you can't explain why you did something, THAT is the first problem that needs to be addressed.
SlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeTry it. Do you still leak?
Oh, and I should also add:
4. [super dealloc] should be the last thing you do in dealloc, not the first.
SlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeBut otherwise, your init method is now fine, and your dealloc method is now fine. So if you are still leaking, you have issues elsewhere.
Is your dealloc method being called now?
SlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeIs this the correct way to do it?
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeIn order to delete the highest object, I just need to say [highestObject release]?
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeIf anybody is still reading this thread, the leak is coming from the array alloc line, and I have no idea why still! :( :(
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome...appear anywhere else in your class?
As I said before, the issue is no longer in the code you've posted so far. But you haven't posted any more code.
SlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome