[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [paho-dev] Why the topic's length is set to zero?
|
Thanks, Ian.
Have a nice weekend,
Guilherme
Am 04.06.2016 06:32 schrieb "Ian Craggs" <
icraggs@xxxxxxxxxxxxxxxxxxxxxxx>:
Hi Guilherme,
I went back to read the documentation:
"The length of the topic if there are one more NULL characters
embedded in topicName, otherwise topicLen
is 0. If topicLen is 0, the value returned by strlen(topicName)
can be trusted. If topicLen is greater than 0,
the full topic name can be retrieved by accessing topicName
as a byte array of length topicLen."
A 0 in the topicLen means that you can use null-terminated C
string functions on the topic, otherwise you have to use memcpy, etc
to get the full string. As I remember the rationale was that it is
easier to check for 0, than to have to use strlen(). Now I think
about it, this probably isn't ideal, but I had a few other things to
worry about at the time too. :-)
Could be one of those things for a reversioning tidy up.
Ian
On 06/03/2016 11:42 PM, Guilherme
Maciel Ferreira wrote:
Hi guys,
I was using the topic's length as a return from "message arrived" callback:
int on_message_arrived(
void *context,
char *topic_name,
int topic_length,
MQTTClient_message *message)
{
MQTTClient_freeMessage(&message);
MQTTClient_free(topic_name);
return topic_length;
}
But the topic length is being zeroed, both at MQTTClient_run()
[https://github.com/eclipse/paho.mqtt.c/blob/develop/src/MQTTClient.c#L558-L559]
and at MQTTAsync_receiveThread()
[https://github.com/eclipse/paho.mqtt.c/blob/develop/src/MQTTAsync.c#L1679-L1680].
The zeroing is done by the snippet:
if (strlen(qe->topicName) == topicLen)
topicLen = 0;
The question is, why are you zeroing the topic length passed to
"message arrived" callback? Why topicName length and topicLen cannot
be equal?
I got there because I've noticed some invalid memory accesses when I
call MQTTClient_disconnect() after freeing a message through
MQTTClient_freeMessage(). I'm calling MQTTClient_disconnect() from the
"message arrived" callback. But the message seems to being freed
twice. Then, I figured out that this "message handler" should return
non-zero, so the message is removed from some internal message queue
[https://github.com/eclipse/paho.mqtt.c/blob/develop/src/MQTTClient.c#L570-L571].
Best regards,
Guilherme Ferreira
_______________________________________________
paho-dev mailing list
paho-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/paho-dev
--
Ian Craggs
icraggs@xxxxxxxxxx IBM United Kingdom
Paho Project Lead; Committer on Mosquitto
_______________________________________________
paho-dev mailing list
paho-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/paho-dev