Failed to start advertising, err -12 (-ENOMEM)

Hi,

I'm working on an application that combines both central and peripheral roles. My 'gateway' should connect to 2 peripherals and sometimes start advertising to make allow connecting to another central (called 'central' going forward). I seem to be encountering some issues when the central disconnects from the gateway (sometimes initiated by the gateway, sometimes initiated by the central). When I want to restart advertising on the gateway, I get an error -12 (-ENOMEM) because bt_conn_add_le returns NULL. I tried increasing my CONFIG_BT_MAX_CONN and CONFIG_BT_CTLR_SDC_PERIPHERAL_COUNT, and while this seems to give me a couple of extra tries, eventually it starts failing. I retry advertising every second when it fails, but it just keeps failing. I logged all the conn objects by using bt_conn_foreach and printing the state they're in, and it seems that I keep adding some conn references that are in disconnected state.

Is there some cleaning up of these conn objects necessary? I was under the impression that these would be cleared automatically after calling bt_conn_unref. I tried calling it once more in the disconnected callback, but that didn't help. I then enabled the LOG statements in the bt_conn_unref and bt_conn_ref to see what the values of the ref are, and it seems that it is going negative even. I see that there is an assert checking for this, but it seems that I don't have this enabled. So I'm wondering, is a negative ref a reason to keep the conn object alive? Or is there another reason that could explain why my connection objects keep filling up?

Alternatively, is there a way to start advertising without creating a new conn and reusing an older one?

All this is with ncs v2.5.0 on an nRF52840 (from the nRF9160DK).

Best,

Wout

Related