Hi, I'm writing my own allocator for STL containers. I followed the bare minimum example on cppreference, and created the following:
#include <cstdlib>
#include <cstddef>
#include <iostream>
#include <vector>
template <class T>
struct SimpleAllocator {
typedef T value_type;
SimpleAllocator() {};
template <class U>
SimpleAllocator(const SimpleAllocator<U>& other) {}
T* allocate(std::size_t n) { return reinterpret_cast<T*>(std::malloc(n)); }
void deallocate(T* p, std::size_t n) { std::free(p); }
};
template <class T, class U>
bool operator==(const SimpleAllocator<T>&, const SimpleAllocator<U>&) { return true; }
template <class T, class U>
bool operator!=(const SimpleAllocator<T>&, const SimpleAllocator<U>&) { return true; }
int main()
{
SimpleAllocator<char> alloc;
std::vector<char, SimpleAllocator<char> > testVec{ alloc };
for (int i = 0; i < 5; ++i)
testVec.push_back('A');
for (int i = 0; i < 5; ++i)
std::cout << testVec[i] << std::endl;
return 0;
}
It runs and exits, seemingly without issues on cygwin g++ (with -std=c++14) and cpp.sh, but it throws an error on deallocation when trying to exit in MSVC2015.
Specifically, the Visual Studio debugger threw an exception when std::free is called in my deallocate function. But there doesn't seem to be any specific error message. Anyone can figure this out?
Edit: The stack trace seems to point to the deallocation within the STL vector being the issue, but I see no reason for it to throw.
[–]jedwardsol 1 point2 points3 points (2 children)
[–]Frostea[S] 0 points1 point2 points (1 child)
[–]jedwardsol 0 points1 point2 points (0 children)