简体   繁体   中英

Cocoa Garbage Collection and Retain Cycles

I'm still pretty new to Cocoa/Objective-C, so I hope I can explain my question.
I created a Garbage Collected application in Xcode 4.2 I have my app delegate that creates an Authorize object (my class) this object will create and present a Password object (my class/xib).

The Password window accepts input and either approves or denies and closes. From there, the Authorize object will close and control returns to the app delegate. When the password nib is presented as a modal window.

As a test, I include an NSLog statement when each object is initialized and when it is finalized. For some reason, my Password object is not being finalized. The authorize object is finalized.

I even eliminated the Authorize object and created the Password directly from the app delegate - still no finalize.

I have researched and tried everything/anything I can think of to get this object to finalize (I'm assuming it's not releasing the memory for this object if it is not finalized).
Can anyone suggest something that I might have overlooked?

Update:
After more experimenting, I found the problem. I created a new project with only the AppDelegate and a WindowController/Nib file. Using the same settings with GC and build, I was able to eliminate everything until I found the issue.
I have a NSSecureTextfield in my Password nib. For whatever reason, this was not releasing when the window was closed.
I added the line [textField removeFromSuperview] before closing the window and the finalize method was called.
I'm not sure if this is the best method, but it seems to work.

For the ARC option, I was under the impression that the project would need to target 10.7 or higher to use ARC. I was hoping to target 10.5 as a minimum.
Maybe I'm wrong about that. If I am, please let me know.

If finalize isn't being called, then either you've still got a strong reference, or GC isn't running. Since other objects finalize, obviously GC is running. So you're going to have to look for who is holding a strong reference. Audit your code for everyone who has a variable of the Password type as a start. Make sure it's not stuck into a dictionary or array somewhere. Make sure it's not held by a view controller or view that isn't gone yet.

And if at all possible switch to ARC rather than GC. This probably would be exactly the same, but everything else is much better. GC was an interesting experiment that many of us ignored, but ARC is definitely where the platform is headed.

Regarding this part of the (updated) question:

For the ARC option, I was under the impression that the project would need to target 10.7 or higher to use ARC. I was hoping to target 10.5 as a minimum. Maybe I'm wrong about that. If I am, please let me know.

You can target 64-bit Snow Leopard or later using ARC, but if you do you can't use zeroing weak references - use assign properties instead and mark any explicitly declared ivars as __unsafe_unretained if you want to go this route. (More info on that in this question .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM