Exception handling in Symbian OS

/* http://wiki.forum.nokia.com/index.php/Exception_handling_in_Symbian_OS */

Symbian OS applications can achieve efficient exception handling by following the rules below:


Rule 1: Instead of returning an error code when an out-of-resource error occurs, by convention Symbian OS functions should leave, and all leaves should be trapped by a trap harness.


Rule 2: If you allocate memory on the heap, and the pointer to it is an automatic variable (i.e. not a member variable), it should be pushed to the cleanup stack so that it can be deleted when a leave occurs. All objects pushed to the cleanup stack should be popped before they are destroyed.


Rule 3: A C++ constructor must not leave or fail. Therefore, if construction of an object can fail with an out-of-resource error, all the instructions that might fail should be taken out of the C++ constructor and put in a ConstructL() function, which should be called after the C++ constructor has completed.


Contents

<script type=text/javascript> if (window.showTocToggle) { var tocShowText = "show"; var tocHideText = "hide"; showTocToggle(); } </script>

Rule 1: functions that leave, and trap harnesses

Functions that leave

Instead of returning an error code, functions in Symbian OS should leave whenever an out-of-resource error occurs. A leave is a call to User::Leave(), and it causes program execution to return immediately to the trap harness within which the function was executed.

All functions that can leave should have the suffix “L”. This enables programmers to know that the function might leave and therefore to ensure that it is executed within a trap harness.

So

error=doExample() 
if (error!=KErrNone) 
 
{ 
 
// Do some error code 
 
}

becomes

TRAPD(error,doExampleL()); 
if(error!=KErrNone) 
 
{ 
 
// Do some error code 
 
}


In the above example the benefits of using a trap harness are not obvious in either efficiency or readability of the code. However, if the doExample() function were to call another function, which in turn called another, and so on (as is likely to be the case in a real application), the benefits would be considerable: there would be no need to check return values for errors at each level, and this would save many lines of code. new (ELeave)

The possibility of new failing arises so often that in Symbian OS the new operator has been overridden to take a parameter, ELeave. When called with this parameter, the overridden new operator will leave if it fails to allocate the required memory. This is implemented globally, so any class can use new (ELeave).

So,

CSomeObject* myObject = new CSomeObject; 
if (!myObject) User::Leave(KErrNoMemory);

can be replaced by

CSomeObject* myObject = new (ELeave) CSomeObject;

in Symbian OS programs.


The NewL() and NewLC() conventions

By convention, Symbian OS classes often implement the functions NewL() and NewLC().

NewL() creates the object on the heap and leaves if an out-of-memory error occurs.

For simple objects this simply involves a call to new (ELeave). However, for compound objects it can incorporate two-phase construction: see “Rule 3” below.

NewLC() creates the object on the heap, leaving if an out-of-memory error occurs, and pushes the object to the cleanup stack: see “Rule 2” below.

In general, when creating C-class objects, programs should use NewL() if a member variable will point to the object, and NewLC() if an automatic variable will point to it.

However, it is not always advisable to implement NewL() and NewLC() for every class. If NewL() or NewLC() are called from only one place in the application, implementing them will actually use more lines of code than it saves. It is a good idea to assess the need for NewL() and NewLC() for each individual class.

Note: NewL() and NewLC() must be declared as static functions in the class definition. This allows them to be called before an instance of the class exists. They are invoked using the class name. For example:

CSomeObject* myObject=CSomeObject::NewL();


Using a trap harness: TRAP and TRAPD

Symbian OS provides two trap harness macros, TRAP and TRAPD, which are both very similar. Whenever a leave occurs within code executed inside the harness, program control returns immediately to the trap harness macro. The macro then returns an error code which can be used by the calling function.

To execute a function within a trap harness, use TRAPD as follows:

TRAPD(error,doExampleL()); 
if (error !=KErrNone) 
 
{ 
 
// Do some error code 
 
}


TRAP differs from TRAPD only in that the program code must declare the leave code variable. TRAPD is more convenient to use as it declares it inside the macro. So, using TRAP, the above example would become:

TInt error; 
TRAP(error,doExampleL()); 
 
if (error !=KErrNone) 
 
{ 
 
// Do some error code 
 
}


Any functions called by doExampleL() are also executed within the trap harness, as are any functions called by them, and so on: a leave occurring in any function nested within doExampleL() will return to this trap harness. Another trap harness can be used within the first, so that errors are checked at different levels within the application.

Rule 2: using a cleanup stack

Why a cleanup stack is needed

If a function leaves, control returns immediately to the trap harness within which it was called. This means that any automatic variables within functions called inside the trap harness are destroyed. A problem arises if any of these automatic variables are pointers to objects allocated on the heap: when the leave occurs and the pointer is destroyed, the object it pointed to is orphaned and a memory leak occurs.

Example:

TRAPD(error,doExampleL()); 
… 
 
void doExampleL() 
 
{ 
 
CSomeObject* myObject1=new (ELeave) CSomeObject; 
 
CSomeObject* myObject2=new (ELeave) CSomeObject; // WRONG 
 
}


In this example, if myObject1 was new’ed successfully, but there was insufficient memory to allocate myObject2, myObject1 would be orphaned on the heap. Thus, we need some mechanism for retaining any such pointers, so that the memory they point to can be freed after a leave. Symbian OS mechanism for this is the cleanup stack.

Using a cleanup stack

The cleanup stack is a stack containing pointers to all the objects that need to be freed when a leave occurs. This means all C-class objects pointed to by automatic variables rather than instance data.

When a leave occurs, the TRAP or TRAPD macro pops and destroys everything on the cleanup stack that was pushed to it since the beginning of the trap.

All applications have their own cleanup stack, which they must create. (In an EIKON application, this is done for you by the EIKON framework.) Typically, all programs will have at least one object to push to the cleanup stack.

Objects are pushed to the cleanup stack using CleanupStack::PushL() and popped using CleanupStack::Pop(). Objects on the cleanup stack should be popped when there is no longer a chance that they could be orphaned by a leave, which is usually just before they are deleted. PopAndDestroy() is normally used instead of Pop(), as this ensures the object is deleted as soon as it is popped, avoiding the possibility that a leave might occur before it has been deleted, causing a memory leak.

A compound object that owns pointers to other C-class objects should delete those objects in its destructor. Therefore any object which is pointed to by member data of another object (rather than by an automatic variable) does not need to be pushed to the cleanup stack. In fact, it must not be pushed to the cleanup stack, or it will be destroyed twice if a leave occurs: once by the destructor and once by the TRAP macro.

Rule 3: Two-phase construction

Sometimes, a constructor will need to allocate resources, such as memory. The most ubiquitous case is that of a compound C-class: if a compound class contains a pointer to another C-class, it will need to allocate memory for that class during its own construction.

(Note: C-classes in Symbian OS are always allocated on the heap, and always have CBase as their ultimate base class)

In the following example, CMyCompoundClass has a data member which is a pointer to a CMySimpleClass.

Here’s the definition for CMySimpleClass:

class CMySimpleClass : public CBase 
{ 
 
public: 
 
CMySimpleClass(); 
 
~CMySimpleClass(); 
 
… 
 
private: 
 
TInt iSomeData; 
 
};


Here’s the definition for CMyCompoundClass:

class CMyCompoundClass : public CBase 
{ 
 
public: 
 
CMyCompoundClass(); 
 
~CMyCompoundClass(); 
 
… 
 
private: 
 
CMySimpleClass * iSimpleClass; // owns another C-class 
 
};


Consider what you might be tempted to write as the constructor for CMyCompoundClass:

CMyCompoundClass::CMyCompoundClass() 
{ 
 
iSimpleClass=new CMySimpleClass; // WRONG 
 
}


Now consider what happens when you new a CMyCompoundClass, e.g.:

CMyCompoundClass* myCompoundClass=new (ELeave) CMyCompoundClass;


With the above constructor, the following sequence of events occurs:

1. Memory is allocated for the CMyCompoundClass 2. CMyCompoundClass’s constructor is called 3. The constructor news a CMySimpleClass and stores a pointer to it in iSimpleClass 4. The constructor completes But, if stage 3 fails due to insufficient memory, what happens? There is no way to return an error code from the constructor to indicate that the construction was only half complete. new will return a pointer to the memory that was allocated for the CMyCompoundClass, but it will point to a partially-constructed object.

If we make the constructor leave, we can detect when the object was not fully constructed, as follows:

CMyCompoundClass::CMyCompoundClass() // WRONG 
{ 
 
iSimpleClass=new (ELeave) CMySimpleClass; 
 
}


However, this is not a viable method for indicating that an error has occurred, because we have already allocated memory for the CMyCompoundClass. A leave would destroy the pointer (this) to the allocated memory, and there would be no way to free it, resulting in a memory leak. The solution is to allocate all the memory for the components of the object, after the compound object has been initialized by the C++ constructor. By convention, in Symbian OS this is done in a ConstructL() function.

Example

void CMyCompoundClass::ConstructL() // RIGHT 
{ 
 
iSimpleClass=new (ELeave) CMySimpleClass; 
 
}


The C++ constructor should contain only initialization that cannot leave (if any):

CMyCompoundClass::CMyCompoundClass() // RIGHT 
{ 
 
… // Initialization that cannot leave 
 
}


The object is now constructed as follows:

CMyCompoundClass* myCompoundClass=new (ELeave) CMyCompoundClass; 
CleanupStack::PushL(myCompoundClass); 
 
myCompoundClass->ConstructL(); // RIGHT


This can be encapsulated in a NewL() or NewLC() function for convenience. Implementing two-phase contruction in NewL() and NewLC()

If a compound object has a NewL() function (or NewLC()), this should contain both stages of construction. After the first stage, the object should be pushed to the cleanup stack before ConstructL() is called, in case ConstructL() leaves.

Example:

CMyCompoundClass* CMyCompoundClass::NewLC() 
{ 
 
CMyCompoundClass* self=new (ELeave) CMyCompoundClass; 
 
CleanupStack::PushL(self); 
 
self->ConstructL(); 
 
return self; 
 
} 
 
CMyCompoundClass* CMyCompoundClass::NewL() 
 
{ 
 
CMyCompoundClass* self=new (ELeave) CMyCompoundClass; 
 
CleanupStack::PushL(self); 
 
self->ConstructL(); 
 
CleanupStack::Pop(); // self 
 
return self; 
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在信号处理领域,DOA(Direction of Arrival)估计是一项关键技术,主要用于确定多个信号源到达接收阵列的方向。本文将详细探讨三种ESPRIT(Estimation of Signal Parameters via Rotational Invariance Techniques)算法在DOA估计中的实现,以及它们在MATLAB环境中的具体应用。 ESPRIT算法是由Paul Kailath等人于1986年提出的,其核心思想是利用阵列数据的旋转不变性来估计信号源的角度。这种算法相比传统的 MUSIC(Multiple Signal Classification)算法具有较低的计算复杂度,且无需进行特征值分解,因此在实际应用中颇具优势。 1. 普通ESPRIT算法 普通ESPRIT算法分为两个主要步骤:构造等效旋转不变系统和估计角度。通过空间平移(如延时)构建两个子阵列,使得它们之间的关系具有旋转不变性。然后,通过对子阵列数据进行最小二乘拟合,可以得到信号源的角频率估计,进一步转换为DOA估计。 2. 常规ESPRIT算法实现 在描述中提到的`common_esprit_method1.m`和`common_esprit_method2.m`是两种不同的普通ESPRIT算法实现。它们可能在实现细节上略有差异,比如选择子阵列的方式、参数估计的策略等。MATLAB代码通常会包含预处理步骤(如数据归一化)、子阵列构造、旋转不变性矩阵的建立、最小二乘估计等部分。通过运行这两个文件,可以比较它们在估计精度和计算效率上的异同。 3. TLS_ESPRIT算法 TLS(Total Least Squares)ESPRIT是对普通ESPRIT的优化,它考虑了数据噪声的影响,提高了估计的稳健性。在TLS_ESPRIT算法中,不假设数据噪声是高斯白噪声,而是采用总最小二乘准则来拟合数据。这使得算法在噪声环境下表现更优。`TLS_esprit.m`文件应该包含了TLS_ESPRIT算法的完整实现,包括TLS估计的步骤和旋转不变性矩阵的改进处理。 在实际应用中,选择合适的ESPRIT变体取决于系统条件,例如噪声水平、信号质量以及计算资源。通过MATLAB实现,研究者和工程师可以方便地比较不同算法的效果,并根据需要进行调整和优化。同时,这些代码也为教学和学习DOA估计提供了一个直观的平台,有助于深入理解ESPRIT算法的工作原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值