Skip to content

Commit 61a253d

Browse files
committed
duplicate hostnames to fix issues with pointers that go away
1 parent 3a7d226 commit 61a253d

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/MQTTClient.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ class MQTTClient {
9999
void begin(const char hostname[], Client &client) { this->begin(hostname, 1883, client); }
100100

101101
void begin(const char hostname[], int port, Client &client) {
102-
// set config
103-
this->hostname = hostname;
104-
this->port = port;
102+
// set hostname and port
103+
this->setHost(hostname, port);
104+
105+
// set client
105106
this->netClient = &client;
106107

107108
// initialize client
@@ -134,7 +135,13 @@ class MQTTClient {
134135
void setHost(const char hostname[]) { this->setHost(hostname, 1883); }
135136

136137
void setHost(const char hostname[], int port) {
137-
this->hostname = hostname;
138+
// free hostname if set
139+
if(this->hostname != nullptr) {
140+
free((void *)this->hostname);
141+
}
142+
143+
// set hostname and port
144+
this->hostname = strdup(hostname);
138145
this->port = port;
139146
}
140147

0 commit comments

Comments
 (0)