Jarred Allen
Jarred Allen

Reputation: 362

Rust no_std find why global memory allocator is required

I'm writing Rust code for a platform which doesn't have support for std or alloc, so I have to use only core. Recently, my code has started producing this compile error:

error: no global memory allocator found but one is required; link to std or add `#[global_allocator]` to a static item that implements the GlobalAlloc trait

error: `#[alloc_error_handler]` function required, but not found

Is there a way I can get the rust compiler to tell me why a global memory allocator is required? Presumably, somewhere in my code or my dependencies something is attempting to allocate and that's causing it to go wrong, but I can't figure out where this is happening.

The only advice I saw online for this situation is to make a dummy custom allocator and then look through the compiled binary for references to it. However, I did this and am unable to find any reference to my custom allocator in the binary executable that it produced, which is making me more confused as to what's going on.

Upvotes: 6

Views: 1734

Answers (1)

Jarred Allen
Jarred Allen

Reputation: 362

For my case, it worked to use cargo vendor to download the source for all of my dependencies into a local directory, and then I used ripgrep to search through the dependencies for extern crate alloc;, which led me to one of my dependencies allocating without my knowledge. I don't know why the allocations didn't show up in the resulting binary, but this tool led me to the cause better than implementing a dummy allocator.

Upvotes: 2

Related Questions