1 year, 9 months ago.

Why MQTT generates the "Operator new out of memory" failure?

Hi guys, I am facing a problem with the MQTT protocol. After publishing several messages the program stops the execution and prints the "Operator new out of memory" failure. What I want is to have a persistent connection with an MQTT broker, in order to send messages in an indefinitely way. But it seems that something is filling the memory and I don't know what it is. Before the failure everything works fine.

I am using the U-blox C030-U201 Starter Kit which has a lot of available memory.

Flow of the execution

  • Use the Ethernet library to connect to the LAN (without using DHCP)
  • Generate the corresponding MQTTNetwork and MQTTClient objects
  • Send menssages indefinitely

The code I used is shown next:

The used code

#define logMessage pc.printf

#include "mbed.h"
#include "EthernetInterface.h"
#include "MQTTNetwork.h"
#include "MQTTmbed.h"
#include "MQTTClient.h"

int main(int argc, char* argv[])

	EthernetInterface eth;//Object of class network interface
	eth.set_network("", "", "");//Desactivate DHCP and set the network configuration
	NetworkInterface* network_interface = 0;
    int connect_success = -1;
    float version = 0.6;
    char* topic = "mbed-sample";

    logMessage("HelloMQTT: version is %.2f\r\n", version);	
    logMessage("CONNECTION using Ethernet\r\n");
	network_interface = ð
    connect_success = eth.connect();
	if(connect_success == 0) {
		printf("[EasyConnect] Connected to Network successfully\r\n");
	else {
		printf("[EasyConnect] Connection to Network Failed %d!\r\n", connect_success);
		return -1;
	const char *ip_addr  = network_interface->get_ip_address();
	if (ip_addr == NULL) {
			logMessage("[EasyConnect] ERROR - No IP address\r\n");
			return -1;	
	logMessage("[EasyConnect] IP address %s\r\n", ip_addr);
    if (!network_interface) {
				logMessage("[ERROR] Impossible to recover the network\r\n");
        return -1;

    MQTTNetwork mqttNetwork(network_interface);
    MQTT::Client<MQTTNetwork, Countdown> client = MQTT::Client<MQTTNetwork, Countdown>(mqttNetwork);

    const char* hostname = "";
    int port = 1883;
    logMessage("Connecting to %s:%d\r\n", hostname, port);

    int rc = mqttNetwork.connect(hostname, port);
    if (rc != 0) logMessage("rc from TCP connect is %d\r\n", rc);

    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    data.MQTTVersion = 3;
    data.clientID.cstring = "mbed-sample";

    if ((rc = client.connect(data)) != 0)
        logMessage("rc from MQTT connect is %d\r\n", rc);

    MQTT::Message message;

    char buf[100];
	int messageCounter = 1;

	while (1){
		sprintf(buf, "{\"a\":{\"h\":\"31\",\"p\":\"80\",\"t\":\"24\"},{\"P012\":\"%d\"}}\r\n", messageCounter);
		message.qos = MQTT::QOS0;
		message.retained = false;
		message.dup = false;
		message.payload = (void*)buf;
		message.payloadlen = strlen(buf)+1;
		rc = client.publish(topic, message);
    if ((rc = client.unsubscribe(topic)) != 0)
        logMessage("rc from unsubscribe was %d\r\n", rc);

    if ((rc = client.disconnect()) != 0)
        logMessage("rc from disconnect was %d\r\n", rc);


    logMessage("Version %.2f: finish %d msgs\r\n", version, arrivedcount);

    return 0;

What I have tried

I also tried to reconnect after a while, because I though that maybe if close the session I can free the resources, but it doesn't work. The client reconnects successfully but at the end after sending a number of mesagges it fails:


Be the first to answer this question.