Skip to content

Commit be3ab04

Browse files
committed
add desctrutors for ParseResponse & ParseClient to release resource after out of scope
1 parent 5a4f600 commit be3ab04

File tree

4 files changed

+174
-148
lines changed

4 files changed

+174
-148
lines changed

src/internal/ParseClient.cpp

Lines changed: 105 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -24,147 +24,158 @@
2424
ParseClient::ParseClient() {
2525
}
2626

27-
void ParseClient::begin(const char *applicationId, const char *clientKey) {
28-
Console.println("begin");
27+
ParseClient::~ParseClient() {
28+
end();
29+
}
2930

30-
// send the info to linux through Bridge
31-
if(applicationId) {
32-
Bridge.put("appId", applicationId);
33-
}
34-
if (clientKey) {
35-
Bridge.put("clientKey", clientKey);
36-
}
31+
void ParseClient::begin(const char *applicationId, const char *clientKey) {
32+
Console.println("begin");
33+
34+
// send the info to linux through Bridge
35+
if(applicationId) {
36+
Bridge.put("appId", applicationId);
37+
}
38+
if (clientKey) {
39+
Bridge.put("clientKey", clientKey);
40+
}
3741
}
3842

3943
void ParseClient::setInstallationId(const char *installationId) {
40-
this->installationId = installationId;
41-
if (installationId) {
42-
Bridge.put("installationId", installationId);
43-
} else {
44-
Bridge.put("installationId", "");
45-
}
44+
this->installationId = installationId;
45+
if (installationId) {
46+
Bridge.put("installationId", installationId);
47+
} else {
48+
Bridge.put("installationId", "");
49+
}
4650
}
4751

4852
const char* ParseClient::getInstallationId() {
49-
if (!this->installationId) {
50-
char *buf = new char[37];
53+
if (!this->installationId) {
54+
char *buf = new char[37];
5155

52-
requestClient.begin("parse_request");
53-
requestClient.addParameter("-i");
54-
requestClient.run();
56+
requestClient.begin("parse_request");
57+
requestClient.addParameter("-i");
58+
requestClient.run();
5559
read(&requestClient, buf, 37);
56-
this->installationId = buf;
57-
}
58-
return this->installationId;
60+
this->installationId = buf;
61+
}
62+
return this->installationId;
5963
}
6064

6165
void ParseClient::setSessionToken(const char *sessionToken) {
62-
if ((sessionToken != NULL) && (strlen(sessionToken) > 0 )){
63-
this->sessionToken = sessionToken;
64-
Bridge.put("sessionToken", sessionToken);
65-
} else {
66-
Bridge.put("sessionToken", "");
67-
}
66+
if ((sessionToken != NULL) && (strlen(sessionToken) > 0 )){
67+
this->sessionToken = sessionToken;
68+
Bridge.put("sessionToken", sessionToken);
69+
} else {
70+
Bridge.put("sessionToken", "");
71+
}
6872
}
6973

7074
void ParseClient::clearSessionToken() {
71-
setSessionToken(NULL);
75+
setSessionToken(NULL);
7276
}
7377

7478
const char* ParseClient::getSessionToken() {
75-
if (!this->sessionToken) {
76-
char *buf = new char[33];
77-
78-
requestClient.begin("parse_request");
79-
requestClient.addParameter("-s");
80-
requestClient.run();
81-
read(&requestClient, buf, 33);
82-
this->sessionToken = buf;
83-
}
84-
return this->sessionToken;
79+
if (!this->sessionToken) {
80+
char *buf = new char[33];
81+
82+
requestClient.begin("parse_request");
83+
requestClient.addParameter("-s");
84+
requestClient.run();
85+
read(&requestClient, buf, 33);
86+
this->sessionToken = buf;
87+
}
88+
return this->sessionToken;
8589
}
8690

8791
ParseResponse ParseClient::sendRequest(const char* httpVerb, const char* httpPath, const char* requestBody, const char* urlParams) {
8892
return sendRequest(String(httpVerb), String(httpPath), String(requestBody), String(urlParams));
8993
}
9094

9195
ParseResponse ParseClient::sendRequest(const String& httpVerb, const String& httpPath, const String& requestBody, const String& urlParams) {
92-
requestClient.begin("parse_request"); // start a process that launch the "parse_request" command
93-
94-
requestClient.addParameter("-v");
95-
requestClient.addParameter(httpVerb);
96-
requestClient.addParameter("-e");
97-
requestClient.addParameter(httpPath);
98-
if (requestBody != "") {
99-
requestClient.addParameter("-d");
100-
requestClient.addParameter(requestBody);
101-
}
102-
if (urlParams != "") {
103-
requestClient.addParameter("-p");
104-
requestClient.addParameter(urlParams);
96+
requestClient.begin("parse_request"); // start a process that launch the "parse_request" command
97+
98+
requestClient.addParameter("-v");
99+
requestClient.addParameter(httpVerb);
100+
requestClient.addParameter("-e");
101+
requestClient.addParameter(httpPath);
102+
if (requestBody != "") {
103+
requestClient.addParameter("-d");
104+
requestClient.addParameter(requestBody);
105+
}
106+
if (urlParams != "") {
107+
requestClient.addParameter("-p");
108+
requestClient.addParameter(urlParams);
105109
requestClient.runAsynchronously();
106-
} else {
107-
requestClient.run(); // Run the process and wait for its termination
108-
}
110+
} else {
111+
requestClient.run(); // Run the process and wait for its termination
112+
}
109113

110-
ParseResponse response(&requestClient);
111-
return response;
114+
ParseResponse response(&requestClient);
115+
return response;
112116
}
113117

114118
bool ParseClient::startPushService() {
115-
pushClient.begin("parse_push"); // start a process that launch the "parse_request" command
116-
pushClient.runAsynchronously();
117-
118-
while(1) {
119-
if(pushClient.available()) {
120-
// read the push starting result
121-
char c = pushClient.read();
122-
while(pushClient.available()) {
123-
pushClient.read();
124-
}
125-
if (c == 's') {
126-
pushClient.write('n'); // send a signal that ready to consume next available push
127-
return true;
128-
} else {
129-
return false;
130-
}
131-
}
132-
}
119+
pushClient.begin("parse_push"); // start a process that launch the "parse_request" command
120+
pushClient.runAsynchronously();
121+
122+
while(1) {
123+
if(pushClient.available()) {
124+
// read the push starting result
125+
char c = pushClient.read();
126+
while(pushClient.available()) {
127+
pushClient.read();
128+
}
129+
if (c == 's') {
130+
pushClient.write('n'); // send a signal that ready to consume next available push
131+
return true;
132+
} else {
133+
return false;
134+
}
135+
}
136+
}
133137
}
134138

135139
ParsePush ParseClient::nextPush() {
136-
ParsePush push(&pushClient);
137-
return push;
140+
ParsePush push(&pushClient);
141+
return push;
138142
}
139143

140144
bool ParseClient::pushAvailable() {
141-
pushClient.write('n'); // send a signal that ready to consume next available push
142-
if(pushClient.available()) {
143-
return 1;
144-
} else {
145-
return 0;
146-
}
145+
pushClient.write('n'); // send a signal that ready to consume next available push
146+
if(pushClient.available()) {
147+
return 1;
148+
} else {
149+
return 0;
150+
}
147151
}
148152

149153
void ParseClient::stopPushService() {
150-
pushClient.close();
154+
pushClient.close();
151155
}
152156

153157
void ParseClient::end() {
154-
stopPushService();
158+
if(installationId) {
159+
delete[] installationId;
160+
}
161+
if(sessionToken) {
162+
delete[] sessionToken;
163+
}
164+
165+
stopPushService();
155166
}
156167

157168
void ParseClient::read(Process* client, char* buf, int len) {
158169
memset(buf, 0, len);
159170
int p = 0;
160-
while(1) {
161-
if(client->available()) {
162-
while (p < (len-1) && client->available()) {
163-
buf[p++] = client->read();
164-
}
165-
break;
166-
}
167-
}
171+
while(1) {
172+
if(client->available()) {
173+
while (p < (len-1) && client->available()) {
174+
buf[p++] = client->read();
175+
}
176+
break;
177+
}
178+
}
168179
}
169180

170181
ParseClient Parse;

src/internal/ParseClient.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class ParseClient {
4949
* \brief Constructor of ParseClient object
5050
*/
5151
ParseClient();
52+
/*! \fn ParseClient()
53+
* \brief Destructor of ParseClient object
54+
*/
55+
~ParseClient();
5256

5357
/*! \fn begin(const char *applicationId, const char *clientKey)
5458
* \brief Initialize the Parse client and user session

0 commit comments

Comments
 (0)