Re: Memory Management Q. 1 and 2
Re: Memory Management Q. 1 and 2
- Subject: Re: Memory Management Q. 1 and 2
- From: Dave DeLong <email@hidden>
- Date: Sun, 21 Dec 2008 10:19:04 -0700
Hi Nick,
1. Not releasing an object does not (that i've ever seen) crash an
app. It does cause a memory leak (unless you clean it up elsewhere),
which may *eventually* crash your app (if it runs out of memory), but
that also seems unlikely. Is there a reason why you're not releasing
the array if you're destroying the object? If the array exists beyond
the lifetime of its containing object, then that would be a good
indication that you've got your array in the wrong spot. Generally,
when you destroy an object, you should release everything it was
holding on to.
2. With regards to trees, if you release the root object, it should
recurse through the entire tree structure and release everything in
the tree as well (because the root no longer cares about its immediate
children, which probably will cause them to be dealloc'd, and if they
are, then they release their children, etc etc). With regards to the
method you posted, if you release the children array, it will
automatically release everything inside it. There's no need to
manually release everything in the array. If for some extremely
bizarre reason you need the empty array to exist beyond the
deallocation of its containing object, then it'd probably be simpler
to release the array and then reallocate it, instead of manually
releasing everything in it.
HTH,
Dave
On 21 Dec, 2008, at 10:09 AM, Nick Rogers wrote:
Hi,
While learning to program in cocoa, people usually start out without
giving much thought to memory management especially about freeing
the used up memory.
I can say so, because I know a few people like that and also I'm one
of them.
My focus was on to get started on the project quickly and complete
it ASAP.
But now that its almost finished up, I'm forced to think about
freeing memory.
These are 2 questions in a series of more upcoming ones.
1. There is an instance variable (NSMutableArray*) in my object,
that I use to hold a large number of elements.
Now when I destroy this object, I don't release the array.
Will it have any effect like making the program crash, subsequently?
Especially when I again create the same kind of object again and
again fill the array.
2. I'm feeding an object of type HDIR* named dirRoot to the
NSOutlineView.
This HDIR can have a number of children of type HDIR* stored in an
NSMutableArray * named children (an ivar in HDIR object).
So its a tree.
When I run the program, the Activity Monitor shows the virtual
memory increasing as this tree is populated.
After a substantial increase in VM, I stop the tree and then release
this dirRoot (HDIR*) object.
But at this time the VM remains the same in Activity Monitor.
The dirRoot may also be retained by some other object, but I have
taken care to release those objects as well.
So my question is whether this tree is released and the VM shown in
Activity Monitor may not be released back to the system.
OR the tree may not have been released at all?
the dealloc method of HDIR is as follows:
- (void)dealloc
{
if (children)
{
while ([children count])
{
HDIR *child = [children lastObject];
[children removeLastObject];
[child release];
}
[children release];
children = nil;
}
if (fileName)
{
[fileName release];
}
if (pathName)
{
[pathName release];
}
[super dealloc];
}
Is there a problem in this method?
Thanks,
Nick
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden