Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pointer being freed was not allocated #990

Open
omartijn opened this issue Jan 21, 2025 · 0 comments
Open

pointer being freed was not allocated #990

omartijn opened this issue Jan 21, 2025 · 0 comments

Comments

@omartijn
Copy link

#942 was closed, and I cannot re-open it (because I do not have permissions), but it's still broken:

#include <algorithm>
#include <chrono>
#include <functional>
#include <iostream>
#include <thread>
#include <vector>

int main() {
    std::vector<std::unique_ptr<std::size_t>> numbers(1024 * 1024 * 100);
    std::vector<std::thread> threads(1);

    std::atomic<std::size_t> index{};

    auto start = std::chrono::system_clock::now();

    for (auto& thread : threads) {
        thread = std::thread{[&index, &numbers]() {
            while (true) {
                auto i = index.fetch_add(1, std::memory_order_relaxed);
                if (i >= numbers.size()) return;

                numbers[i] = std::make_unique<std::size_t>(i);
            }
        }};
    }

    for (auto& thread : threads) thread.join();

    auto end = std::chrono::system_clock::now();

    auto duration =
        std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
    std::cout << "Running on " << threads.size() << " threads took " << duration
              << std::endl;
}

Compiling this on macOS, like this:

c++ -std=c++20 -g -Wall -Wextra -L/Users/martijn/test -lmimalloc -o allocate allocate.cpp

leads to the following output when run:

allocate(16926,0x16d067000) malloc: *** error for object 0x3ff8c010020: pointer being freed was not allocated
allocate(16926,0x16d067000) malloc: *** set a breakpoint in malloc_error_break to debug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant