rust-lang-nursery / lazy-static.rs

A small macro for defining lazy evaluated static variables in Rust.
Apache License 2.0
1.91k stars 111 forks source link

Using macro twice in one file causes issues #77

Closed cptroot closed 7 years ago

cptroot commented 7 years ago

I'm not sure if this occurs only on the spin_no_std setting, but if you use the lazy static macro twice in one file like:

lazy_static! {
    static ref STATIC1: u32 = 1;
}
lazy_static! {
    static ref STATIC2: u32 = 2;
}

Will cause the hidden global variable that keeps track of initialization to become corrupted.

cptroot commented 7 years ago

Hmmmmm. I think I misdiagnosed an issue I'm having. I'm still getting the issue even after I combined my two lazy_static macros. Sorry for the bad issue report.

cptroot commented 7 years ago

For people in the future who are wondering what my issue was, it seems like I was blowing my stack (I was working on an OS kernel with very small stacks), due to the fact that the lazy_static puts the local variables on the stack (twice in debug mode). Since the variables I were modifying were the size of my entire stack, and because they get put on the stack no matter whether the closure has already run, I ended up writing a custom version of spin::Once to fix my problem with mutable references. This should be fixed in a better way once placement in becomes a thing on nightly.