aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax <post@wickenrode.com>2017-05-14 21:42:39 +0200
committerMax <post@wickenrode.com>2017-05-14 21:42:39 +0200
commitf603e3fe223189a35134a553fd1fcaef7f63ea70 (patch)
treeab02327c8b366e12d70a789c176e5eea4fc32af5
parentf741be75bc3f3d0964942a66d0fafc7ba0c62208 (diff)
downloadsequelpro-f603e3fe223189a35134a553fd1fcaef7f63ea70.tar.gz
sequelpro-f603e3fe223189a35134a553fd1fcaef7f63ea70.tar.bz2
sequelpro-f603e3fe223189a35134a553fd1fcaef7f63ea70.zip
Move a struct from heap to stack, since the caller will outlive its callees anyway
-rw-r--r--Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Ping & KeepAlive.m17
1 files changed, 9 insertions, 8 deletions
diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Ping & KeepAlive.m b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Ping & KeepAlive.m
index 8728bf3f..65a7ae02 100644
--- a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Ping & KeepAlive.m
+++ b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Ping & KeepAlive.m
@@ -159,11 +159,13 @@ end_cleanup:
if (timeout > 0) pingTimeout = timeout;
// Set up a struct containing details the ping task will need
- SPMySQLConnectionPingDetails *pingDetails = malloc(sizeof(SPMySQLConnectionPingDetails));
- pingDetails->mySQLConnection = mySQLConnection;
- pingDetails->keepAliveLastPingSuccessPointer = &keepAliveLastPingSuccess;
- pingDetails->keepAlivePingThreadActivePointer = &keepAlivePingThreadActive;
- pingDetails->parentId = self;
+ // we can do this on the stack since this method makes sure to outlive the ping thread
+ SPMySQLConnectionPingDetails pingDetails = {
+ .mySQLConnection = mySQLConnection,
+ .keepAliveLastPingSuccessPointer = &keepAliveLastPingSuccess,
+ .keepAlivePingThreadActivePointer = &keepAlivePingThreadActive,
+ .parentId = self
+ };
// Create a pthread for the ping
pthread_t keepAlivePingThread_t;
@@ -171,7 +173,7 @@ end_cleanup:
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
- pthread_create(&keepAlivePingThread_t, &attr, (void *)&_backgroundPingTask, pingDetails);
+ pthread_create(&keepAlivePingThread_t, &attr, (void *)&_backgroundPingTask, &pingDetails);
// Record the ping start time
pingStartTime_t = mach_absolute_time();
@@ -200,13 +202,12 @@ end_cleanup:
}
} while (keepAlivePingThreadActive);
- //wait for thread to go away, otherwise our free() below might run before _pingThreadCleanup()
+ //wait for thread to go away, otherwise pingDetails may go away before _pingThreadCleanup() finishes
pthread_join(keepAlivePingThread_t, NULL);
// Clean up
keepAlivePingThread_t = NULL;
pthread_attr_destroy(&attr);
- free(pingDetails);
// Unlock the connection
[self _unlockConnection];