From e8156fea82c199164db6e284e5fe8fcdb49d65bd Mon Sep 17 00:00:00 2001 From: Felix Queissner Date: Tue, 13 Oct 2015 12:53:08 +0200 Subject: [PATCH] Fixes two evil memory leaks in vector and string. --- include/ker/string.hpp | 10 ++++++---- include/ker/vector.hpp | 14 ++++++++++++-- scripts/main.cu.spark | 2 +- src/interrupts.c | 4 ++-- src/malloc.c | 2 +- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/include/ker/string.hpp b/include/ker/string.hpp index 3fe6871..b2b0e82 100644 --- a/include/ker/string.hpp +++ b/include/ker/string.hpp @@ -8,6 +8,8 @@ #include #include +#include + #endif #include "config.hpp" @@ -73,12 +75,12 @@ namespace ker String(const uint8_t *bytes, size_t length) { // Allocate memory for n bytes + nulltermination + length - this->mReferences = reinterpret_cast(malloc(sizeof(*this->mReferences) + sizeof(uint8_t) * (length + 1))); + this->mReferences = static_cast(malloc(sizeof(*this->mReferences) + sizeof(uint8_t) * (length + 1))); this->mText = reinterpret_cast(&this->mReferences[1]); this->mLength = length; // We have a single reference - *this->mReferences = 1; + (*this->mReferences) = 1; // Initialize string memcpy(this->mText, bytes, length); @@ -88,8 +90,8 @@ namespace ker ~String() { if(this->mReferences != nullptr) { - *this->mReferences -= 1; - if(this->mReferences == 0) { + (*this->mReferences) -= 1; + if((*this->mReferences) == 0) { // Last reference was released, now destroy the memory. free(this->mReferences); } diff --git a/include/ker/vector.hpp b/include/ker/vector.hpp index fc2b675..c12cc54 100644 --- a/include/ker/vector.hpp +++ b/include/ker/vector.hpp @@ -177,15 +177,25 @@ namespace ker if(this->mReserved >= space) { return; } + if((this->mReserved + initialCap) > space) { + space = this->mReserved + initialCap; + } const size_t newSize = sizeof(T) * space; T *newData = (T*)malloc(newSize); if(this->mData != nullptr) { - memcpy(newData, this->mData, newSize); + // Ermahgerd, what a leak. Not obvious but it leaks. + // memcpy(newData, this->mData, newSize); + // Fix: copy construct all objects into the new memory, then destroy the original data. + for(size_t i = 0; i < this->mLength; i++) { + new (&newData[i]) T (this->mData[i]); + this->mData[i].~T(); + } + free(this->mData); } this->mData = newData; - this->mReserved = space; + this->mReserved = space; } T& operator [](size_t idx) diff --git a/scripts/main.cu.spark b/scripts/main.cu.spark index 3eae9ff..a68633f 100644 --- a/scripts/main.cu.spark +++ b/scripts/main.cu.spark @@ -1,6 +1,6 @@ ; ============================================= ; compiled with Copper 1.0 -; 2015-10-12 18:47:11 +; 2015-10-13 12:52:49 ; ============================================= ; native method: print(…) diff --git a/src/interrupts.c b/src/interrupts.c index d60ee3f..912ac91 100644 --- a/src/interrupts.c +++ b/src/interrupts.c @@ -86,7 +86,7 @@ void intr_routine(CpuState *state) if(state->intr < 0x20) { if(handler == nullptr) { - kprintf("\n\x12\x04 Exception [%d] %s!\x12\0x07 \n", state->intr, name); + kprintf("\n\x12\004Exception [%d] %s!\x12\007\n", state->intr, name); kprintf( "EIP: %x" "", @@ -114,7 +114,7 @@ void intr_routine(CpuState *state) else { if(handler == nullptr) { - kprintf("\n\x12\x04Interrupt [%d] %s occurred!\x12\0x7\n", state->intr, name); + kprintf("\n\x12\004Interrupt [%d] %s occurred!\x12\007\n", state->intr, name); while(1) { // Prozessor anhalten diff --git a/src/malloc.c b/src/malloc.c index 1a30b53..a11f690 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -87,7 +87,7 @@ void malloc_print_list(int freeList) serial_printf(SERIAL_COM1,"\n"); serial_printf(SERIAL_COM1,"["); - serial_write(SERIAL_COM1, (char*)list + sizeof(List), list->length); + serial_write(SERIAL_COM1, (const uint8_t*)(list) + sizeof(List), list->length); serial_printf(SERIAL_COM1,"]"); /*