Skip to content

assertion "pbuf_free: p->ref > 0" failed: file #2685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Chriserke opened this issue Apr 18, 2019 · 11 comments
Closed

assertion "pbuf_free: p->ref > 0" failed: file #2685

Chriserke opened this issue Apr 18, 2019 · 11 comments

Comments

@Chriserke
Copy link

Chriserke commented Apr 18, 2019

Error

Why am i getting this error and how would i go about fixing it?

#include "WiFi.h"
#include "AsyncUDP.h"

const char* ssid = "Chrisiaan wifi";
const char* password =  "Buijse1234";

AsyncUDP udp;

void setup()
{
    Serial.begin(115200);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    if (WiFi.waitForConnectResult() != WL_CONNECTED) {
        Serial.println("WiFi Failed");
        while(1) {
            delay(1000);
        }
    }
    if(udp.listen(44445)) {
        Serial.print("UDP Listening on IP: ");
        Serial.println(WiFi.localIP());
        udp.onPacket([](AsyncUDPPacket packet) {
            Serial.print("UDP Packet Type: ");
            Serial.print(packet.isBroadcast()?"Broadcast":packet.isMulticast()?"Multicast":"Unicast");
            Serial.print(", From: ");
            Serial.print(packet.remoteIP());
            Serial.print(":");
            Serial.print(packet.remotePort());
            Serial.print(", To: ");
            Serial.print(packet.localIP());
            Serial.print(":");
            Serial.print(packet.localPort());
            Serial.print(", Length: ");
            Serial.print(packet.length());
            Serial.print(", Data: ");
            Serial.write(packet.data(), packet.length());
            Serial.println();
            //reply to the client
            packet.printf("Got %u bytes of data", packet.length());
        });
    }
}

void loop()
{
    delay(1000);
    //Send broadcast
    //udp.broadcast("Anyone here?");
}

EDIT: commenting line 329 pbuf_free(_pb); in AsyncUDP.cpp removes the problem but doesnt clear the buffer. So im not able to recieve anything anymore after about 7-8 times

@Chriserke
Copy link
Author

Well the issue is shows up when i send Hello world from a small python server. What im trying to do is have the esp32 be a UDP server while my python side sends a msg and have it recieve it. Every time it receives a message it will crash with this error code
error baktrace

As for hardware im not sure where to find it all but

board: ESP32 Dev Module v1
IDE name: Arduino IDE 1.8.9 Flash Frequency: 80Mhz
PSRAM enabled: no
Upload Speed: 512000 Computer OS: Windows 10

Not sure about IDF component
And Core Installation version but i've installed most of the stuff yesterday through gitbash and i assume its up to date

Regards Chris.

@m0dd
Copy link

m0dd commented Apr 20, 2019

Well the issue is shows up when i send Hello world from a small python server. What im trying to do is have the esp32 be a UDP server while my python side sends a msg and have it recieve it. Every time it receives a message it will crash with this error code
error baktrace

As for hardware im not sure where to find it all but

board: ESP32 Dev Module v1
IDE name: Arduino IDE 1.8.9 Flash Frequency: 80Mhz
PSRAM enabled: no
Upload Speed: 512000 Computer OS: Windows 10

Not sure about IDF component
And Core Installation version but i've installed most of the stuff yesterday through gitbash and i assume its up to date

Regards Chris.

Hi! I have the same problem than Chriserke but with different hardware.

Hardware:

Board: WEMOS LOLIN32
Chip: ESP32D0WDQ6 (revision 1)
Core Installation version: 1.0.2 (https://github.com/espressif/arduino-esp32/releases/tag/1.0.2)
IDE name: Arduino IDE 1.8.9 (Windows Store 1.8.21.0)
Flash Frequency: 80Mhz
PSRAM enabled: ?no? ?yes?
Upload Speed: 921600
Computer OS: Windows 10

Description:

As soon as the udp packet is received, the mcu resets with following serial message:

assertion "pbuf_free: p->ref > 0" failed: file "/Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/lwip/lwip/src/core/pbuf.c", line 765, function: pbuf_free
abort() was called at PC 0x400d725b on core 1

Backtrace: 0x4008c7e0:0x3ffc8ce0 0x4008ca11:0x3ffc8d00 0x400d725b:0x3ffc8d20 0x400f0473:0x3ffc8d50 0x400d2bbf:0x3ffc8d70 0x400d2c0f:0x3ffc8e00 0x4008877d:0x3ffc8e30

I also tried to comment out everything inside the udp.onPacket(AsyncUDPPacket packet) {} to make sure that this has nothing to do with string problems.

Sketch:

#include "WiFi.h"
#include "AsyncUDP.h"

const char * ssid           = "SSID";
const char * password       = "1234";
String       client_address = "C01";
int          udpPort        = 7001; // Make sure this port matches the 'Server' port!
int          Control_pin    = 22;   // The device pin on the client to control e.g. an LED or Relay On/Off
String       received_message;

AsyncUDP udp;

void setup()
{
  delay(5000);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("WiFi Failed");
    while (1) {
      delay(1000);
    }
  }
  Serial.println("Listening on Port: " + String(udpPort));
  if (udp.listen(udpPort)) {
    Serial.print("UDP Listening on IP: ");
    Serial.println(WiFi.localIP());
    udp.onPacket([](AsyncUDPPacket packet) {
      //received_message = packet.readStringUntil('\n');
      //Serial.println(received_message);
      //received_message.trim(); // Remove unwanted characters
      // Test for a valid message structure 
//      if (received_message.startsWith(client_address) || received_message.startsWith("HAC") || received_message.startsWith("LAC"))
//      { // e.g. C01H or C)1L or ACH or ACL
//        if (received_message.endsWith("H")) {
//          Serial.println("Turning ON Device for "  + client_address);
//          Device_Control("ON");
//        }
//        if (received_message.endsWith("L")) {
//          Serial.println("Turning OFF Device for " + client_address);
//          Device_Control("OFF");
//        }
//        if (received_message == "HAC")      {
//          Serial.println("Turning ON Device for "  + client_address + " - global ON");
//          Device_Control("ON");
//        }
//        if (received_message == "LAC")      {
//          Serial.println("Turning OFF Device for " + client_address + " - global OFF");
//          Device_Control("OFF");
//        }
//      }
    });
  }
  //udp.broadcast("Device operated correctly");
}

void loop()
{
  Serial.println("mainloop running");
  delay(1000);
  Device_Control("ON");
  delay(1000);
  Device_Control("OFF");
}

void Device_Control(String Device_mode) {
  if (Device_mode ==  "ON") {
    pinMode(Control_pin, OUTPUT);
    digitalWrite(Control_pin, HIGH);
  }
  else {
    pinMode(Control_pin, OUTPUT);
    digitalWrite(Control_pin, LOW);
  }
}

Debug Messages:

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:928
ho 0 tail 12 room 4
load:0x40078000,len:8424
ho 0 tail 12 room 4
load:0x40080400,len:5868
entry 0x4008069c
Listening on Port: 7001
UDP Listening on IP: 192.168.0.78
mainloop running
mainloop running
mainloop running
mainloop running
mainloop running
mainloop running
mainloop running
mainloop running
mainloop running
mainloop running
mainloop running
assertion "pbuf_free: p->ref > 0" failed: file "/Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/lwip/lwip/src/core/pbuf.c", line 765, function: pbuf_free
abort() was called at PC 0x400d725b on core 1

Backtrace: 0x4008c7e0:0x3ffc8ce0 0x4008ca11:0x3ffc8d00 0x400d725b:0x3ffc8d20 0x400f0473:0x3ffc8d50 0x400d2bbf:0x3ffc8d70 0x400d2c0f:0x3ffc8e00 0x4008877d:0x3ffc8e30

@me-no-dev
Copy link
Member

revert this change and give it a go: a87b2ec#diff-3b5c94ff125c7d1f27ea52a9139410ec

@lbernstone
Copy link
Contributor

lbernstone commented Apr 21, 2019

Reverting works for me. I don't see any memory leaking between receives.

@Chriserke
Copy link
Author

Now i can run the UDPAsync example, works fine thanks.

@Edris89
Copy link

Edris89 commented May 9, 2019

I had the same problem today....May 2019
Any fix for this?

@axa88
Copy link

axa88 commented May 13, 2019

#MeToo

using AsyncUDP just after updating to ESP boards v1.0.2 and got this error upon receiving UDP packets.

assertion "pbuf_free: p->ref > 0" failed: file "/Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/lwip/lwip/src/core/pbuf.c", line 765, function: pbuf_free
abort() was called at PC 0x400dfc0f on core 1

Backtrace: 0x4008c88c:0x3ffc9fc0 0x4008cabd:0x3ffc9fe0 0x400dfc0f:0x3ffca000 0x400fa29f:0x3ffca030 0x400d9e7f:0x3ffca050 0x400d9ecf:0x3ffca0e0 0x40088829:0x3ffca110

Rebooting...

Reverting to v1.0.1 fixed things

@me-no-dev
Copy link
Member

change is reverted. will be released soon.

@jweigelt
Copy link
Contributor

jweigelt commented May 13, 2019

So if I understand this correctly, this relies on the callback accepting the packet by value and not by reference (as specified in AsyncUDP.h). This will call the destructor twice but use the copy constructor for cloning the object.

Accepting the packet by reference will therefore leak memory (and eventually crash) as the destructor is only called once. This is immediately noticed as the ESP stops receiving packets.

Doing anything in the callback function that would call the copy constructor will also crash as pbuf_unref() is now called without a prior call to pbuf_ref().

If this is intended behaviour it would be nice to note this somewhere. Without looking through the source files I find this to be rather counter-intuitive. :-)

It should be possible to fix this by either adding another call to pbuf_ref() to the copy constuctor or removing the ref'ing from the packet class alltogether.

@folosada
Copy link

folosada commented May 19, 2019

@me-no-dev I downloaded the latest code to AsyncUDP but I still have the same issue.

assertion "pbuf_free: p->ref > 0" failed: file "/Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/lwip/lwip/src/core/pbuf.c", line 765, function: pbuf_free
abort() was called at PC 0x400d8d83 on core 1

Backtrace: 0x4008cbac:0x3ffca530 0x4008cddd:0x3ffca550 0x400d8d83:0x3ffca570 0x400f372f:0x3ffca5a0 0x40120a20:0x3ffca5c0 0x40120fcd:0x3ffca5e0 0x40121023:0x3ffca660 0x40088b49:0x3ffca690

If I comment the line 329 in AsyncUDP.cpp the crash isn't happen, but the packets works 1 time and I need wait between five and ten minutes to send the packets again.

Line 329:

AsyncUDPPacket::~AsyncUDPPacket()
{
    pbuf_free(_pb);
}

Board: Espressif ESP32 Dev Module
IDE Name: Visual Studio Code with PlatformIO
Computer OS: Windows 10

doppelgrau pushed a commit to doppelgrau/esp32-aog that referenced this issue Oct 11, 2019
Newer plattform has support for saving configuration, older need older 
async-tcp.
doppelgrau added a commit to doppelgrau/esp32-aog that referenced this issue Jan 13, 2020
* GPS and Motordriver working.
Really ugly hacks, needs cleanup

* Cleanup, removing bno, preparing for generic imu correction

* generic correction (https://appelsiini.net/2018/calibrate-magnetometer/)

* LSM9DS1 implemented

* Implemented calibrationone bug: lsm9ds1 filter are way to slow

* Better IMU Reset, some cleanups, fixed save/restore

* only write on I2C if something changed

* changed to 50hz to reduce cpu util

* Implemented Ackermann

* removed a line by mistake

* fix typo

* Workaround for espressif/arduino-esp32#2685
Newer plattform has support for saving configuration, older need older 
async-tcp.

* Put some dependencys in the plattformio.ini
Removed (nearly) all code to set up the new config/setup.
Features should be copied back later.

* Start wifiAp and nothing else if HW not initialized

* cleanup

* Added new submodule

* Copy files for the espui in the data directory

* changed partition

* core webui start

* no change in memory, easier a single firmware instead spiffs-dedendencys

* removed mdns, no real benefit in test-setup

* working idleStats

* Only one namespace for configuration.
Implemented HW chooser

* rename example to make it consistent (all hardware that is not on I2C in the init)

* Begin of the "IO-Abtraction-Layer"

* Start of IO abstraction, fixed preferences

* start LED-Task earlier

* less global data, added logik for pwm

* allow the ads1115 differential measureings compared to A3 (e.g. VCC) instead to gnd (normal single ended mode).
Should result in better results with ratiometric sensors and a not 100% 
stable 5V source

* some addition to the init process (still WI)

* switched ads1115 library.
Removed soe (currently) unsused dependencys

* IO abstraction now contains the ads1115

* completed features for IO abstraction

* Network Setup

* allowed SSID selektion in AP mode (hostname in emergency/config mode

* First webinterface for the network setup

* Added Label-Field for PGNs, correction of intendation

* introduced a variable with the local IP (accessing can be different ETH/WiFi) => abtraction needed

* Created the UDP PGN handler (sending/receiving) - untested ATM

* undo the revert to broken idlestats

* start threads

* Working PGN exchange

* implemented (not yet tested) the different rtcm sources

* preperations to seperate the common rtcm and nmea tasks, since file gets too long

* disbaled fixed uload port

* network for nodemcu

* Move nmea structures in new files

* some adjustments on the network webinterface

* prepared for UI via SPIFFs

* removed upstream ESPUI module, own fork until bug s00500/ESPUI#63 is fixed

* added fork from ESPUI

* Nmea implementation

* moved all status updates on the Network core, try to avoid heap coruption (some used libraries seems not to bee thread safe)

* removed own fork, upstream fixed the issue

* added ESPUI

* Added json document size

* Better Wifihandling

* avoid multithread updated of the webUI

* minor fixes

* put all status updates in one task to avoid crashes of the webui, removed bluetooth (arduino seems to have problems enabling wifi and BT at the same time, since the radio is shared and the options for the different "coexistence"-parameters are not accessible.

* small fixes for gps setup and output

* some backgroundtasks only if not in setup-mode/hw

* Moved disabling of updated

* added debug messages.
Fixed reading digital inputs from the fxl6408 and address offsets

* small fixes (udp intervall, more info/error messages

* after talking to the espui dev the spiffs-variant has only a minor performance benefit, so remove the data directry for simplification (so spiffs setup planed anymore)

* moved networking code from hwSetup to network to make files smaller/better to understand.
Names of functions/variables not yet refactored

* created functionpointer for analogIn; added two special values (true/false or zero / one)

* implemented work- and steerswitch

* moved inputs from hw specific to general init

* moved gps-init (esp. the async udp part) after network init

* fixed missing i2c locks

* added wheel angle reading

* Added digital out selectors and utun-relay control

* fixed GUI for uturn

* changed PWM to 10 bit (finer control => good 8 bit usable after minpwm

* added motor to ioAccess and init f9p board

* Added a modofied AutoPID (symmetrical)

* fixed datatype in the header

* added steering actuator

* small fix in the GUI (control in the wron tab)

* save in the right label

* distribute the time a bit more, to reduce further the risk of a rce condition in the not threadsafe webserver

* show when PWm-Test is active

* use dutyCycle ...

* bugfixes to enable steering (steering enabled = false; preferences in the main thread => datastructures
dash0820 added a commit to dash0820/arduino-esp32-stripped that referenced this issue Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants