Memory Management

Avoiding Thread Memory Leaks

To avoid thread memory leaks on the device, your application needs to call SDL_WaitThread for each of its spawned threads. One way of handling this is to have a thread put itself on a queue before it exits and then have the main thread periodically call SDL_WaitThread on queue items.

Handling Unaligned Memory Accesses

The device's kernel traps any unaligned memory accesses. When this happens, it writes an entry to the system log, an operation which takes a fair amount of CPU time. Consequently, even though your application continues running, it may be very slow. To avoid this, you should ensure that all read and write accesses are word (32 bits) aligned. If you suspect unaligned memory accesses are happening, check the system log.

To check the system log for unaligned memory access messages:

  1. Log into the device.
  2. Run the dmesg (display system message buffer) command.

You could see a number of Alignment... messages similar to this:

[67585.050000] Alignment trap: appname (13587) PC=0x000bdafc Instr=0xe1d2c0b1
Address=0x01670b2f FSR 0x001 

If there are too many messages and you are having trouble finding the ones about alignment, pipe the output from dmesg to the grep program:

[root@palm-webos-device:/# dmesg | grep "Alignment" 

To track down unaligned memory accesses in your application:

  1. Log into the device.

  2. Run the following from the command line:

    [root@palm-webos-device:/# echo 5 > /proc/cpu/alignment     
    
    

    This causes your application to crash whenever an unaligned access occurs.

  3. Build your application with the -g option (produces debugging information in the webOS native format) and start it on the device using GDB.

  4. When access occurs, get a stack crawl of the offending location by running where at the command line:

    root@palm-webos-device:/# gdb appname
    (gdb) run
    ...
    Program received signal SIGBUS, Bus error.
    Switching to Thread 0x36001e70 (LWP 13719)
    0x0010d0f8 in operator func
    (gdb) where
    ...