Skip to content

Bug - Memory leak in Pico W http example #605

@cirrith

Description

@cirrith

I'm still relatively new to Pico and was trying to connect my Pico 2 W to my Wi-Fi so that it could act as a simple remote sensor.

I believe the provided example pico_w/wifi/http_client/example_http_client_util.c has a memory leak, which prevents it from making more than a few requests before exhausting the allocated protocol control blocks (PCB).

This issue wouldn't have appeared in the provided code as it only makes a few requests before exiting. However, if the example is adapted to a real project, the bug would prevent boards from making more than ~PBUF_POOL_SIZE number of requests.

A pbuf_free(p); should be added to the receiver function to free the buffer after use. Here is the fixed version:

err_t http_client_receive_print_fn(__unused void *arg, __unused struct altcp_pcb *conn, struct pbuf *p, err_t err) {
    HTTP_INFO("\ncontent err %d\n", err);
    u16_t offset = 0;
    while (offset < p->tot_len) {
        char c = (char)pbuf_get_at(p, offset++);
        HTTP_INFOC(c);
    }
    pbuf_free(p);
    return ERR_OK;
}

Optionally, it can be added to the internal_recv_fn function to make it easier to swap in a custom receiver.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions