有时我仍然需要编写一个-dealloc方法来进行清理.在极少数情况下,我需要引用实例的属性来正确地进行清理.例如从NSNotificationCenter取消注册给定的发件人对象,该对象由属性拥有和引用.
ARC是否在我自己的代码之后插入了它的属性释放代码,还是在我自己的代码之前将它插入-dealloc的开头?
如果ARC将在任何自定义-dealloc代码之前插入代码,那么这将非常危险,因为如果需要,您将无法再访问属性.
这个问题是关于ARC在合成的-dealloc中插入属性释放代码的地方,而不是关于实现-dealloc还是不实现.
我在我的.h文件中声明了一个属性
@property (weak, nonatomic) UIPickerView *levelPicker;
Run Code Online (Sandbox Code Playgroud)
在我的实现文件中合成为:
@synthesize levelPicker = _levelPicker;
Run Code Online (Sandbox Code Playgroud)
然后我在同一个实现文件中有一个代码块,它执行以下操作:
if (self.levelPicker == nil) {
self.levelPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
self.levelPicker.delegate = self;
self.levelPicker.dataSource = self;
}
textField.inputView = self.levelPicker;
Run Code Online (Sandbox Code Playgroud)
在这种情况下,self._levelPicker未设置为新的UIPickerView.即self.levelPicker = blah赋值不起作用.
但是,如果我将属性声明更改为:
@property (strong, nonatomic) UIPickerView *levelPicker;
Run Code Online (Sandbox Code Playgroud)
然后一切都按预期工作,_levelPicker设置为新分配的UIPickerView.
有人可以告诉我为什么会这样吗?我以为我正在理解参考是如何工作的,但我想我还有更多要学习的东西.我读了一些其他相关的SO帖子,但对我来说仍然不完全清楚.
我有一个按钮的iphone示例应用程序.点按它会调用代码:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"test"
message:@"test"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles: nil];
[alert show];
Run Code Online (Sandbox Code Playgroud)
应用程序已启用ARC.
问题是,如果我在警报中单击"确定"按钮,应用程序将与EXC_BAD_ADDRESS崩溃 - 可能是因为警报已被弧线删除.
建议的解决方法是什么?不向viewcontroller添加属性
谢谢
可能重复:
使用ARC为什么再使用@properties?
注意:我实际上并不认为我们可以取消属性,但我试图理解为什么不这样做的推理.:)
回到这个问题,似乎我们使用属性的主要原因是避免内存管理问题.换句话说,通过创建属性,我们不需要写出所有这些保留和释放方法:
- (void) setOtherObj:(MyOtherObject *)anOtherObject {
if (otherObject == anOtherObject) {
return;
}
MyOtherObject *oldOtherObject = otherObject; // keep a reference to the old value for a second
otherObject = [anOtherObject retain]; // put the new value in
[oldOtherObject release]; // let go of the old object
}
Run Code Online (Sandbox Code Playgroud)
由于ARC现在处理这个问题,我们是不是可以一起取消属性,只是通过做类似的东西来设置ivars otherObject = anotherObject?
我正在尝试使用地址簿,但我对内存管理的理解充其量也是如此.
我的项目使用的是自动引用计数(ARC),但据我所知,ARC只管理Objective-C中的保留/释放.
我理解我的第一个函数调用:ABAddressBookCreate()返回一个我"拥有"的ABAddressBookRef,因为我是从Create名字中的方法获取的.我CFRelease当我是受够了.
我不明白的是ABRecordRef在这个方法的过程中是如何保持活着的.我不应该CFRetain和CFRelease它一起吗?如果我没有保留/释放,我在同一个类中有另一个几乎相同的方法崩溃了ABAddressBookRef.
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef record = ABAddressBookGetPersonWithRecordID(iPhoneAddressBook, self.addressBookRecordID);
NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(record, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString*)ABRecordCopyValue(record, kABPersonLastNameProperty);
NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
ABMultiValueRef phoneRef = ABRecordCopyValue(record, kABPersonPhoneProperty);
// Set up an NSArray and copy the values in.
NSArray *phoneNumberArray = (__bridge_transfer id)ABMultiValueCopyArrayOfAllValues(phoneRef);
CFRelease(iPhoneAddressBook);
// Finally, do stuff with contact information in Obj-C land..
Run Code Online (Sandbox Code Playgroud)
退出的问题:有我创建通过不调用泄漏CFRelease …
我正试图在我的app委托中使用NSWindowController打开一个窗口.我创建了一个带有关联NIB的基本NSWindowController并尝试以这种方式显示窗口:
@implementation MyAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Show the main window from a separate nib
MyWindowController * theWindowController = [[MyWindowController alloc] initWithWindowNibName:@"MyWindowController"];
[theWindowController showWindow:self];
}
@end
Run Code Online (Sandbox Code Playgroud)
当我启动应用程序时,MyWindowController的窗口只出现一小部分秒(似乎一旦启动就会被释放).
使用ARC,我怎么能强迫窗户粘住而不是立即冲洗?我不使用NSDocuments,我希望能够同时使用许多这些MyWindowController.
我有以下代码为NSView背景生成"噪声纹理".
我已经链接的目标Cocoa和QuartzCore框架,在构建阶段,但是编译器保持与以下错误抱怨:
ARC语义问题
"接收'CIFilter'用于课堂消息是一个前瞻性声明"
"选择器filterWithName的未知类方法"
CIFilter *randomGenerator = [CIFilter filterWithName:@"CIColorMonochrome"];
[randomGenerator setValue:[[CIFilter filterWithName:@"CIRandomGenerator"] valueForKey:@"outputImage"]
forKey:@"inputImage"];
[randomGenerator setDefaults];
noisePattern = [randomGenerator valueForKey:@"outputImage"];
Run Code Online (Sandbox Code Playgroud)
这有什么不对?我已经尝试了所有可以想象的技巧,以摆脱错误.我肯定知道这段代码是有效的,因为它是一个没有任何问题编译的示例项目的副本.任何帮助表示赞赏.
在将Objective-C代码迁移到ARC时,我无法实现NSFastEnumeration协议.有人能告诉我,如何摆脱以下warnig(参见代码片段)?提前致谢.
// I changed it due to ARC, was before
// - (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*) state objects: (id*) stackbuf count: (NSUInteger) len
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*) state objects: (__unsafe_unretained id *) stackbuf count: (NSUInteger) len
{
...
*stackbuf = [[ZBarSymbol alloc] initWithSymbol: sym]; //Warning: Assigning retained object to unsafe_unretained variable; object will be released after assignment
...
}
- (id) initWithSymbol: (const zbar_symbol_t*) sym
{
if(self = [super init]) {
...
}
return(self);
}
Run Code Online (Sandbox Code Playgroud) 众所周知,block如果block隐含地保留也保留了对象,则a 可以诱导保留周期block.例:
self.block = ^{
[self foo];
};
Run Code Online (Sandbox Code Playgroud)
通常规定的解决方法是简单地使用a __weak self来避免保留周期:
__typeof(self) __weak weakSelf = self;
self.block = ^{
[weakSelf foo];
};
Run Code Online (Sandbox Code Playgroud)
但是,如果self在执行过程中被释放,则会导致问题foo.更好的策略是捕获块内本地的__strong引用__weak self.但是,我不知道是否__weak捕获了修饰符__typeof,如果是,我可以用另一个覆盖__strong吗?
所以,其中一个是正确的,但是哪个?
一个
__typeof(self) __weak weakSelf = self;
self.block = ^{
__typeof(self) strongWeakSelf = weakSelf;
[strongWeakSelf foo];
};
Run Code Online (Sandbox Code Playgroud)
乙
__typeof(self) __weak weakSelf = self;
self.block = ^{
__typeof(self) __strong strongWeakSelf = weakSelf;
[strongWeakSelf foo];
};
Run Code Online (Sandbox Code Playgroud)
C …
在Apple Instruments中检查内存分配时,我得到数千个""项目都将"TextInput"作为"负责任的库".
我没有在我的应用程序中使用键盘.
这些物体来自哪里?

谢谢!
memory-leaks objective-c instruments ios automatic-ref-counting
objective-c ×6
ios ×4
iphone ×2
cocoa ×1
instruments ×1
ipad ×1
ivar ×1
memory-leaks ×1
osx-lion ×1
properties ×1
xcode ×1