Objective-C Memory Management Being Exceptional 异常处理与内存

Objective-C Memory Management ? ?Being Exceptional ?异常处理与内存

3.1Cocoa requires that all exceptions must be of type NSException

cocoa?需要所有的异常是NSException类型的。

so even though you can throw an exception from other objects, Cocoa isn‘t set up to deal with those.

所以你即使从别的类抛出异常,cocoa?也不会处理。

?

Exception handling is really intended for errors that are generated by your programs. Cocoa frameworks usually handle errors by exiting the program, which is not what you want. You should throw and catch exceptions in your code, instead of letting them escape to the framework level.

cocoa框架通常用已经存在的程序解决异常。你应该throw and catch exceptions??在你自己的代码中,而不是任其跑到框架层。

To enable support for exceptions, make sure the -fobj-exceptions flag is turned on.

为了支持异常,你应该确保-fobj-exceptions打开。

When an exception is thrown and is not caught, the program stops at the exception point and propagates the exception.

当一个异常抛出后,并没有被接到的话,则程序停在异常点。

?

3.2 ?Keywords for exceptions?

All the keywords for exceptions start with @. Here‘s what each one does

所以的异常keyword?均以@开头。

(1)@try: Defines a block of code that will be tested to determine if an exception

should be thrown.

测试一个异常是否应该被抛出。

(2)@catch(): Defines a block of code for handling a thrown exception. Takes an

argument, typically of type NSException, but can be of other types.

处理异常

(3)@finally: Defines a block of code that gets executed whether an exception is

thrown or not. This code will always be executed.

不管是否抛出异常都运行。

(4)@throw: Throws an exception.

抛出异常

3.3 Catching Different types of exceptions?

You can have multiple @catch blocks depending on which type of exception you want to handle.

可以根据exception?的类型来选择@catch?的类型

@try{

} @catch (MyCustomException *custom) {

} @catch (NSException *exception) {

} @catch (id value) {

} @finally {

}

?

A program throws an exception by creating an instance of NSException and using one of two techniques:

一个程序抛出异常通过创建NSException?和使用下面的技术:

(1)Using @throw exception;

(2)Sending a raise message to an NSException objectwe‘ll create an exception:?我们建一个NSExcepiton?

NSException *theException = [NSException exceptionWithName: ...];

We can then throw with either?我们throw?通过下面:

@throw theException;

or

[theException raise];

You‘ll usually throw exceptions from inside the exception handling code.

也可以在一个exception handling code里再抛出异常。

@try {

? ? NSException *e = ...;

@throw e; }

? @catch (NSException *e) {

?? ? @throw; // rethrows e.

}

2.4 Exceptions need memory management too .?异常也需要内存管理

?

Memory management can be tricky when exceptions are involved.

- (void)mySimpleMethod

{

? ? NSDictionary *dictionary = [[NSDictionary alloc] initWith....];

? ? [self processDictionary:dictionary];

? ? [dictionary release];

}

let‘s imagine that processDictionary throws an exception. The program jumps out of this method and looks for an exception handler. But because the method exits at this point, the dictionary object is not released, and we have a memory leak.

假如processDictionary?抛出异常,但是dictionary?还没有释放,因此有了memory leaks .

解决办法:One simple way to handle this is to use @try and @finally, doing some cleanup in @finally because it‘s always executed (as we said earlier).

在@finally?处处理,因为总是执行。

- (void)mySimpleMethod

{

? ? NSDictionary *dictionary = [[NSDictionary alloc] initWith....];

? ? @try {

?? ? [self processDictionary:dictionary];

? ? }

? ? @finally {

? ? [dictionary release];

? ? }

}

?

2.5 Exceptions and autorelease pools ? 异常和自动释放池

Exceptions are almost always created as autoreleased objects because you don‘t know when they will need to be released. When the autorelease pool is destroyed, all objects in that pool are destroyed also, including the exception.

Exceptions?几乎总是被创作为自动释放因为你不知道什么时候结束。

?

? - (void)myMethod

{

? NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

? NSDictionary *myDictionary =

? ? [[NSDictionary alloc] initWithObjectsAndKeys:@"asdfads", nil];

? @try {

? ? [self processDictionary:myDictionary];

? } @catch (NSException *e) {

? ? @throw;

? } @finally {

? ? [pool release];

? }

}

There‘s a problem when we think about exception handling. We discussed earlier that we can rethrow exceptions in the @catch block, which causes the @finally block to execute before the exception is rethrown.

我们可以rethrow exceptions?在@catch block中。这导致了@finally在异常抛出前执行。

This will cause the local pool to be released before the exception can be delivered, thus turning it into a dreaded zombie exception.

这将导致恐怖的zombie exception.

- (void)myMethod

{

? id savedException = nil;

? NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

? NSDictionary *myDictionary =

? ? [[NSDictionary alloc] initWithObjectsAndKeys:@"asdfads", nil];

? @try {

? ? ? ? [self processDictionary:myDictionary];

? } @catch (NSException *e) {

?? ? ? savedException = [e retain];

?? ? ? @throw;

? } @finally {

?? ? ? [pool release];

?? ? ? [savedException autorelease];

? }

}

By using retain, we saved the exception in the parent pool. When our pool is released, we already have a pointer saved, and when the parent pool is released, the exception will be released with it.

通过retain?,我们保留了这个exception?在parent pool。

?

?

?

?

?

时间: 2024-10-05 19:52:19

Objective-C Memory Management Being Exceptional 异常处理与内存的相关文章

Objective -C Memory Management 内存管理 第一部分

Objective -C Memory Management??内存管理??第一部分 Memory management is part of a more general problem in programming called resource management. 内存管理是资源管理的一部分. Every computer system has finite resources for your program to use. These include memory, open fi

Fixed Partition Memory Management UVALive - 2238 建图很巧妙 km算法左右顶点个数不等模板以及需要注意的问题 求最小权匹配

/** 题目: Fixed Partition Memory Management UVALive - 2238 链接:https://vjudge.net/problem/UVALive-2238 题意:lv 思路:lrjP352. 来自lrj训练指南. n个程序作为左边结点, n*m个结点在右边:由于只要求n个程序在右边能找到的匹配点,km算法可以求解.修改nx,ny的值. if(f[i][j]==-1){ for(int k = 1; k <= n; k++) love[i][j*n+k-

Android Memory Management, OutOfMemoryError

A Android框架强制每个进程的24 MB内存限制.在一些旧的设备,如在G1,限制为16 MB 更低,更重要的是,由位图使用的内存限制.处理图像的应用程序,它是很容易达到此限制,并获得与OOM 异常死亡 的过程:E / dalvikvm堆(12517):1048576字节外部分配这个 过程中过大的E / GraphicsJNI(12517): VM将不会让我们分配1048576字节 / AndroidRuntime(12517):关闭VM / dalvikvm(12517):主题ID = 1

2015.12.21 内存管理(memory management)

Memory Management 1.什么是内存管理? 程序在运行过程中管理内存分配的过程,当需要内存的时候就申请一片内存空间,不需要就释放掉. 2.如何去管理内存 站在分配对象拥有权的角度来操作内存. 3.内存管理的两种办法 a. MRR(Manual Retain Release)手动管理,实现的机制:reference counting(引用计数机制). b. ARC(Auto Reference Counting)自动引用计数,实现机制:系统在程序编译阶段自动添加了释放对象的办法. 4

《modern operating system》 chapter 3 MEMORY MANAGEMENT 笔记

MEMORY MANAGEMENT The part of the operating system that manages (part of) the memory hierarchy is called thememory manager 这章感觉有点多...80 多页..看完都看了两天多,做笔记就更有点不想...有点懒了..但是要坚持下去,可以自己较劲 对于内存的抽象,最简单的抽象就是...没有抽象 和第一次看不一样,把summary放在最前面,对整个mamory management的

Summary of Memory Management Methods

Summary of Memory Management Methods Table 18-1 summarizes the various memory management methods. If you do not enable automatic memory management, then you must separately configure one memory management method for the SGA and one for the PGA. Note:

Automatic Tuning of Memory Management

4.2.2 Automatic Tuning of Memory Management Two memory management initialization parameters, MEMORY_TARGET and MEMORY_MAX_TARGET, enable automatic management of the System Global Area (SGA), Program Global Area (PGA), and other memory required to run

Objective-C Memory Management 内存管理 2

Objective-C Memory Management?内存管理? 2? 2.1 The Rules of Cocoa Memory Management 内存管理规则 (1)When you create an object using new, alloc, or copy, the object has aretain count of 1. You are responsible for sending the object a release or autorelease mess

如何展开Linux Memory Management学习?

Linux的进程和内存是两座大山,没有翻过这两座大山对于内核的理解始终是不完整的. 关于Linux内存管理,在开始之前做些准备工作. 首先bing到了Quora的<How can one really learn Linux Kernel Memory Management?>,这也是大部分初学者,或者说大部分Linux内核开发人员的疑问? 正如Vaishali Thakkar所说,最好的方法是读代码,但是面对这么庞杂的代码,往往是无从下手.Vaishali Thakkar推荐从LSF/MM论