Strange access violation at vector indexer
At first some introductions: I am currently working on a C++ compatibility thing which means being able to run projects with different compiler options with each other. Therefore I test wi开发者_如何学运维th a Release DLL and a Debug application linking to that other project. Most of the problems come up when using STL so I have to assure that both projects only use their own version of the STL. Thatswhy I have a wrapper class that can be build out of std::vectors, std::lists and so on but contains only an array which is completely compatible. Now I can wrap values in an array and unpack them on the other side into a valid STL object.
Now to get a bit closer to the question: There are some classes that contain STL but also need to be wrapped into an array. So I have to wrap the inner STL object, too, which means adding a tag and saving it right next to the associated array element.
Building up this wrapper class is no problem at all but unpacking it crashes with an access violation in the vector class right here:
const_reference operator[](size_type _Pos) const
{ // subscript nonmutable sequence
#if _HAS_ITERATOR_DEBUGGING
if (size() <= _Pos)
{
_DEBUG_ERROR("vector subscript out of range");
_SCL_SECURE_OUT_OF_RANGE;
}
#endif /* _HAS_ITERATOR_DEBUGGING */
_SCL_SECURE_VALIDATE_RANGE(_Pos < size());
return (*(_Myfirst + _Pos)); <---- HERE
}
The executing code at that moment is this:
template<class T>
struct mwContainerItem
{
T m_element;
void * m_tag;
};
template<class T>
class mwContainer
{
STLList ToList()
{
STLList l;
for(size_t i=0; i<m_size; ++i) <---- It crashes when accessing m_size
{
l.push_back(m_elements[i].m_element); <---- It also crashes when accessing m_elements
}
return l;
}
mwContainerItem<T>* m_elements;
size_t m_size;
};
The curious thing about it is I'm unpacking a std::list but it crashes in std::vector. Viewing at the whone thing I have a class contained by a std::vector and this very class contains a std::list of some basic class without STL. So unpacking means copying the outer array into a std::vector and every inner array into a std::list.
This error only occurs when I use the different compiler options, packing und unpacking in the very same project works just fine.
I really hope that anyone can help me cause I don't have any idea.
Regards
Do you really need two versions of STL? I mean, are you building the two projects with two different compilers, and two different STL implementations? When mixing debug and release versions, the problem usually comes from having two different heaps. Then, trying to release memory allocated in one module in the other, will cause an error. If this is the case you're facing, you could try another approach - have both use the same heap.
If you have control over both projects, you can export an allocator (and matching deallocator) from the DLL, and use it in the EXE. This way, memory management will be done on a single heap, and build type won't matter. You can use it in operator new, in a vector/list allocator etc.
This will probably not solve packing issues (who changes packing settings anyway?...), but it is something you want when using more than one heap.
精彩评论