How to avoid common errors and make program efficient.

转载 2007年10月15日 20:48:00
How to avoid common errors and make program efficient.

From Forum Nokia Wiki

Jump to: navigation, search

Following are the general tips for avoiding unnecssary and common errors,

and make your program efficient.


Contents

[hide]

[edit] KERN-EXEC 3

Kern-Exec 3 crashes are often caused due to stack corruption or stack Overflow, prefer the use of heap to the stack. Be aware that recursive functions can eat the stack at runtime — this will lead to a Kern-Exec 3 panic.


[edit] Some common errors for application panic

• Failure to have properly added a non-member, heap-allocated variable to the Cleanup Stack. • The ‘double delete’ – e.g. failure to correctly Pop() an already destroyed item from the Cleanup Stack, causing the stack to try to delete it again at a later time. • Accessing functions in variables which may not exist in your destructor;

e.g.







The above statement should always be coded as










• Putting member variables on the Cleanup Stack – never do this, just delete them in your destructor as normal.


[edit] Use CleanupClosePushL()

Always use CleanupClosePushL() with R classes which have a Close() method. This will ensure they are properly cleaned up if a leave occurs.

For example:








[edit] HBufC

Always set member HBufC variables to NULL after deleting them. Since HBufC allocation (or reallocation) can potentially leave, you could find yourself in a situation where your destructor attempts to delete an HBufC which no longer exists.


You don’t need to use HBufC::Des() to get into an HBufC. All you have to do is dereference the HBufC with the * operator – this is particularly relevant, for example, when passing an HBufC as an argument to a method which takes a TDesC& parameter.


[edit] _L() Macro

Dont use the _L() macro in your code. you should prefer _LIT() instead. The problem with _L() is that it calls the TPtrC(const TText*) constructor, which has to call a strlen() function to work out the length of the string. While this doesn’t cost extra RAM, it does cost CPU cycles at runtime. By contrast, the _LIT() macro directly constructs a structure which is fully initialized at compile time, so it saves the CPU overhead of constructing the TPtrC.

Alternatively, you can use following macros instead of _L():





It gets rid of strlen, but still TPtrC constructors are not inline functions.

[edit] TRAP

If you have cause to use a TRAP of your own, do not ignore all errors. A common coding mistake is:







This means all other error codes are ignored. If you must have a pattern like the above, leave for other errors:










[edit] Cleanup Stack

Do not wait to PushL() things on to the Cleanup Stack. Any newly allocated object (except member variables) should be added to the stack immediately. For example, the following is wrong:







because the allocation of Object2 could fail, leaving Object1 ‘dangling’ with no method of cleanup. The above should be:










[edit] Don't push objects on the cleanup stack twice

Always remember that functions with a trailing C on their name automatically put the object on the Cleanup Stack. You should not push these objects onto the stack yourself, or they will be present twice. The trailing C functions are useful when you are allocating non-member variables.


[edit] Two-phase construction

Two-phase construction is specially designed to avoid memory leaks, It is essential that you implement this design pattern to avoid memory leaks in your code.For each line of code you write, a good question to ask yourself is ‘Can this line leave?’. If the answer is ‘Yes’, then think: ‘Will all resources be freed?’.


[edit] Descriptors as function parameters

When using descriptors as function parameters, use the base class by default. In most cases, pass descriptors around as a const TDesC&. For modifiable descriptors use TDes&.



[edit] When using Active Objects, be careful of the following things

• There is no need to call TRAP() inside RunL(). The Active Scheduler itself already TRAPs RunL() and calls CActive::RunError()after a leave.
• To this end, you should implement your own RunError() function to handle leaves from RunL().
• Keep RunL() operations short and quick. A long-running operation will block other AOs from running.
• Always implement a DoCancel() function and always call Cancel() in the AO destructor.

[edit] Ensure your application responds to system shutdown events

It is vital that you respond to EEikCmdExit (and any platform-specific events, for example EAknSoftkeyBack and EAknCmdExit on Series 60) in your AppUi::HandleCommandL() method.




[edit] Make use of the Active Object framework wherever possible

Tight polling in a loop is highly inappropriate on a battery powered device and can lead to significant power drain.


[edit] Program compiles for WINS but not for ARMI

Your program compiles for WINS even runs on the emulator but gives errors during ARMI build. Possible reason for this is you have left a space in the header file name i.e. instead of #include "headerfile.h" you've typed #include "headerfile.h ". Just remove that space and the compile again.


[edit] HTTP Posts

In case of HTTP posts with forms always remember to delete the instance of CHTTPFormEncoder. If it is a php script on your server the Form elements are read bottom to top whereas python script reads the Form elements top to bottom. So what may seem to work on php wont work if the scripting language is changed to python. So always:









How To Avoid Sudden Outburst Of Backup Shell Script / Program Disk I/O

http://www.cyberciti.biz/tips/linux-set-io-scheduling-class-priority.html How To Avoid Sudden...
  • onlyForCloud
  • onlyForCloud
  • 2015年07月27日 15:09
  • 291

wince6.0 don't know how to make**错误

U1073: don't know how to make U1073: don't know how to make 'C:\WINCE500\platform\common\lib\ARMV4I...
  • xgx198831
  • xgx198831
  • 2012年11月21日 20:03
  • 4824

How to deal with telecom fraud?

How to deal with telecom fraud? These days telecom fraud occurs frequently by means of telephones...
  • lijia111111
  • lijia111111
  • 2017年12月06日 21:05
  • 165

《java how to program》习题全解----正在创作中

java how to program 是一本好书,我个人认为其中最精彩的部分是练习题,非常基础和实用,如果能把其中所有的习题全部做完,水平一定会有极大的提高。但是,不管水平如何,总会有一些题很令人头...
  • bighao
  • bighao
  • 2006年11月26日 14:46
  • 1710

C++ How To Program整理(10)

template   void arrayPrint(const T* const array,int count)   {     for(int i=0;i     {       c...
  • ZHANGJIXIN
  • ZHANGJIXIN
  • 2012年07月13日 18:07
  • 694

PB50编辑类似“NMAKE : fatal error U1073: don't know how to make 'tbtcore'”的错误解决

PB50编辑类似“NMAKE : fatal error U1073: don't know how to make 'tbtcore'”的错误解决  前提原因:打了platform Builder...
  • jiyingyijiye
  • jiyingyijiye
  • 2011年09月02日 11:08
  • 2175

[分享] C++ How to Program, 8/E (C++大学教程 第8版) 英文版PDF下载

[分享] C++ How to Program, 8/E (C++大学教程 第8版) 英文版PDF下载 下载:[晓山青青]Cpp.How.to.Program,8th.Editi...
  • u011501221
  • u011501221
  • 2013年12月20日 21:01
  • 3509

How to make Decision Effectively

How to make Decision Effectively In theory, making decisions sounds really easy: you are given tw...
  • shenghuiping2001
  • shenghuiping2001
  • 2013年12月03日 19:22
  • 822

C++ How To Program整理(9)

@polymorphism,使能够进行通用化编程,在处理同一个类的(继承)层次结构下的对象时,像是基类的对象一样。 @同一个消息发送给不同的对象,会有不同的处理结果,此所谓多态性。 @被调用的功能...
  • ZHANGJIXIN
  • ZHANGJIXIN
  • 2012年07月12日 19:51
  • 753

《java how to program》第三版1-6章大部分习题答案[原创]

《java how to program》第三版1-6章大部分习题答案[原创]下载地址 http://download1.csdn.net/down3//20070126/26183744138.ra...
  • bighao
  • bighao
  • 2007年01月26日 18:43
  • 1363
收藏助手
不良信息举报
您举报文章:How to avoid common errors and make program efficient.
举报原因:
原因补充:

(最多只允许输入30个字)