Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#15 STD allocations don't use registered callbacks

open-fixed
nobody
None
5
2011-07-14
2011-06-22
Anonymous
No

When using JSON_MEMORY_CALLBACKS, the memory allocations made by the standard library don't go through the registered callbacks, and instead use global operator new. A custom allocator should be passed to all standard library functions to ensure all allocations go through the registered callbacks.

Examples of what I had to change to make this happen:
typedef std::basic_string<char, std::char_traits<char>, my_allocator<char> > json_string;

And every time an std::map is used:
std::map<void *, void *, std::less<void*>, my_allocator<std::pair<const void*, void*> > > mymap;

Discussion

  • Sorry it took so long to respond, good idea, I'm adding that now.

     
  • Unfortunately, I've decided to push this to a later version. It's actually much harder than you would think to create a cross-platform, cross STL, allocator that's thread-safe.

     
  • I've written an allocator as the final addition to the next release. Once all the unit tests pass, STL will use your custom memory callbacks.

     
    • status: open --> closed-fixed
     
    • status: closed-fixed --> open-fixed
     
  • I just wanted to add that the original posters code is not a correct bug fix. There is undefined behavior in it. There are several global strings, which use the allocator, and the callbacks are also global. If they get initialized in the wrong order, a segfault will occur. The next release of libjson will account for this, this is why it's taking me a little longer than I originally thought.