On Dec 21, 2008, at 9:09 AM, Nick Rogers wrote:
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.

Would you dive into tuning a lawnmower without understanding how an internal combustion engine works [abstract] or without technical documentation on the particular family of small engines you are dealing with [specific]?

Would you do electrical wiring on your house, potentially risking fire hazard, without a basic understanding of 120v (220v for some of you) wiring and a minimal understanding of local building codes?

Would you hop in an airplane after the navigation and flight controls had been replaced by a very experienced auto mechanic who had never worked with, nor received training on, airplane maintenance?

Probably not, in all cases. But that is exactly what most programmers do. Arguably, writing code against the Cocoa frameworks (or any other modern GUI API) means effectively interfacing with a system of moving parts more complex than a car or, even, a modern jet aircraft. The only saving grace is that software doesn't tend to blow up and kill people when you mess up.

And I'm only comfortable in making such a snarky statement with the full admission that, in hindsight, I did exactly the same thing when I started out. As well, I had a group of people far more experienced at me railing upon me to stop stabbing about in the dark, read the damned docs/headers/source/schematics, and quit wasting my time...

:)

Seriously, though, start here:

http://developer.apple.com/referencelibrary/GettingStarted/GS_Cocoa/index.html

It will likely lead you here:

http://developer.apple.com/documentation/Cocoa/Conceptual/OOP_ObjC/Introduction/chapter_1_section_1.html

And your memory management questions are covered here:

http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

Though still useful to know the above, for new code there is generally no reason to avoid GC:

http://developer.apple.com/documentation/Cocoa/Conceptual/GarbageCollection/Introduction.html

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.

If you don't release an object, but destroy the containing object, then -- no -- it won't cause a crash immediately. It will cause a crash eventually when your application runs out of heap; fills the 2GB or so of heap -- memory -- available to it.

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.

VM is only really useful as a vague indication that your application may be leaking memory over time. It is completely useless for actually debugging memory use and potential leaks.

For that, there is a litany of additional tools. Start with Instruments. The Object Alloc instrument, in particular, is explicitly designed to debug this kind of stuff.

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?

Yes -- two.

(1) When you release any of the Cocoa collection classes via -release, it sends -release to all contained objects. So, there is no need to enumerate the array and release the children manually.

That your program isn't crashing in the above indicates that you very likely are over-retaining the children in the first place. See the memory management guide above.

(2) Objective-C is a nil-eats-messages language. That is, there is no need to check to see if an object reference is non-nil prior to messaging it. The above should be compacted to this:

- (void) dealloc
{
    [children release];
    [fileName release];
    [pathName release];
    [super dealloc];
}

b.bum

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to