Custom deleter for unique_ptr

Sockets… why they are not included in the C++ standard? We had a lot of trouble wrapping BSD sockets C API into our C++ library. Recently, analyzing our application with valgrind I discovered memory leaks in one of the socket wrappers.

The raw pointer to C struct addrinfo was wrapped into unique pointer:

class IpSocket
    std::unique_ptr<struct addrinfo> addresssInfo_;

The memory was allocated as

int result = getaddrinfo(host, post, &hints, &aInfo);
addressInfo_ = std::unique_ptr<struct addrinfo>(aInfo);

In our case, the IpSocket destructor will try to free memory by invoking the default deleter (delete operator) over object stored in addressInfo_. That is the problem, the getaddrinfo() allocates the memory using C function malloc and hence we need to invoke free() function to release this memory.

The manpage for getaddrinfo() says that the freeaddrinfo() function should be used to free memory. The quick look into freeaddrinfo() source confirms that the function does what we expected

void freeaddrinfo (struct addrinfo *ai)
    struct addrinfo *p;	
    while (ai != NULL)
        p = ai;
        ai = ai->ai_next;
        free (p->ai_canonname);
        free (p);

In order to force our unique_ptr use freeaddrinfo we need to define custom deleter and put it as a second template argument.

class IpSocket
    struct AddressInfoDeleter
            void operator()(struct addrinfo *p) { freeaddrinfo(p); }

    std::unique_ptr<struct addrinfo, AddressInfoDeleter> addressInfo_;

Now the memory is released correctly.

Black Magic Probe

I’ve been a long time an AVR user, but the technology is going ahead and it is time to use something more modern. Recently I started to look closer on the ARM microcontrollers and finally, I’ve selected STM32 Cortex-M3 as my base unit. For me, the perfect development environment would be QtCreator + GNU toolchain (since I use this environment on my work and I do not like Java-based IDEs). This post will be devoted to the hardware enabling on-chip debugging the STM32 applications using gdb.

In order to be able to use gdb, one need some adapter providing the interface between the debugger and ARM chip. I have considered several popular alternatives  ST-Link from ST-Microelectronics, J-Link from Segger and Black Magic Probe from Black Sphere Technologies.

J-Link is a cool tool but it is really expensive. ST-Link is a nice and cheap tool and is supported by BareMetal plugin in QtCreator. In fact, gdb can’t work directly with ST-Link and you will need intermediate software like openocd to communicate with this dongle.

Black Magic Probe (BMP) implements gdb protocol natively and you do not need any additional software. BMP supports JTAG as well as SWD protocols, it has bootloader for updating firmware over USB. This dongle is also quite expensive for the hobbyist. Hopefully, the developer provides the schematics and firmware under the GNU Public License 3, and hence it is possible to create your own BMP use it for developing the commercial products and even sell the BMP clones.  I’ve decided to build my own BMP with some minor modifications. First of all, I’ve selected the original version since it contains full-sized JTAG header. I’ve made the following modifications with respect to the original schematics: the mini-USB was replaced by USB-B one, the SWD header was added to simplify the flashing BMP chip.

PCB layout


Final PCB

Now it is time to build and flash the firmware:

git clone
cd blakmagic
git submodule init
git submodule update
make -j4 PROBE_HOST=native

This will create a bootloader file and BMP firmware file. In order to make debugging dongle one need to have debugging dongle 🙂 Connect your favorite programmer to the SWD head on the BMP and flash it. Hier the ST-Link is used:

st-flash erase
st-flash --flash=0x20000 write blackmagic_dfu.bin 0x8000000
st-flash --flash=0x20000 --reset write blackmagic.bin 0x8002000

The second command flashes the bootloader and the third one downloads the BMP firmware. Now it is time to connect BMP to your PC via USB an check if it is recognized. Use lsub command to see USB devices attached. If everything is fine you will see the following line:

ID 1d50:6018 OpenMoko, Inc.

then the device is recognized. To test gdb protocol just start gdb and issue the following commands:

(gdb) target extended-remote /dev/ttyACM0
(gdb) monitor version

Black Magic Probe (Firmware v1.6.1-269-g7cb1858) (Hardware Version 0)
Copyright (C) 2015 Black Sphere Technologies Ltd.
License GPLv3+: GNU GPL version 3 or later <>

In the next article, I will show how to configure BMP with QtCreator.

Colorized man pages howto

1) Create a file ~/.less_termcap with color settings

export LESS_TERMCAP_mb=$(tput bold; tput setaf 2) # green
export LESS_TERMCAP_md=$(tput bold; tput setaf 6) # cyan
export LESS_TERMCAP_me=$(tput sgr0)
export LESS_TERMCAP_so=$(tput bold; tput setaf 3; tput setab 4) # yellow on blue
export LESS_TERMCAP_se=$(tput rmso; tput sgr0)
export LESS_TERMCAP_us=$(tput smul; tput bold; tput setaf 7) # white
export LESS_TERMCAP_ue=$(tput rmul; tput sgr0)
export LESS_TERMCAP_mr=$(tput rev)
export LESS_TERMCAP_mh=$(tput dim)
export LESS_TERMCAP_ZN=$(tput ssubm)
export LESS_TERMCAP_ZV=$(tput rsubm)
export LESS_TERMCAP_ZO=$(tput ssupm)
export LESS_TERMCAP_ZW=$(tput rsupm)
export GROFF_NO_SGR=1         # For Konsole and Gnome-terminal

2) Add the following lines to your .bashrc

[[ -f ~/.less_termcap ]] && . ~/.less_termcap

3) Reload bash settings with

source ~/.bashrc

And now you should be able to see man pages in color.

Add source code syntax highlighting for less

It is about how to get colorized less output for source code.

This howto is written with respect to Ubuntu 17.10.

1) Install source-highlight

sudo apt-get install source-highlight

2) Look for lesspipe

dpkg -L libsource-highlight-common | grep lesspipe

My one is in /usr/share/source-highlight/ folder

3) add two following lines in your .bashrc

export LESSOPEN="| /usr/share/source-highlight/ %s"
export LESS=' -R '

Happy lessing!

Mounting Blackberry 10 device using CIFS

Blackberry 10 devices

// /media/Playbook cifs user=playbook,password=<your_passord>,rw,iocharset=utf8,file_mode=0777,dir_mode=0777,user,noauto
// /media/Z30 cifs user=BlackBerry,password=slytvyn,rw,iocharset=utf8,file_mode=0777,dir_mode=0777,user,noauto
// /media/Z30_sdcard cifs user=BlackBerry,password=<your_password>,rw,iocharset=utf8,file_mode=0777,dir_mode=0777,user,noauto