skip to Main Content

Im currently stuck on some weird issues im not understanding.

I have compiled my program for unix with gcc and now im trying to run it, what happens is that
the program initializes a socket-io-service and crashes instantly because it tries to allocate memory with the boost pool.

Im trying to create a new instance of foo..

for (int i = 0; i <= 32; i++)
{
    foo* bar = new foo();
}

This is what happens when new is called:

void * operator new (size_t size)
{
    (void)size;
    return (void *)(mypool.malloc());  
}

If i use malloc(size_t) instead of m_pool.malloc() everything starts up fine..

Things i used:
GCC v11
Boost 1.59
Boost 1.65
Boost 1.81

CentOs 7
Ubuntu 22.04

I dont understand what is happening here, is it maybe some issue with x32/x64?

Or could it be the compiler?

I hope you guys can help, if needed more information please ask

I tried different systems, different boost version, i also created a simple program and manually tried the pool.. it worked

2

Answers


  1. Chosen as BEST ANSWER

    I found the issue!

    The program uses a library which runs the critical code, and what happened was that this code:

    for (int i = 0; i <= 32; i++)
    {
        foo* bar = new foo;
    }
    

    Got executed in the constructor from a class which is a singleton.

    The singleton was declared like this in the header:

    static xy__instance;
    static xy* instance()
    {
        return &__instance;
    }
    

    So as the program was starting the frist thing that happenend was creating an instance of the singleton from the library.. (see the startup below..)

    #9 0x7f4502d58eba in __libc_start_main_impl ../csu/libc-start.c:379
    #10 0x555db2ddaa34 in _start (/home/program+0x70a34)
    
    #0 0x7f450336f5f7 in operator new[](unsigned long, std::nothrow_t const&) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:108
    #1 0x555db2deb8d1 in boost::default_user_allocator_new_delete::malloc(unsigned long) /usr/local/include/boost-1_65_1/boost/pool/pool.hpp:97
    #2 0x555db2ded1a9 in boost::pool<boost::default_user_allocator_new_delete>::malloc_need_resize() /usr/local/boost_1_65_1/boost/pool/pool.hpp:693
    #3 0x555db2dec894 in boost::pool<boost::default_user_allocator_new_delete>::malloc() /usr/local/include/boost-1_65_1/boost/pool/pool.hpp:431
    #4 0x555db2e48424 in MemoryPoo::operator new(unsigned long) (/home/program+0xde424)
    #5 0x555db2e3a4fb in mySingleton::mySingleton() /home/xy.cpp:23
    #6 0x555db2e3e970 in __static_initialization_and_destruction_0 /home/xy.cpp:48
    #7 0x555db2e3f095 in _GLOBAL__sub_I__ZN3bnfC2Ev /home/xy.cpp:686
    #8 0x7f4502d58eba in call_init ../csu/libc-start.c:145
    #9 0x7f4502d58eba in __libc_start_main_impl ../csu/libc-start.c:379
    

    So my thought was: A memory allocation can't happen if there is no main-thread yet.. please correct me if im wrong here because im not sure!

    And then i changed the singleton to this:

    static xy* instance()
    {
        static xy __instance;
        return &__instance;
    }
    

    And it worked!

    I think that the instance was created immediatly because it was directly in the header and that the current gcc-compiler handles it different then in older versions..

    Note that i migrated that project from C++98. Now it runs fine and i can finally use the benefits of newer C++ versions!


  2. gcc44 "centos 6.10" it works outside of the function like

    static xy__instance;
    static xy* instance()
    {
        return &__instance;
    }
    

    gcc48 and above? "centos 7.9 core" gcc 4.8.5
    some reason wants it inside the function.

    Thank you for this <3 saved me a headache compiling haha

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search