Quote Originally Posted by marcel View Post
Regarding the segfaults:
Are you allocating from more than one thread.
AFAIK there shouldn't be ANY thread apart from the main/GUI one in the test case...

The global memory pool should be locked for exclusive access by the current allocating thread ( mem allocation is not atomic ). It is possible, if you use more than one thread, that two threads may try to allocate the same block in the mem pool, resulting in a mem violation -> therefore the segfault.
I'm afraid it's trickier than that... When I serialize access to the pool (for both alloc and dealloc) through QMutex I get a deadlock message and the application hangs...

The main function is the application entry point - it is called by the OS when you run a program. You can force something to be called before main, or after main exits.
That's not exactly true... For example, under Linux some other functions are called before main and after (mainly things related to global objects and added internally by the compiler but still...). I found an example on the web showing how to do that but it worked only with recent versions of MSVC so I just dropped it. Anyway singleton may help me here because it would ensure that the pool is created as soon as program asks for memory allocation.

Could we see your custom allocator/deallocator and how you use it? Maybe you can show us where exactly you get the segfaults...
Sure! But it probably won't help... As far as I investigated, everything is allocated and initialized properly but for some reason the pointer to one of the allocated QReadWriteLock is somewhere decremented by one byte causing the segfault... I don't know where this decrement is done and I've no idea why it occurs when my allocator is used...

@vermarajeev :
Your trick will work but there is no way to ensure that A::A() gets called BEFORE ALL OTHER STATIC CTORs and A::~A() AFTER ALL OTHER STATIC DTORs....