diff --git a/libs/core/i2c.cpp b/libs/core/i2c.cpp index 56fdd440..35c3cc76 100644 --- a/libs/core/i2c.cpp +++ b/libs/core/i2c.cpp @@ -35,7 +35,9 @@ public: Buffer readBuffer(int address, int size, bool repeat = false) { Buffer buf = mkBuffer(NULL, size); + registerGCObj(buf); int status = this->i2c.read(address << 1, buf->data, size, repeat); + unregisterGCObj(buf); if (status != ErrorCode::DEVICE_OK) { buf = 0; } diff --git a/libs/serial---linux/serial-target.cpp b/libs/serial---linux/serial-target.cpp index 0236a74a..ce620703 100644 --- a/libs/serial---linux/serial-target.cpp +++ b/libs/serial---linux/serial-target.cpp @@ -190,7 +190,9 @@ Buffer LinuxSerialDevice::readBuffer() { pthread_mutex_lock(&lock); int sz = bufferedSize(); auto r = mkBuffer(NULL, sz); + registerGCObj(r); int sz2 = readBuf(r->data, sz); + unegisterGCObj(r); if (sz != sz2) target_panic(999); pthread_mutex_unlock(&lock); diff --git a/libs/serial/serial-target.h b/libs/serial/serial-target.h index f9eb1cc9..e9697797 100644 --- a/libs/serial/serial-target.h +++ b/libs/serial/serial-target.h @@ -51,17 +51,16 @@ class CodalSerialDeviceProxy { // n maybe 0 but we still call read to force // to initialize rx auto buf = mkBuffer(NULL, n); + auto res = buf; + registerGCObj(buf); auto read = ser.read(buf->data, buf->length, SerialMode::ASYNC); if (read == DEVICE_SERIAL_IN_USE || read == 0) { // someone else is reading - return mkBuffer(NULL, 0); + res = mkBuffer(NULL, 0); + } else if (buf->length != read) { + res = mkBuffer(buf->data, read); } - if (buf->length != read) { - registerGCObj(buf); - auto buf2 = mkBuffer(buf->data, read); - unregisterGCObj(buf); - buf = buf2; - } - return buf; + unregisterGCObj(buf); + return res; } void writeBuffer(Buffer buffer) { diff --git a/libs/storage/storage.cpp b/libs/storage/storage.cpp index ce5c7110..986a429e 100644 --- a/libs/storage/storage.cpp +++ b/libs/storage/storage.cpp @@ -164,8 +164,10 @@ Buffer readAsBuffer(String filename) { if (sz > 0xffff) return NULL; auto res = mkBuffer(NULL, sz); + registerGCObj(res); f->seek(0); f->read(res->data, res->length); + unregisterGCObj(res); return res; }