ddovod / jet-live

c++ hot code reload for linux and macos
MIT License
411 stars 23 forks source link

global object may cause double free or corruption #50

Open demon90s opened 1 year ago

demon90s commented 1 year ago

If we add some global object like "std::vector v = { "1", "2", "3" };" in our cpp file, which will be compiled later at runtime. Now we reload the project, everything is ok.

Then, our program exit normally(not by signal), it will be crashed with some message like:

Error in `./main': double free or corruption (fasttop): 0x00000000019aeac0 ======= Backtrace: ========= /lib64/libc.so.6(+0x81329)[0x7ff37cb07329] /root/jet-live_test/build/lib_reload1.so(_ZN9__gnu_cxx13new_allocatorISsE10deallocateEPSsm+0x20)[0xc269f0] /root/jet-live_test/build/lib_reload1.so(_ZNSt16allocator_traitsISaISsEE10deallocateERS0_PSsm+0x2b)[0xc268db] .....

ddovod commented 4 months ago

Sorry, somehow I missed your question, probably was too busy on my primary work.

It's important to note what compiler is used, I noticed that on some gcc versions there're weird issues happening here and there. I just checked this case on clang 18.1.8 on xubuntu 24.04, works fine

std::vector<std::string> arr = { "1", "2", "3" };

int main() {
    std::string msg;
    while (true) {
        for (const auto& el : arr) {
            msg += el;
        }
        std::cout << msg << std::endl;
    }
}

I'm getting "123" log messages, but when I change the arr values to 3 2 1 and reload the code, I get 321 messages and no crashes