有人可以解释为什么跟随我的代码崩溃吗?在foo方法中的块内发生崩溃。我有EXC_BAD_ACCESS或“对象错误:双重释放”。当我将“启用僵尸对象”设置为“开”时,我还收到了“-[NSObject description]:消息发送到已释放实例”。
@interface ViewController ()
@property (nonatomic, strong) NSObject *obj;
@end
@implementation ViewController
// just adding button
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setTitle:@"test" forState:UIControlStateNormal];
btn.frame = CGRectMake(100, 100, 100, 100);
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
// fired by button
- (void)btnAction:(id)sender {
for (int i = 0; i < 100; i++) {
[self foo];
}
}
// I want to understand this method
- (void)foo {
NSLog(@"foo");
self.obj = NSObject.new;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), …Run Code Online (Sandbox Code Playgroud) 我注意到当我在故事板中创建一个插座时,它会生成以下代码__weak IBOutlet UILabel *mLabel;.
为什么它将它声明为弱指针?根据我的理解,当对象被释放时,其所有成员也将被释放.在我的大部分代码中,我都宣称这些出口是强有力的指针.这会产生问题吗?
我正在开发一个没有ARC的旧项目.它有很多错误,代码看起来很难看,我正在重写它.
快速浏览一下我的代码吧
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell = [self createCellWithInfo:[self.search objectAtIndex:indexPath.row]];
return cell;
}
-(UITableViewCell *)createCellWithInfo:(NSDictionary *)info{
UITableViewCell * cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@“Cell”] autorelease];
//set image for cell
//set text for cell.textlabel
//set text for cell.detailTextLabel
//create an UIButton and add to cell.content view
return cell;
}
Run Code Online (Sandbox Code Playgroud)
重点在于这行代码
[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@“Cell”] autorelease]
如果我@"Cell" …
在运行我的线程一段时间后,Instruments表明__NSDate已经稳定地修改了它的#vide值.
我的结论是,这个步骤不会处理对象.但是,这一行导致编译错误NSAutoreleasePool*pool = [[NSAutoreleasePool alloc] init];
如何强制此线程保留其所有对象,或者如何使用工作ARC创建正确的线程.
- (void) start {
NSThread* myThread = [[NSThread alloc] initWithTarget:self
selector:@selector(myThreadMainRoutine)
object:nil];
[myThread start]; // Actually create the thread
}
- (void)myThreadMainRoutine {
// stuff inits here ...
// Do thread work here.
while (_live) {
// do some stuff ...
[runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];
[NSThread sleepForTimeInterval:0.05f];
}
// clean stuff here ...
}
Run Code Online (Sandbox Code Playgroud) multithreading objective-c nsthread ios automatic-ref-counting
曾经是dealloc但是因为ARC已经消失了.
我需要一些方法来确切地告知对象何时被释放(我宁愿不使用乐器,因为它真的很慢而且现在不适合我.)
启用ARC时,将o在此代码段中放入自动释放池吗?
- (NSObject *)obj {
NSObject *o = [[NSObject alloc] init];
return o;
}
Run Code Online (Sandbox Code Playgroud)
更重要的是,这两个代码片段之间的区别是什么?
- (NSObject *)obj {
NSObject * __autoreleasing o = [[NSObject alloc] init];
return o;
}
Run Code Online (Sandbox Code Playgroud)
与
- (NSObject *)obj {
NSObject * __strong o = [[NSObject alloc] init];
return o;
}
Run Code Online (Sandbox Code Playgroud) 我正在关注Ray Wenderlich网站上关于Swift中的ARC的教程,我很想知道为什么在操场上创建一个类时可选的允许但不是隐式的可选项?
我到目前为止的游乐场代码是:
class User {
var name: String
init(name: String) {
self.name = name
print("User \(name) is initialized")
}
deinit {
print("User \(name) is being deallocated")
}
}
class Phone {
let model: String
var owner: User?
init(model: String) {
self.model = model
print("Phone \(model) is initialized")
}
deinit {
print("Phone \(model) is being deallocated")
}
}
do {
let user1 = User(name: "John")
}
let user2 = User.init(name: "Berry")
Run Code Online (Sandbox Code Playgroud)
在Phone类中,如果我将所有者变量更改为带有感叹号的隐式可选项,则操场不会抛出错误,但是如果我删除问号或不使其成为可选项,则会收到错误.
如果未设置,则隐式可选强制应用程序是否会崩溃?
感谢任何帮助,以深入了解为什么隐式可选项是可以的.
我正在观看此视频,其中Marco正在讨论自动参考计数.我已经知道在Android和iOS(Firemonkey)下我的对象被重新计算,所以我不需要try finally块.
引用计数实现是否根据平台(VLC或FMX)或OS运行?
我的意思是:
var a: TObject;
begin
a := TObject.Create;
a.use1;
a.use2;
end;
Run Code Online (Sandbox Code Playgroud)
如果它在Android/iOS/Mac上运行,这在Firemonkey中很好,我没有内存泄漏.但是,如果我在Windows下运行它(并且我使用过Firemonkey),由于没有引用计数,我是否还有内存泄漏?
反正从视频我已经理解的是,try finally随着通话Free,即使在ARC,是不是不好的用法,但它只是无用的.
我已经读过如果你将一个新对象分配给一个弱属性,那么该对象将在赋值后被释放.甚至编译器的警告都是一样的.
@interface RetainCycleObjCViewController ()
{
}
@property (nonatomic, weak) void (^weakBlock)(void);
@end
@implementation RetainCycleObjCViewController
- (void)viewDidLoad {
[super viewDidLoad];
_weakBlock = ^void{
NSLog(@"Execution inside a weakBlock");
};
_weakBlock();
}
@end
Run Code Online (Sandbox Code Playgroud)
我得到了与weakBlock相同的警告:将块文字分配给弱变量; 对象将在分配后释放
但是当我在下一行执行_weakBlock()时,它仍会打印语句.这怎么可能?因为现在应该从内存中删除新创建的块对象,给定0引用计数?
我有一部分代码需要优化-所以我已经完成了直接调度。直接分派可以工作(如代码执行,执行正确的操作且不会崩溃),但是ARC某种程度上失去了对客户端对象的跟踪,并且永远也不会被释放。标准版本的调度也适用,并且也不会泄漏。如何修复直接发送版本?
标准发布版本:
Client * client;
client = [Client newClientForServerSocket: serverSocket];
Run Code Online (Sandbox Code Playgroud)
直接发送版本:
Client * client;
Class clientClass = Client.class;
client = (*IMP_newClientForServerSocket)(clientClass,@selector(newClientForServerSocket:),serverSocket);
Run Code Online (Sandbox Code Playgroud)
(附带说明:奇怪的是,如果我将Client.class直接粘贴在调度的“ self”参数中,则调度崩溃。可能是一个提示。)
ios ×7
objective-c ×6
autorelease ×1
delphi ×1
iboutlet ×1
nsthread ×1
optional ×1
selector ×1
swift ×1
uitableview ×1