-
-
Notifications
You must be signed in to change notification settings - Fork 118
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
Fixed uninitialized value for this->ip #39
base: master
Are you sure you want to change the base?
Conversation
Hi, uint8_t server[] = { 192, 168.2, 1}; MQTT client(server, 1883, callback); ip member is "uint8_t *ip" can't use for string ("192.168.1.2"). |
Hello, I know I can use {192, 168, 1, 2} but I want to use "192.168.1.2" for several reasons:
The patch I submitted really doesn't change anything if {192, 168, 1, 2} is used, but allows using the form "192.168.1.2" also. regards, |
Hi @dguerizec , Oh, I see now what you want. may be you call this function. MQTT(char* domain, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int) first parameter domain can use for IP address string. |
Exactly, but it doesn't work since commit 18b6672 |
Hi @dguerizec , Now I check and work fine. Please check your environment. |
Well, an uninitialized variable can work fine depending on the environment, but I assure you it doesn't work with mine :) I'm not using the Particle IDE, but my own build system with firmware version 0.5.2 and MQTT library from github master. My compiler is: |
Maybe the main difference comes from the way I initialize my MQTT object. I'm doing (here): While you do: I checked the code and there is one initialization of this->ip to NULL, but it's in the default constructor and I guess it's never called either way. The initialization of this->ip should occur in initialize, even if ip is NULL. Otherwise this->ip could contain garbage and the test here will fail and try to connect to garbage IP. |
Okay, but if merge this code part, I think the bottom of a problem is other reason. MQTT mqtt = MQTT client("192.168.10.10", 1883, callback); ... // in initialize function , this part call. if (domain != NULL) this->domain = domain; ... // in MQTT.connect function, this TCP connection point is called. if (ip == NULL) result = _client->connect(this->domain.c_str(), this->port); I think this point maybe problem. Following is on the source code. |
Try to change to the following. MQTT mqtt = MQTT client("192.168.10.10", 1883, callback); ... // in initialize function , this part call. if (domain != NULL) this->domain = String(domain); ... // in MQTT.connect function, this TCP connection point is called. if (ip == NULL) result = _client->connect(this->domain, this->port); I checked like this now on the WebIDE lib environment, works well. |
The problem I experienced is not on the domain part, but on the uninitialized ip member variable. Another possibility for the fix could be to set ip to NULL in MQTT.h line 113: |
That's copy constructor is needed. But I don't define copy constructor. |
OK, I think I see your point. I'll check that when I get home. |
Hello, I tested this on line 121 at MQTT.h file:
As on the connect() method you verify if the IP is NULL I think that it should be NULL by default at least that you set it properly on the constructor. @hirotakaster Is it possible to close and merge this pr? What do you think? |
When connecting with a domain instead of an ip (or an ip in a string like "192.168.1.2"), the member variable this->ip was not initialized.
Later on, calling connect() would try to connect with this uninitialized ip and ignoring domain, thus connection was failing.