Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fixed memory leak in string buffer
  • Loading branch information
soundanalogous committed Aug 1, 2013
commit b1c78c5685b90f56d02bf0021cd64729bae776f0
2 changes: 2 additions & 0 deletions Firmata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ void FirmataClass::processSysexMessage(void)
j++;
}
(*currentStringCallback)(buffer);
free(buffer);
buffer = 0;
}
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion examples/EchoString/EchoString.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void sysexCallback(byte command, byte argc, byte*argv)

void setup()
{
Firmata.setFirmwareVersion(0, 1);
Firmata.setFirmwareVersion(FIRMATA_MAJOR_VERSION, FIRMATA_MINOR_VERSION);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated this so I could test in my client application. All examples should probably be updated to use these constants.

Firmata.attach(STRING_DATA, stringCallback);
Firmata.attach(START_SYSEX, sysexCallback);
Firmata.begin(57600);
Expand Down
44 changes: 29 additions & 15 deletions test/unit/firmata_test/firmata_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,6 @@ void loop()
Test::run();
}

// Note: this test required adding a method (Firmata.unsetFirmwareVersion()) to
// Firmata.cpp solely for the purpose of running this test. The method has been
// removed from Firmata.cpp, but keeping the test here as a recored
// test(setFirmwareVersionDoesNotLeakMemory)
// {
// Firmata.setFirmwareVersion(1, 0);
// int initialMemory = freeMemory();

// Firmata.setFirmwareVersion(1, 0);

// assertEquals(0, initialMemory - freeMemory());

// Firmata.unsetFirmwareVersion();
// }

test(beginPrintsVersion)
{
FakeStream stream;
Expand Down Expand Up @@ -61,6 +46,10 @@ void processMessage(const byte* message, size_t length)
}
}

void stringCallback(char *myString)
{
}

byte _digitalPort;
int _digitalPortValue;
void writeToDigitalPort(byte port, int value)
Expand Down Expand Up @@ -140,3 +129,28 @@ test(specifiedDigitalWritePort)

assertEqual(1, _digitalPort);
}

test(stringDataDoesNotLeakMemory)
{
Firmata.attach(STRING_DATA, stringCallback);

int initialMemory = freeMemory();

byte stringMsg[] = { START_SYSEX, STRING_DATA, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, END_SYSEX };

for (int i = 0; i < 4; i++) {
processMessage(stringMsg, 13);
}

assertEqual(0, initialMemory - freeMemory());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the update this test would fail. It was incrementing the memory by 7 bytes each iteration. Now memory is freed and I've even verified that I can send echo a string from a client application at least 10,000 times.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is no longer necessary now that malloc is no longer used for incoming strings

}

test(setFirmwareVersionDoesNotLeakMemory)
{
Firmata.setFirmwareVersion(1, 0);
int initialMemory = freeMemory();

Firmata.setFirmwareVersion(1, 0);

assertEqual(0, initialMemory - freeMemory());
}