Fixes two evil memory leaks in vector and string.

This commit is contained in:
Felix Queissner 2015-10-13 12:53:08 +02:00
parent 112cdadf69
commit e8156fea82
5 changed files with 22 additions and 10 deletions

View file

@ -8,6 +8,8 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#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<uint32_t*>(malloc(sizeof(*this->mReferences) + sizeof(uint8_t) * (length + 1)));
this->mReferences = static_cast<uint32_t*>(malloc(sizeof(*this->mReferences) + sizeof(uint8_t) * (length + 1)));
this->mText = reinterpret_cast<uint8_t *>(&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);
}

View file

@ -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)

View file

@ -1,6 +1,6 @@
; =============================================
; compiled with Copper 1.0
; 2015-10-12 18:47:11
; 2015-10-13 12:52:49
; =============================================
; native method: print(…)

View file

@ -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

View file

@ -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,"]");
/*