Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Mosquitto » "double free or corruption" after publish message(Get double free error after publish callback on_publish. And got two on_publishs with same mid.)
"double free or corruption" after publish message [message #1837070] Thu, 21 January 2021 15:09 Go to next message
Xiao B is currently offline Xiao BFriend
Messages: 3
Registered: January 2021
Junior Member
Hi Everyone,

I am using Mosquitto source code lib to create a MQTT client talking to test.mosquitto.org. The code will randomly stop after runs for several hours due to the "double free" error.

When this error happens, I can see two ON_PUBLISH callbacks get triggered and mids in both ON_PUBLISH are same. You can see this in attched picture.


My question is:

Why it has two same mid? Is it because message got re-published?


Any comments are appreciated!



Re: "double free or corruption" after publish message [message #1837088 is a reply to message #1837070] Thu, 21 January 2021 21:49 Go to previous messageGo to next message
Roger Light is currently offline Roger LightFriend
Messages: 90
Registered: September 2013
Member
The library hasn't retransmitted messages like this for a long time, so I suspect that you are using an old version. Is that correct?

Without seeing some example code that exhibits this behaviour it's impossible to say what is happening - are you able to share anything?

Regards,

Roger
Re: "double free or corruption" after publish message [message #1837147 is a reply to message #1837088] Fri, 22 January 2021 18:10 Go to previous messageGo to next message
Xiao B is currently offline Xiao BFriend
Messages: 3
Registered: January 2021
Junior Member
Thank you for your reply!

Yes. You are right. The lib I used is from 2018.

However, I updated the lib this morning and had a same error. Please see attached picture.


Regards,

Xiao
Re: "double free or corruption" after publish message [message #1837149 is a reply to message #1837088] Fri, 22 January 2021 18:27 Go to previous message
Xiao B is currently offline Xiao BFriend
Messages: 3
Registered: January 2021
Junior Member
Here are some codes I am using.

For MQTT thread
void * kbMqttThread(void *arg)
{
	_initMqtt();
	while(true)
	{
		if(_startMqtt() == true)
		{
			_wait4Connected();
		
			while((!mosquitto_loop(mosq,-1,1)) && _bConnected)
			{
				sleep(1);
			}
		
			_stopMqtt();			// Something is wrong when we reach this point. We'd like to clean everything and restart MQTT.
		}
		else
		{
			LOG_WITHOUT_MASK("\nMQTT ----  Connot start MQTT!\n");
		}
		sleep(10);				// Sleep 10 seconds and try restart MQTT again.
	}
	
	//mosquitto_lib_cleanup();
	return 0;
}


For publish. Error happens randomly after publish.
uint8_t kbMqttPublish(SJsonMessage * p)
{
	int rc;
	int payloadLen;
	
	if(kbMqttReady2Publish() == false)
	{
		return false;				// It is not ready to publish
	}
	
	payloadLen = strlen(p->Message);
	if((payloadLen < 5) || (payloadLen >= JSON_EVENT_BUFFER_SIZE))
	{
		FAST_LOG_WITHOUT_MASK("\nMQTT ERROR: Message length WRONG!");
		while(1)
		{	// Trap here. Need to be removed.
		};
	}

	rc = mosquitto_publish(mosq, NULL, p->Topic, payloadLen, p->Message, MQTT_QOS_0, false);
	if(rc != MOSQ_ERR_SUCCESS)
	{
		fprintf(stderr, "Error publishing: %s\n", mosquitto_strerror(rc));
		return false;
	}
	else
	{
		return true;
	}
}


For mqtt start
static uint8_t _startMqtt(void)
{
	int rc;
	
	_bConnected = false;
	
	// Required before calling other mosquitto functions
	mosquitto_lib_init();				
	mosq = mosquitto_new(NULL, true, NULL);
	if(mosq == NULL)
	{
		fprintf(stderr, "\nError: Out of memory.\n");
	}
	else
	{
		//Do nothing
	}

	/* Configure callbacks. This should be done before connecting ideally. */
	mosquitto_connect_callback_set(mosq, _onConnect);
	mosquitto_publish_callback_set(mosq, _onPublish);
	mosquitto_subscribe_callback_set(mosq, _onSubscribe);
	mosquitto_message_callback_set(mosq, _onMessage);
	mosquitto_disconnect_callback_set(mosq,_onDisconnect);

	rc = mosquitto_connect(mosq, _mqttConfig.brokerIP, _mqttConfig.Port, _mqttConfig.keepLiveSeconds);
	if(rc != MOSQ_ERR_SUCCESS)
	{
		mosquitto_destroy(mosq);
		fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc));
		return false;
	}
	return true;
}



For mqtt stop


static void _stopMqtt(void)
{
	if(mosq)
	{
		mosquitto_disconnect(mosq);
		mosquitto_loop_stop(mosq,false);
		
		// Reset callback functions
		mosquitto_connect_callback_set(mosq, NULL);
		mosquitto_publish_callback_set(mosq, NULL);
		mosquitto_subscribe_callback_set(mosq, NULL);
		mosquitto_message_callback_set(mosq, NULL);
		mosquitto_disconnect_callback_set(mosq,NULL);
		
		// Release mosquitto library
		mosquitto_destroy(mosq);
		mosq = NULL;
	}
	mosquitto_lib_cleanup();
	LOG_WITHOUT_MASK("\nMQTT ---- STOPPED!");
}


Regards,
Xiao
Previous Topic:User Authentification (limited IP-Addresses only)
Next Topic:fail2ban on mqtts
Goto Forum:
  


Current Time: Thu May 02 16:55:37 GMT 2024

Powered by FUDForum. Page generated in 5.03798 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top