Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in error handling? #6

Open
ad-johnson opened this issue Mar 31, 2020 · 1 comment
Open

Memory leak in error handling? #6

ad-johnson opened this issue Mar 31, 2020 · 1 comment

Comments

@ad-johnson
Copy link

ad-johnson commented Mar 31, 2020

Hi,
I've used the arduino code for SCPI processing and it's working very well. Perhaps I'm not using it correctly, and I should say I'm no C expert but I think there's a memory leak in the way the code manages errors.

When an error is created by the application to place on the queue, the code 'malloc's some memory to create it. When it is popped off the queue, say to be displayed, that memory is not freed. When there is no error, a 'success' error is malloc(ed) and returned but again, the memory is not freed.

My consuming app is fairly simple and is based on your example meter.ino. However, my application does raise errors and loop() pops them off the queue until a success error is received. If I don't free() each error popped, the Arduino stops responding after approximately 10 - 12 processes of a IDN? command; If I free() each error then it seems to run indefinitely.
Here's my loop():
void loop()
{
char line_buffer[60];
unsigned char read_length;
struct scpi_error
error;

while(1)
{
/* Read in a line and execute it. /
read_length = Serial.readBytesUntil('\n', line_buffer, 60);
if(read_length > 0)
{
scpi_execute_command(&ctx, line_buffer, read_length);
error = scpi_pop_error(&ctx);
while (error->id != 0) {
Serial.print(error->id);
Serial.print(": ");
Serial.println(error->description);
free( (void
)error);
error = scpi_pop_error(&ctx);
}
free( (void*)error);
}
}
}
You can see the free() commands; comment those out and the Arduino grinds to a halt. Perhaps Errors should be returned as a linked list, like command, with a function to free_errors() like free_tokens().

Regards,
Andrew

@neobrien
Copy link

No response still.......

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants