Wednesday, December 16, 2009

Checking for NULL pointer before deleting or calling free()

Today after reading two posts (references given at the end) from Stack Overflow I have got a better understanding of how dynamically allocated memory should be freed and why in that way.

Earlier I knew only that pointer should be checked against NULL before freeing memory, otherwise catastrophic disaster may occur because it may happen that an already freed memory is being freed again. So I always put it like:

if (pointer == NULL) {
    free(pointer); // or "delete pointer;" in C++
}

Later while working with files in C, I came to realize that freeing memory in this fashion does not make the pointer NULL. So dangling pointer surfaces in this context. I added another line:

pointer = NULL;

However, freeing a NULL pointer is not a problem at all now because according to ANSI standard, free() function does nothing when the passed pointer is NULL.

For the sake of being cautious and avert non-standard compilers from causing havoc to your program, you may check for NULL pointer. Otherwise, it all boils down to only the following 2 lines:

delete pointer;
pointer = NULL;


References:

(1) checking for NULL before calling free
(2) Is there any reason to check for a NULL pointer before deleting?

Tuesday, December 15, 2009

BlackBerry application facing HTTP connection problem on simulator

Before testing an application that uses HTTP connection on BlackBerry simulator, I often forget to enable Mobile Data Service (MDS) with the simulator and I find my application not working as expected.

So if you encounter problem with your application failing to establish HTTP connection, check your intended simulator's MDS setting.

To enable MDS setting,

1. From JDE, go to Edit -> Preferences
2. Select Simulator tab
3. Select General tab under Simulator tab
4. Check "Launch Mobile Data Service (MDS) with Simulator"

Tuesday, December 08, 2009

How to clean or reset BlackBerry simulator

For a veteran BlackBerry developer, it is not unusual to get his BlackBerry simulator crammed with many icons of applications he developed over the years. At one point, when he needs to scroll deep down the screen to find his intended application icon, it becomes really annoying. He then wants to remove application icons from the simulator.

Another kind of need may arise in case of an application which requires the simulator reset its file system each time the simulator is launched.

I have two versions of the same application with same application icon installed on simulator. I wanted to clear everything up and to test one particular version. But I did not know how to do it. I then googled and found the following solutions.

Solution #1: In JDE, go to File -> Erase simulator file

Deletes the dmp files from the simulator directory but does not remove the application icons.

Solution #2: From command line, run clean.bat located at simulator sub-directory under JDE installation directory such as C:\Program Files\Research In Motion\BlackBerry JDE 4.3.0\simulator

Deletes your applications' files and dmp files from the simulator directory. So the icons are removed!

Solution #3: Delete the following files located at the simulator directory such as C:\Program Files\Research In Motion\BlackBerry Device Simulators 4.2.2\4.2.2.114 (8300-Vodafone):

* dmp files
* debug, cod, csl, cso, jar files of your applications

This works when the installed simulator does not have the solution #2 i.e. clean.bat is absent in the simulator's directory.

Solution #4: If you're using the JDE and wants your simulator reset each time you run it, go to Edit->Preferences; Simulator tab; Memory sub-tab, and check "Reset the filesystem on startup"