npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mbed-find-dangling-pointers

v1.0.3

Published

Find un-free'd pointers in Mbed OS applications

Downloads

2

Readme

Find dangling pointers

Memory on microcontrollers is scarce, so finding and closing memory leaks is crucial. This script helps you detect dangling pointers in Mbed OS 5 applications.

Enabling memory tracing

First, enable memory tracing via:

  1. In mbed_app.json under macros, set:

    {
        "macros": ["MBED_MEM_TRACING_ENABLED"]
    }
  2. In your application, call:

    mbed_mem_trace_set_callback(mbed_mem_trace_default_callback);
  3. Connect a serial monitor to your device - probably set a high baud rate to not slow down the application too much.

Using this script

  1. Install the script through npm.

    $ npm install mbed-find-dangling-pointers -g
  2. Save the complete serial output of your application to a file, then run:

    $ mbed-find-dangling-ptrs serial-log-file.txt path-to-your-elf-file.elf

    Note: You can find the .elf file in BUILD/TARGET_NAME/GCC_ARM/.

  3. This will give you the dangling pointers:

    Extracting symbols from example/example.elf
    Extracting symbols OK
    
    Found 5 dangling pointers
    
    --------------------------------------------------
    5 dangling pointers (total: 3840 bytes): [ 0x200036c0 (768), 0x200039d0 (768), 0x20003ce0 (768), 0x20003ff0 (768), 0x20004300 (768) ]
    
        int main() {
            print_memory_info();
            mbed_mem_trace_set_callback(mbed_mem_trace_default_callback);
    
            while (true) {
                wait(2.0);
    
                void *ptr1 = malloc(512);
    >>>         void *ptr2 = calloc(768, 1);
                void *ptr3 = (void*)new DigitalOut(LED1);
    
                // Grab the heap statistics
                mbed_stats_heap_t heap_stats;
                mbed_stats_heap_get(&heap_stats);
                printf("Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
    
                // Forget to free a pointer
                free(ptr1);
                free(ptr3);

    The line that is marked with >>> is the line where the allocation happened.

Example program

#include "mbed.h"
#include "mbed_mem_trace.h"

DigitalOut led1(LED1);

void print_memory_info() {
    // allocate enough room for every thread's stack statistics
    int cnt = osThreadGetCount();
    mbed_stats_stack_t *stats = (mbed_stats_stack_t*) malloc(cnt * sizeof(mbed_stats_stack_t));

    cnt = mbed_stats_stack_get_each(stats, cnt);
    for (int i = 0; i < cnt; i++) {
        printf("Thread: 0x%lX, Stack size: %lu / %lu\r\n", stats[i].thread_id, stats[i].max_size, stats[i].reserved_size);
    }
    free(stats);

    // Grab the heap statistics
    mbed_stats_heap_t heap_stats;
    mbed_stats_heap_get(&heap_stats);
    printf("Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
}

int main() {
    print_memory_info();
    mbed_mem_trace_set_callback(mbed_mem_trace_default_callback);

    while (true) {
        wait(2.0);

        void *ptr1 = malloc(512);
        void *ptr2 = calloc(768, 1);
        void *ptr3 = (void*)new DigitalOut(LED1);
        void *ptr4 = malloc(256);

        ptr4 = realloc(ptr4, 512);

        // Grab the heap statistics
        mbed_stats_heap_t heap_stats;
        mbed_stats_heap_get(&heap_stats);
        printf("Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);

        // Forget to free a pointer
        free(ptr1);
        free(ptr3);
        free(ptr4);
    }
}