×
Namespaces

Variants
Actions

Tips and Tricks - Symbian C++ Programming

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: lpvalente (08 Aug 2007)
Last edited: hamishwillee (26 Jul 2012)

This article contains tips and tricks that developers have learned during development life cycle. Developers can add their input to this article to make it a useful resource.

Contents

Brainstrom.jpg Tips and Tricks Regarding Project Building

ERROR: RCMake failed:

  • ERROR: RCMake failed: (Resources): rcomp.exe failed with code -1. (Reason: Unknown error)

Modification in MMP

Solution: If you are rebuilding your application, make sure that the emulator is closed.

  • Whenever you make any change in the .MMP file, you need to rebuild/re-import the project to get this change affected.

Brainstrom.jpg Tips and Tricks Descriptor Handling

Always Pass TDes and TDesC Parameters by Reference

  • Always Pass TDes and TDesC Parameters by Reference.When declaring method parameters, you must declare TDesC and TDes as pass-by-reference and not pass-by-value. The reason is that these classes do not contain any actual string data and if you pass them by value, then the rules of C++ mean that static binding is being used and the consequence is that polymorphism won’t work. The end result is that your code compiles but you are not actually passing any data.
void fun(const TDesC aString) // error, should be const TDesC&
{
TBufC<10> buffer(aString); // buffer will contain random data
. . .
}


Declare Literals in .cpp Files.

When using literals, try not to declare them in a header file because all CPP files that include it generate a new copy, leading to code bloat. Instead put your _LITs in the CPP file and have a method return a reference to it:

_LIT(KTxtHelloWorld, "Hello World!");
EXPORT_C const TDesC& CMyClass::GetHelloText() const
{
return KTxtHelloWorld;
}

A more simpler and efficient approach which will reduced the code size as compared to above will be to use globally declared const TPtrC objects. Here is an example

/** Header File globalstrings.h**/
GLREF_D const TPtrC KGlobalString;
 
/***Source File globalstrings.cpp**/
GLDEF_D const TPtrC KGlobalString(_S("Globally Shared String"));
 
/***Usage File Main.cpp**/
#include "globalstrings.h"
...
console->Write(KGlobalString);

Initialize TPtr Class Data Members in constructor.

TPtr does not have a default constructor, so when it is used as a data member of a class, it must be initialized in the constructor initialization list, otherwise a compilation error is generated.

When T object is on the heap

T object is on the heap and has been added to the cleanup stack, the cleanup stack will not call the destructor in the case of a leave as CBase-derived objects get their destructors called by the cleanup stack in case of a leave.If you have a T object on the heap and you need to call its destructor in case of a leave, then you need to use a TCleanupItem.

How to create a static array of literals

To create an const array of unicode descriptors you can simply declare it as an array of TPtrC's and initialize it as

const TPtrC KStringArray[]={
_S("String1"),
_S("String2"),
_S("String3"),
...
};
  • More Info: How to create a static array of literals [1]

Base64, Quoted Printable and UU Encoding and Decoding

  • Base64, Quoted Printable and UU Encoding and Decoding [2]

Brainstrom.jpg Tips and Tricks DLL

How to let a DLL know which application uses it

  • How to let a DLL know which application uses it [3]

Brainstrom.jpg Tips and Tricks File Handling

Data to be flushed to file

  • The buffer and file can be synchronized by calling SyncL(), which

causes the data to be flushed to file:

buf.SynchL() ;

Brainstrom.jpg Tips and Tricks Package File

How to identify a mobile at execution or installation time

  • How to identify a mobile at execution or installation time [4]

Start automatically an application or an exe after its installation

  • Start automatically an application or an exe after its installation[5]

Brainstrom.jpg Tips and Tricks For GUI

MBM file format

  • The MBM file format [6]

Brainstrom.jpg Tips and Tricks On Active Objects

Never call DoCancel() directly

  • Remember you should never call DoCancel() directly because, unlike Cancel(), this doesn’t check if there is an outstanding request before sending a cancel to the service provider.
CMyActive::~CMyActive()
{
Cancel();
// Other cleanup code
...
}

Common mistake declaring an unnecessary TRequestStatus member variable

  • Its Common mistake for developers to declare an unnecessary TRequestStatus member variable in their active object. Don’t do this – instead use the one supplied by the CActive base class. Nor is it necessary to set the iStatus value to KRequestPending after making a call; the framework sets this value in SetActive().

Brainstrom.jpg Tips And Tricks for Debugging

Helpful Links

For more tips please visit this link: Symbian Emulator shortcut keys

  • DBMS Panics [7]

To Debug Draw() function of a control class

A tip to debug Draw() function of a control class

Top 5 common mistakes to cause memory leaks

  • Top 5 common mistakes to cause memory leaks in Symbian C++ applications [8]

System Error

"System Error." while starting the application
When we create an application by modifying an existing application, sometimes we get an error "System error" and right soft key with OK. There is no other information. One of the reason I noticed is that 3rd UID mismatch. It is good to take a look 3rd UID in these files (of course those should match). -mmp file -File where AppDllUid() is implemented -reg.rss file -pkg file

Display the extended panic code in Emulator or Device

  • Display the extended panic code in Emulator or Device [9]

Brainstrom.jpg Symbian:Optimization Tips And Tricks

Overview

This section contains tips and tricks for Symbian C++ Optimization. Experts would try keep this section updated so that its helpful for the beginners. As Symbian is so vast and there are more than one ways to do a thing and sometime it becomes difficult to decide what is the correct path to follow, due to shortage of time. This section at a glance help beginner to decide and follow the correct path.

Helpful Links

  • Common mis-use and abuse of Symbian OS [10]

DrawBitmap() is slower than BitBlt()

  • DrawBitmap() is slower than BitBlt(), so BitBlt() should be used in preference wherever possible

Compress Your Symbian C++ Executables

  • Compress Your Symbian C++ Executables [11]

Brainstrom.jpg Internal links

Brainstrom.jpg External Links For Symbian


Brainstrom.jpg External Links For C/C++

This page was last modified on 26 July 2012, at 09:27.
59 page views in the last 30 days.
×