我有一个崩溃问题 - >([AGIPCAssetsController numberOfSectionsInTableView:]:消息发送到解除分配的实例0x1976d7b0),它有时会发生,但有时不会发生.所以,我不知道如何解决它.看来我在tableview内存管理中做错了.我在iOS7/iOS7.1 iphone4上测试过.我没有测试其他版本/手机.这是我的代码.
@interface AGIPCAssetsController : UIViewController<UITableViewDataSource,
UITableViewDelegate, AGIPCGridItemDelegate,UIAlertViewDelegate >{
}
@property (retain,nonatomic) IBOutlet UITableView *tableView;
Run Code Online (Sandbox Code Playgroud)
我将tableview属性设置为retain,但是在dealloc之后调用numberOfSectionsInTableView.这是我的问题.1.任何解决它的想法?2.任何解决方法的想法.如调用[NSThread sleepForTimeInterval:0.5]; 在dealloc的开头,保持对象活着.
我正在编写一个框架,我有一个自定义init方法的对象:
@implementation OSDatabase
@synthesize database;
// MEM
- (void)dealloc {
sqlite3_close(database);
[super dealloc];
}
// INIT
- (id)initWithDatabasePath:(NSString *)path error:(NSError **)error {
if (self = [super init]) {
if (!sqlite3_open_v2([path UTF8String], &database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)) {
error = [NSError errorWithDomain:@"OSDatabaseErrorDomain" code:1 userInfo:nil];
[self dealloc];
return nil;
}
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud)
如果出现错误dealloc,在init方法内部调用是否安全?我不确定这一点,记忆管理是我生命中最重要的事情之一.
谢谢.
它是否正确?
- (void)dealloc {
[super dealloc];
[stageObjects release];
}
Run Code Online (Sandbox Code Playgroud)
或者我应该打电话
[super dealloc]
Run Code Online (Sandbox Code Playgroud)
总是在所有版本之后,我的意思是这个功能的最后一行?
我是否必须release在dealloc类的方法中调用非指针变量?
例如
@interface myClass : NSObject {
BOOL isDirty; // do i have to release this?
NSInteger hoursSinceStart; // and this?
NSDate *myDate; // i will release the pointer in dealloc
}
@property (assign, nonatomic) NSInteger hoursSinceStart; // property to release?
@property (assign, nonatomic) BOOL isDirty; // property to release?
@end
Run Code Online (Sandbox Code Playgroud) 在我的一个函数中,我有一个while循环,它可能需要临时创建一个对象.我的代码看起来像这样:
while(c < end){
if(specialCase){
Object *myObject = [Object alloc];
//do stuff with myObject
//I tried [myObject dealloc] here, but it crashed when this method was called.
}
c++;
}
Run Code Online (Sandbox Code Playgroud)
代码工作正常,但我担心内存泄漏.我想知道是否以及如何释放myObject.
我有一个名为JSONNetworkUtility的服务对象,我将它作为ivar存储在我的模型中,以及具有相同名称,非原子和保留的合成属性:
myNetworkUtility = [[JSONNetworkUtility alloc] initNetworkConnectionWithURL:urlString withQueryString:nil delegate:self];
Run Code Online (Sandbox Code Playgroud)
该委托包括两个回调,一个是networkUtility:didFailWithError:,另一个是networkUtility:didFinishWithData:.奇怪的是该属性导致一些奇怪的错误:
- (void)networkUtility:(JSONNetworkUtility *)networkUtility didFinishWithData:(NSArray *)jsonArray
{
NSLog(@"myNetworkUtility = %@", myNetworkUtility);
// returns <JSONNetworkUtility: 0x3944450>
NSLog(@"networkUtility = %@", networkUtility);
// also returns <JSONNetworkUtility: 0x3944450>
NSLog(@"self.myNetworkUtility = %@", self.myNetworkUtility);
// fails and throws an NSZombie error!
}
Run Code Online (Sandbox Code Playgroud)
我在该行上得到的错误是:
*** - [MyModel myNetworkUtility]:发送到解除分配的实例0x148190的消息
我完全难过了!关于为什么它在吸气器上失败的任何线索?为什么它会返回一个完全不同的对象?
我使用getter的原因是因为我想使用self.myNetworkUtility = nil所以我可以用一个新对象在属性的顶部写,但我已经把它切回到这个跟踪,我仍然有问题......
谢谢!
在下面的场景中,我遇到了崩溃
if (self.videoEngine != nil)
{
[self.videoEngine.player.view removeFromSuperview];
[videoEngine release];
self.videoEngine = nil;
}
Run Code Online (Sandbox Code Playgroud)
videoEngine对象是(非原子的,保留的),它是使用videoEngine = _videoEngine合成的.如果我删除self.videoEngine = nil行代码正常工作.这是正确的行为,为什么nil线会导致崩溃?self.videoEngine = nil是否仍会导致viewDidUnload函数出现问题?