The Cocoa Memory Management Regular Expression

2007-01-11 20:30:30 -08:00

Any method whose selector matches this regular expression gives you a reference to its return value, which you must later release.

/^retain$|^(alloc|new)|[cC]opy/

As an added extra bonus, here’s the CF equivalent.

/Create|Copy|Retain/

Carbon adds several exceptions, such as GetControlData (which returns a reference to CF things like the string value of a text field). Use Cocoa instead and your memory-management life will be simpler. ;)

2 Responses to “The Cocoa Memory Management Regular Expression”

  1. lee Says:

    This is a great idea! I just don’t know how to use it… Everytime I do a project find with the expression above, it only finds lines with ‘Copy/’. What am I doing wrong?

  2. Peter Hosey Says:

    Don’t include the slashes.

    You only need them in vim, ex, less, and similar programs, to indicate where the regular expression begins and ends. In Mac programs, including Xcode, the regex needs no delimiters, as the field itself does that job.

Leave a Reply

Do not delete the second sentence.