@@ -159,19 +159,21 @@ - (BOOL)_pingConnectionUsingLoopDelay:(NSUInteger)loopDelay
159
159
if (timeout > 0 ) pingTimeout = timeout;
160
160
161
161
// Set up a struct containing details the ping task will need
162
- SPMySQLConnectionPingDetails *pingDetails = malloc (sizeof (SPMySQLConnectionPingDetails));
163
- pingDetails->mySQLConnection = mySQLConnection;
164
- pingDetails->keepAliveLastPingSuccessPointer = &keepAliveLastPingSuccess;
165
- pingDetails->keepAlivePingThreadActivePointer = &keepAlivePingThreadActive;
166
- pingDetails->parentId = self;
162
+ // we can do this on the stack since this method makes sure to outlive the ping thread
163
+ SPMySQLConnectionPingDetails pingDetails = {
164
+ .mySQLConnection = mySQLConnection,
165
+ .keepAliveLastPingSuccessPointer = &keepAliveLastPingSuccess,
166
+ .keepAlivePingThreadActivePointer = &keepAlivePingThreadActive,
167
+ .parentId = self
168
+ };
167
169
168
170
// Create a pthread for the ping
169
171
pthread_t keepAlivePingThread_t;
170
172
171
173
pthread_attr_t attr;
172
174
pthread_attr_init (&attr);
173
175
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE);
174
- pthread_create (&keepAlivePingThread_t, &attr, (void *)&_backgroundPingTask, pingDetails);
176
+ pthread_create (&keepAlivePingThread_t, &attr, (void *)&_backgroundPingTask, & pingDetails);
175
177
176
178
// Record the ping start time
177
179
pingStartTime_t = mach_absolute_time ();
@@ -200,13 +202,12 @@ - (BOOL)_pingConnectionUsingLoopDelay:(NSUInteger)loopDelay
200
202
}
201
203
} while (keepAlivePingThreadActive);
202
204
203
- // wait for thread to go away, otherwise our free() below might run before _pingThreadCleanup()
205
+ // wait for thread to go away, otherwise pingDetails may go away before _pingThreadCleanup() finishes
204
206
pthread_join (keepAlivePingThread_t, NULL );
205
207
206
208
// Clean up
207
209
keepAlivePingThread_t = NULL ;
208
210
pthread_attr_destroy (&attr);
209
- free (pingDetails);
210
211
211
212
// Unlock the connection
212
213
[self _unlockConnection ];
0 commit comments