rework strsplit and its memory management for updating chatcounts, which was seemingly problematic on a live mac. cut new release. will enable full debugging if this release does not resolve memory issues

This commit is contained in:
camh 2022-02-23 22:00:23 -08:00
parent ce77fec0fe
commit 2b3f87be58
3 changed files with 42 additions and 66 deletions

Binary file not shown.

Binary file not shown.

View File

@ -15,69 +15,48 @@
// #define MESSAGES_FOR_MACINTOSH_DEBUGGING // #define MESSAGES_FOR_MACINTOSH_DEBUGGING
// based on https://github.com/jwerle/strsplit.c -- cleaned up and modified to use strtokm rather than strtok // based on https://github.com/mr21/strsplit.c/blob/master/strsplit.c
int strsplit (const char *str, char *parts[], const char *delimiter) { static char** _strsplit( const char* s, const char* delim, int* nb ) {
void* data;
char* _s = ( char* )s;
const char** ptrs;
int
ptrsSize,
nbWords = 1,
sLen = strlen( s ),
delimLen = strlen( delim );
#ifdef DEBUG_FUNCTION_CALLS while ( ( _s = strstr( _s, delim ) ) ) {
writeSerialPortDebug(boutRefNum, "DEBUG_FUNCTION_CALLS: strsplit"); _s += delimLen;
#endif ++nbWords;
char *pch;
int i = 0;
char *copy = NULL;
char *tmp = NULL;
copy = strdup(str);
if (! copy) {
goto bad;
}
pch = strtokm(copy, delimiter);
tmp = strdup(pch);
if (!tmp) {
goto bad;
}
parts[i++] = tmp;
while (pch) {
pch = strtokm(NULL, delimiter);
if (NULL == pch) {
break;
} }
ptrsSize = ( nbWords + 1 ) * sizeof( char* );
tmp = strdup(pch); ptrs =
data = malloc( ptrsSize + sLen + 1 );
if (! tmp) { if ( data ) {
*ptrs =
goto bad; _s = strcpy( ( ( char* )data ) + ptrsSize, s );
if ( nbWords > 1 ) {
while ( ( _s = strstr( _s, delim ) ) ) {
*_s = '\0';
_s += delimLen;
*++ptrs = _s;
}
}
*++ptrs = NULL;
} }
if ( nb ) {
*nb = data ? nbWords : 0;
}
return data;
}
parts[i++] = tmp; char** strsplit( const char* s, const char* delim ) {
} return _strsplit( s, delim, NULL );
}
free(copy); char** strsplit_count( const char* s, const char* delim, int* nb ) {
return _strsplit( s, delim, nb );
return i;
bad:
free(copy);
for (int j = 0; j < i; j++) {
free(parts[j]);
}
return -1;
} }
void aFailed(char *file, int line) { void aFailed(char *file, int line) {
@ -232,8 +211,8 @@ void sendIPAddressToCoprocessor() {
} }
// set up function to get messages in current chat // set up function to get messages in current chat
// limit to recent messages // limit to recent messages
// figure out pagination?? button on the top that says "get previous chats"?, TODO // figure out pagination?? button on the top that says "get previous chats"?, TODO
void getMessages(char *thread, int page) { void getMessages(char *thread, int page) {
#ifdef DEBUG_FUNCTION_CALLS #ifdef DEBUG_FUNCTION_CALLS
@ -296,9 +275,8 @@ void getChatCounts() {
strcpy(tempChatCountFunctionResponse, chatCountFunctionResponse); strcpy(tempChatCountFunctionResponse, chatCountFunctionResponse);
int chatCount = 0; int chatCount = 0;
char *(*chats[16])[64];
chatCount = strsplit(tempChatCountFunctionResponse, (char **)chats, ","); char **chats = strsplit_count(tempChatCountFunctionResponse, ",", &chatCount);
for (int chatLoopCounter = 0; chatLoopCounter < chatCount; chatLoopCounter++) { for (int chatLoopCounter = 0; chatLoopCounter < chatCount; chatLoopCounter++) {
@ -320,9 +298,8 @@ void getChatCounts() {
strcpy(tempChatCountFunctionResponse, chatCountFunctionResponse); strcpy(tempChatCountFunctionResponse, chatCountFunctionResponse);
int results = 0; int results = 0;
char *(*chatUpdate[2])[64];
results = strsplit((char *)chats[chatLoopCounter], (char **)chatUpdate, ":::"); char **chatUpdate = strsplit_count((char *)chats[chatLoopCounter], ":::", &results);
if (results != 2) { if (results != 2) {
@ -360,9 +337,8 @@ void getChatCounts() {
sprintf(chatName, "%.63s", &chatFriendlyNames[i * MAX_FRIENDLY_NAME_LENGTH]); sprintf(chatName, "%.63s", &chatFriendlyNames[i * MAX_FRIENDLY_NAME_LENGTH]);
int updateResults = 0; int updateResults = 0;
char *(*updatePieces[2])[64];
updateResults = strsplit(chatName, (char **)updatePieces, " new) "); char **updatePieces = strsplit_count(chatName, " new) ", &updateResults);
if (updateResults != 2) { if (updateResults != 2) {