I finally tracked down the reason for a problem I’ve had for a while:
char *tempdata= (char*)malloc(sizeof(data_packet->datalen));
Should have been:
char *tempdata= (char*)malloc(data_packet->datalen);
Force of habit must have seen me putting the sizeof() inside the malloc() without thinking. The mistake of an amateur.