1. quadratic structure
In C ++, when the resource allocation fails during object initialization, the object knowledge is partially initialized and the Destructor is not called, which may cause resource leakage. To ensure that resources are not leaked, you can perform secondary construction. Some resources that may fail to be allocated can be placed in a Construct function. Note: this should be a reference for Symbian .)
2. Handling Method
Compared with the standard C ++, bada's processing methods work very differently. To best encapsulate everything, it is done by methods.
In bada, data corruption or device failure caused by data corruption is not possible because direct access to data is not possible.
Data access is restricted to prevent malware from exploiting security vulnerabilities such as buffer overflow.
3. Exception Handling
Bada's error and exception handling are also different from standard C ++. Bada uses error results to replace C ++'s exception handling, because C ++'s exception handling takes a lot of running time and space.
All Exception Handling Methods in bada have a return value of the result type capture, and the result type is unsigned long. The E_SUCCESS result indicates that the method returns successfully, and all other returned results fail.
A. exception detection:
A. The function returns a result:
For example:
- result r = E_SUCCESS;
- ...
-
- r = list.Construct(...);
- if (r != E_SUCCESS) // identical to 'if (IsFailed(r))'
- {
- // Process the error condition.
- }
B. The function assigns a value to the result or returns null:
For example:
- pObj = list.GetAt(...);
- if (GetLastResult() != E_SUCCESS) // or 'if (pObj == null)'
- {
- // Process the error condition.
- }
C. If fails to jump to catch:
- r = pObj2->Construct(..);
- TryCatch(r == E_SUCCESS, , "[%s] Service could not be initialized.",
- GetErrorMessage(r));
- ...
- CATCH:
- delete pObj1;
- delete pObj2;
- return;
B. Exception Handling:
A. Use goto CATCH for processing:
- result r = E_SUCCESS;
- ...
-
- r = pList->Construct(...);
- TryCatch(r == E_SUCCESS, delete pList, "[%s] Propagated.", GetErrorMessage(r));
- ...
- CATCH:
- SetLastResult(r);
- return null;
B. Try to put back E_SUCCESS:
- r = list.Construct(...);
- TryReturn(r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r);
C. Return a null value:
- r = list.Construct(...);
- TryReturn(r == E_SUCCESS, null, "[%s] Propagated.", GetErrorMessage(r);
D. convert a wrong environment to another wrong environment:
- r = list.indexOf(...);
- TryReturn(r == E_SUCCESS, E_INVALID_ARG, "'%s' converted to [E_INVALID_ARG].",
- GetErrorMessage(r));
4. Memory Processing:
The memory in bada is managed by the ownership policy. It is the responsibility of ownership to delete the dynamically applied memory and avoid Memory leakage.
Proprietary ownership means that ownership cannot be shared. There are two rules for obtaining ownership.
1> the new operator obtains the ownership of the allocated space.
2> ownership can be transferred but cannot be shared.
Figure 1
5. Application debugging:
Bada provides many macro commands to help you Debug:
1> Assert macro commands:
The Assert macro command is used to test whether the conditions are true. If the conditions are not true, kill the process and they are not compiled into the release version.
AppAssertioncondition)
This is used to check whether the program has a logical error. If an error is returned, the current process is killed.
For example:
- result
-
- MyClass::DoSomething(void)
-
- {
-
- result r = E_SUCCESS;
-
- r = mutex.Acquire();
-
- // do something
-
- r = mutex.Release();
-
- AppAssertion(r == E_SUCCESS); // Process dies if false.
-
- return r;
-
- }
-
- AppAsserttionfcondition, message)
This is used to check whether the program has a logic error. If an error is returned, the current process is killed and a message is displayed on the console.
For example:
- result
-
- MyClass::DoSomething(void)
-
- {
-
- result r = E_SUCCESS;
-
- r = mutex.Acquire();
-
- // do something
-
- r = mutex.Release();
-
- // If false, console prints "Mutex Release Failed"
-
- // and the process is killed.
-
- AppAssertionf(r == E_SUCCESS, "Mutex Release Failed");
-
- return r;
-
- }
Information that may be displayed on the console:
Log macro command:
AppLog (message)
AppLogDebugmessage)
AppLogExceptionmessage)
AppLog allows you to output arbitrary information. AppLogDebug and AppLogException work in the same way. Information is displayed in the console or file.
For example:
- Bool
-
- MyEngine::Init(int value)
-
- {
-
- AppLogDebug("Invoked with value: %d", value);
-
- // Do initialize.
-
- if (something_wrong) // You can use Try family macros instead.
-
- {
-
- AppLogException("Something terrible happened.");
-
- Return false;
-
- }
-
- AppLog("Initialization successful.");
-
- AppLogDebug("Exit.");
-
- return true;
-
- }
Try macro command:
The Try macro command simulates the try-catch of the Standard C ++. Unlike Assert, try not to kill the process.
TryCatchcondition, cleanup, message)
TryCatch detection condition. If it fails, print a piece of information, evaluate a cleanup expression, and then gotoCATCH:
For example:
- const A*
-
- MyClass::DoSomething(const mchar* pValue)
-
- {
-
- result r = E_SUCCESS;
-
- // Do something...
-
- // If pValue is null, print "pValue == null" to the
-
- // console and return E_INVALID_ARG.
-
- TryCatch(pValue != null, r = E_INVALID_ARG, "pValue == null");
-
- SetLastResult(E_SUCCESS);
-
- return _pValue;
-
- CATCH:
-
- SetLastResult(r);
-
- return null;
-
- }
TryReturn (condition, value, message)
If the condition is incorrect, the message is output, and the value is returned.
TryReturnVoid (conditiong, message)
If the conditions are incorrect, print a message.