Re: how to pass arguments to NSMenu selector ?
Re: how to pass arguments to NSMenu selector ?
- Subject: Re: how to pass arguments to NSMenu selector ?
- From: "Sherm Pendley" <email@hidden>
- Date: Thu, 12 Jun 2008 13:31:17 -0400
On Thu, Jun 12, 2008 at 12:32 PM, Dharmendra <email@hidden> wrote:
>
> I made nodeURL local variable like
> NSURL *nodeURL = [[[NSURL aloc] initWithString:....] autorelease];
You could make that a bit shorter by calling a convenience method that
returns an object you don't have to (auto)release:
NSURL *nodeURL = [NSURL urlWithString:...];
> 10000 times. The reason I am explaining this is I still have to figure out
> how to use dealloc. I have followed the procedure described at MacTech
> tutorial on Menulet but i get an error that dealloc is not declared and
> first time used, when it is defined on top. Most of the online codes i have
> seen has [super dealloc] used within a -dealloc function, but not called
> explicitly, so is it automaticalled called on meeting certain criteria ?
Yes, exactly, it's called when an object's retain count drops to zero.
Having said that, if you find yourself chasing a memory-management
bug, don't bother checking the retain count directly. Other objects
(outside of your own code) may retain an object, it may be sitting in
an autorelease pool waiting to actually get released, etc.; there are
far too many factors beyond your own code that can affect the retain
count for it to be of any use in debugging your own code.
In a nutshell, the Cocoa memory management guidelines state that, if
you create an object with one of a very small number of methods*, or
if you send it a -retain, then you're responsible for releasing it
when you're done with it. Otherwise you *must not* release it.
Typically, the code to handle retaining and releasing objects is found
in setter methods, the idea being that you only retain something
you're going to keep, and keeping it means storing it in an instance
variable.
So, you pack all of the memory management code into the instance
variable's setter method, then *always* call that method instead of
assigning directly to the instance variable. Then, if something is
leaking, all you need to do is check your setter methods - are they
releasing the old object before assigning a new one? Likewise, if
something is being over-released, you'd check the setter to make sure
it's retaining the new value when it needs to. Xcode even has some
scripts to automatically generate the proper accessor method for you -
j
In contrast, if you assign directly to the instance variable, you'd
need to worry about releasing the old value and retaining the new,
every time you make the assignment - and tracking down a bug would
mean checking every place in your code where you do that, instead of
being able to just look once in the setter method.
* The complete list of methods, as well as a more thorough description
that includes all the rare edge cases, can be found here:
<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/MemoryManagementRules.html>
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden