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 #942

Open
omartijn opened this issue Oct 4, 2024 · 2 comments
Open

pointer being freed was not allocated #942

omartijn opened this issue Oct 4, 2024 · 2 comments

Comments

@omartijn
Copy link

omartijn commented Oct 4, 2024

I'm trying out mimalloc, and immediately run into an issue, with the following test code. I've tried both with the latest master and with v2.1.7.

#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
@taco-paco
Copy link

Also ran into the same issue, it looks something goes wrong on thread destruction. Simplified version like this crashed for me with familiar:

test(96681,0x16b247000) malloc: *** error for object 0x20000001900: pointer being freed was not allocated
test(96681,0x16b247000) malloc: *** set a breakpoint in malloc_error_break to debug

#include <thread>

void asd() {}
int main() {
    std::thread thread(asd);
    thread.join();

    return 0;
}

mimalloc compiled via:

cmake -DMI_BUILD_STATIC=ON -DMI_BUILD_SHARED=OFF -DMI_BUILD_TESTS=OFF \
    -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="/usr/local/mimalloc" ..

and then statically linked with the code above.

I'm on macOS 14.3 (23D56) running on arm64

@daanx
Copy link
Collaborator

daanx commented Oct 30, 2024

Looking into this. I can repro the second example -- it looks it has to do with zone initialization as it works when using a dynamic library to override and use env DYLD_INSERT_LIBRARIES=./libmimalloc-debug.dylib ./a.out .

Also, if you link with the static object file it also works,

clang++ --std=c++20 -I../../include -g ./mimalloc-debug.o ../../test/main-override.cpp

Here it is best to compile mimalloc with -DMI_USE_CXX=ON or otherwise it triggers assertion failures.

Mmm, usually object files take precedence when resolving function names over library names so it may be a particular function is not overridden correctly with an archive. We may want to advice to not use a static library on MacOS (but the static object file or dylib). I'll try to figure out which function(s) is/are causing the issue.

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

3 participants