Skip to content

v1中allocateNewBlock线程安全问题 #10

@Ikaros423

Description

@Ikaros423

若线程一执行到allocateNewBlock函数中freeList_ = nullptr;语句前,线程二释放了某个槽的空间,并修改了freeList_,线程一执行freeList_ = nullptr后,会导致逻辑上内存泄漏。
`

void* newBlock = operator new(BlockSize_);
reinterpret_cast<Slot*>(newBlock)->next = firstBlock_;
firstBlock_ = reinterpret_cast<Slot*>(newBlock);

char* body = reinterpret_cast<char*>(newBlock) + sizeof(Slot*);
size_t paddingSize = padPointer(body, SlotSize_); // 计算对齐需要填充内存的大小
curSlot_ = reinterpret_cast<Slot*>(body + paddingSize);

// 超过该标记位置,则说明该内存块已无内存槽可用,需向系统申请新的内存块
lastSlot_ = reinterpret_cast<Slot*>(reinterpret_cast<size_t>(newBlock) + BlockSize_ - SlotSize_ + 1);

freeList_ = nullptr;

`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions