当我的应用程序终止时,我使用以下代码(例如我的appController.m内部)进行一些清理...
- (void) dealloc {
[myObject release]; // myObject 's dealloc will not be called either !!!
[arraySMSs release];
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
当应用程序退出时,此方法永远不会被调用!为什么?有没有更好的地方来清理?没有调用的事实解决了内存泄漏问题?或者OS确实需要清理?
谢谢...
这是我的情况.这很复杂所以请耐心等待.
我有一个视图类,让我们称之为MyView.它会创建一个加载指示器子视图,然后启动一个将加载数据的后台操作.它还会创建一个块,后台队列操作在完成后将在主队列中排队.该块通过添加UITextView带有加载数据的另一个子视图a来准备视图.当然,要做到这一点,块必须具有对视图的引用.
因此后台操作保留了块,块保留了视图.和我一起到目前为止?
有时,MyView在后台队列操作完成之前,会从其超级视图中删除实例.有时,在后台队列操作被完全清除之前,主要的队列操作(调用块)会被彻底清除.在这种情况下,MyView可以-dealloc在后台线程上调用它的实例,因为对视图的最后一个引用属于该块,并且对该块的最后一个引用属于后台操作.
UIKit不喜欢从任何线程调用,而是主线程.在这种情况下UITextView,显然甚至包括-dealloc电话.我在文本视图中得到EXC_BAD_ACCESS了一个叫做"web thread lock"的东西-dealloc.
我认为后台线程有时会有最后一个引用是合理的,我想在我的-dealloc实现中处理这个问题,如下所示:
- (void)dealloc {
if ([NSOperationQueue currentQueue] == [NSOperationQueue mainQueue]) {
// The usual -- dealloc subviews safely on the main thread
self.myIvar = nil;
[super dealloc];
}
else {
// Not on the main thread, so keep the object alive
// in spite of the dealloc call.
[self retain]; // explicit retain …Run Code Online (Sandbox Code Playgroud) 这是viewDidLoad在iOS 4.x或更低版本中发布保留的视图的正确(最佳?)方式吗?还有什么需要考虑的吗?
- (void) viewDidUnload
{
[super viewDidUnload];
[self releaseViews];
}
- (void) dealloc {
[self releaseViews];
[super dealloc];
}
#define SAFE_RELEASE(a) [a release]; a = nil;
- (void) releaseViews {
SAFE_RELEASE(myView1);
SAFE_RELEASE(myView2);
SAFE_RELEASE(myView3);
}
Run Code Online (Sandbox Code Playgroud) 如果创建了UIviewController子类,则会自动为您创建方法"dealloc".
- (void)dealloc{}
Run Code Online (Sandbox Code Playgroud)
但是,当我创建一个Objective-C类时,该方法不会自动创建.是否有必要添加dealloc方法以便我可以在类中释放属性?特别是如果我将它们保留在头文件中?
例如,在我的Objective-C类头文件中
@interface ClassA : NSObject
{
NSString *someProperty;
UINavigationController *navcon;
}
@property (nonatomic, retain) NSString *someProperty;
@property (nonatomic, retain) UINavigationController *navcon;
Run Code Online (Sandbox Code Playgroud)
我是否需要手动创建dealloc方法以释放属性,如下所示?
-(void)dealloc
{
[someProperty release];
[navcon release];
}
Run Code Online (Sandbox Code Playgroud) 在可可中,ARC使您不必担心保留,释放,自动释放等.它还禁止呼叫[super dealloc].-(void) dealloc允许一种方法,但我不确定是否/何时调用它.
我知道这对于对象等都是如此的好,但是我把free()它放在哪里与malloc()我所做的匹配-(id) init?
例:
@implementation SomeObject
- (id) initWithSize: (Vertex3Di) theSize
{
self = [super init];
if (self)
{
size = theSize;
filled = malloc(size.x * size.y * size.z);
if (filled == nil)
{
//* TODO: handle error
self = nil;
}
}
return self;
}
- (void) dealloc // does this ever get called? If so, at the normal time, like I expect?
{
if (filled)
free(filled); …Run Code Online (Sandbox Code Playgroud) 我正在与NSWindowController一起实现首选项窗口。Apple的文档指出,默认情况下不会释放控制器和窗口,因为不必重新加载所有内容非常有用,这很有意义。但是他们的文档继续说您可以覆盖该行为,但不解释如何做。
苹果的文档:
When a window is closed and it is part of a document-based
application, the document removes the window’s window
controller from its list of window controllers. This results
in the system deallocating the window controller and the
window, and possibly the NSDocument object itself. When a
window controller is not part of a document-based application,
closing the window does not by default result in the
deallocation of the window or window controller. This is the
desired behavior for …Run Code Online (Sandbox Code Playgroud) 我最近改变了我的应用程序以使用UINavigationController,之前我使用的是UINavigationBar,使用级联子视图添加,这有点脆弱.
我正面临内存使用问题.泄漏工具没有显示任何泄漏,但我创建并添加到UINavigationController的ViewControllers似乎永远不会被释放.因此,每当我创建一个新的VC然后按下NavigationController的后退按钮时,内存使用量就会增加.
我只需这样创建并添加我的VC:
DetailViewController* detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
// setups
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
Run Code Online (Sandbox Code Playgroud)
该应用程序永远不会通过ViewController dealloc和viewDidUnload方法.每次按下后退按钮时,不应该调用这些吗?
我搜索了许多教程并阅读了Apple的内存管理,但是在使用NavigationController时,VC的内存生命周期并不存在.
memory iphone uiviewcontroller uinavigationcontroller dealloc
我怎么dealloc一个布尔值?
以下方式解除分配给我一个警告:指向整数转换的不兼容指针从'void*'分配给'BOOL'(又名'signed char')
- (void)dealloc {
self.booleanVar = nil;
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
也许我应该澄清一下,这是一个从NSObject继承的简单类.
我正在使用你在Cocoa Touch类中看到的self.var = nil模式.假设它是NSString*而应该在dealloc方法中使用self.var = nil还是[var release] ?我在这里有点困惑.
你好我有这个问题:我想有一个矢量作为类成员.这对我来说可能更容易,我为此道歉.
std::vector<int> *myVector;还是std::vector<int> myVector?它是否正确?
if(myCondition)
{
if(!myVector) //is this correct?
myVector = new std::vector<int>(); //is this correct? on this i have a error
}
Run Code Online (Sandbox Code Playgroud) 我有一个问题,我似乎无法dealloc UIDocument(在iCloud中使用)
运行NSMetaDataQuery后查找文件如下..
NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
_query = query;
[query setSearchScopes:[NSArray arrayWithObject:
NSMetadataQueryUbiquitousDocumentsScope]];
NSPredicate *pred = [NSPredicate predicateWithFormat:
@"%K == %@", NSMetadataItemFSNameKey, kFILENAME];
[query setPredicate:pred];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(queryDidFinishGathering:)
name:NSMetadataQueryDidFinishGatheringNotification
object:query];
[query startQuery];
Run Code Online (Sandbox Code Playgroud)
我处理我的查询
- (void)queryDidFinishGathering:(NSNotification *)notification {
NSMetadataQuery *query = [notification object];
[query disableUpdates];
[query stopQuery];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSMetadataQueryDidFinishGatheringNotification
object:query];
_query = nil;
[self loadData:query];
}
Run Code Online (Sandbox Code Playgroud)
然后加载或创建一个新文档.
- (void)loadData:(NSMetadataQuery *)query {
if ([query resultCount] == 1) {
NSMetadataItem *item = [query resultAtIndex:0];
NSURL *url = …Run Code Online (Sandbox Code Playgroud) dealloc ×10
objective-c ×5
ios ×4
cocoa ×2
iphone ×2
release ×2
aggregation ×1
boolean ×1
c++ ×1
free ×1
icloud ×1
memory ×1
memory-leaks ×1
nswindow ×1
quit ×1
retain ×1
uidocument ×1
vector ×1