Skip to content

Commit

Permalink
Nr 296618 dealloc interaction history list (#319)
Browse files Browse the repository at this point in the history
* NR-296618: deallocInteractionHistoryList fix

* fix up dup dealloc Mehtod

* add test interaction history list
  • Loading branch information
cdillard-NewRelic authored Dec 3, 2024
1 parent e4a99b1 commit 87338c4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,7 @@ void NRMA__freeMetaData(void)
}

void NRMA_freeInteractionHistoryList(void) {
NRMAInteractionHistoryNode* root = NRMA__getInteractionHistoryList();
while (root != NULL) {
if (root->name != NULL) {
free((void *)root->name);
root->name = NULL;
}

NRMAInteractionHistoryNode* temp = root;
root = root->next;
free((void *)temp);
}
NRMA__deallocInteractionHistoryList();
}

void NRMA_freeExceptionData(void) {
Expand Down
60 changes: 33 additions & 27 deletions Agent/CrashHandler/ExceptionDataInterface/NRMAInteractionHistory.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,34 @@ void NRMA__insertNode(NRMAInteractionHistoryNode* interaction);

void NRMA__AddInteraction(const char* interactionName, long long timestampMillis)
{

if (interactionName == NULL || strlen(interactionName) == 0) {
return;
}

NRMAInteractionHistoryNode* node = (NRMAInteractionHistoryNode*)malloc(sizeof(NRMAInteractionHistoryNode));
if (node != NULL) {
size_t len = strlen(interactionName);
if (len < 1) {
free(node);
return;
}
char* temp = malloc(sizeof(char) * (len+1));
if (temp == NULL) {
free(node);
// Failure case hit here.
return;
}
strncpy(temp, interactionName,len);
temp[len] = '\0';
node->name = temp;
node->timestampMillis = timestampMillis;

NRMA__insertNode(node);

if (node == NULL) {
return;
}

node->name = strdup(interactionName);
if (node->name == NULL) {
free(node);
return;
}
node->timestampMillis = timestampMillis;
node->next = NULL;

NRMA__insertNode(node);
}

void NRMA__insertNode(NRMAInteractionHistoryNode* interaction)
{
if (interaction == NULL) {
return;
}

interaction->next = __list;
__list = interaction;
}
Expand All @@ -59,14 +63,16 @@ NRMAInteractionHistoryNode* NRMA__getInteractionHistoryList(void)

void NRMA__deallocInteractionHistoryList(void)
{
NRMAInteractionHistoryNode* head = __list;
__list = NULL;
while (head != NULL) {
free((void*)head->name);
head->name = NULL;
NRMAInteractionHistoryNode* tmp = head->next;
free((void*)head);
head = NULL;
head = tmp;
NRMAInteractionHistoryNode* current = __list;
NRMAInteractionHistoryNode *next;

while (current != NULL) {
next = current->next;

free((void*)current->name);
free((void*)current);
current = next;
}

__list = NULL;
}
15 changes: 14 additions & 1 deletion Agent/General/NewRelicAgentInternal.m
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ - (void) applicationDidEnterBackground {
@try {
#endif
[NRMATraceController completeActivityTrace];

[NRMAInteractionHistoryObjCInterface deallocInteractionHistory];
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
} @catch (NSException* exception) {
Expand Down Expand Up @@ -1047,8 +1048,20 @@ + (void) shutdown {
[NRMAUDIDManager deleteStoredID];

// Stored device data, Metadata and crash file are cleared when crash upload.
[NRMAInteractionHistoryObjCInterface deallocInteractionHistory];
#ifndef DISABLE_NR_EXCEPTION_WRAPPER
@try {
#endif
[NRMATraceController completeActivityTrace];

[NRMAInteractionHistoryObjCInterface deallocInteractionHistory];
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
} @catch (NSException* exception) {
[NRMAExceptionHandler logException:exception
class:NSStringFromClass([self class])
selector:NSStringFromSelector(_cmd)];
}
#endif

// Clear stored user defaults
[[[NRMAHarvestController harvestController] harvester] clearStoredConnectionInformation];
[[[NRMAHarvestController harvestController] harvester] clearStoredHarvesterConfiguration];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#import "NRMAInteractionHistory.h"

@interface NRMAExceptionMetaDataTest : XCTestCase

Expand All @@ -31,6 +32,7 @@ - (void)tearDown
[super tearDown];
}

// test
- (void)test_NRMA_writeNRMeta
{

Expand Down Expand Up @@ -68,7 +70,17 @@ - (void)test_NRMA_writeNRMeta
NRMA_setDiskFree(123456);
NRMA_setAccountId(2);
NRMA_setAgentId(1);



// Add interactions.

long long interactionTime = 1000;
NSString* interactionName = @"TestTrace";
NRMA__AddInteraction(interactionName.UTF8String, interactionTime);
long long interactionTime2 = 2000;
NSString* interactionName2 = @"TestTrace2";
NRMA__AddInteraction(interactionName2.UTF8String, interactionTime2);

NRMA_writeNRMeta(NULL, NULL, NULL);


Expand Down

0 comments on commit 87338c4

Please sign in to comment.