Possible bug in using a stateful custom allocator in phmap::node_hash_map #277
Unanswered
dipanshu-grc
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Yes, the issue is that the hash_map is provided an allocator for the map's |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, was trying to use a stateful custom allocator in phmap::node_hash_map.
I am segmentation faults, possible because of memleaks and also compiler warning -
phmap_base.h:4160:10: error: function may return address of local variable [-Werror=return-local-addr] 4160 | return p;
On looking at
template <size_t Alignment, class Alloc> void* Allocate(Alloc* alloc, size_t n) { static_assert(Alignment > 0, ""); assert(n && "n must be positive"); struct alignas(Alignment) M {}; using A = typename phmap::allocator_traits<Alloc>::template rebind_alloc<M>; using AT = typename phmap::allocator_traits<Alloc>::template rebind_traits<M>; **A mem_alloc(*alloc);** void* p = &*AT::allocate(mem_alloc, (n + sizeof(M) - 1) / sizeof(M)); assert(reinterpret_cast<uintptr_t>(p) % Alignment == 0 && "allocator does not respect alignment"); return p; }
Is there a reason to create a local copy mem_alloc? This seems to be an issue in my case. Attaching my custom allocator as reference
`
template <typename T, std::size_t Capacity = 1024>
class FixedAllocator {
public:
using value_type = T;
private:
alignas(alignof(T)) char buffer_[Capacity * sizeof(T)];
void* freeList_ = nullptr;
};
`
Beta Was this translation helpful? Give feedback.
All reactions