矢量索引器出现奇怪的访问冲突

发布于 2024-11-19 00:16:13 字数 1621 浏览 7 评论 0原文

首先进行一些介绍:我目前正在研究 C++ 兼容性,这意味着能够运行具有不同编译器选项的项目。因此,我使用链接到该其他项目的发布 DLL 和调试应用程序进行测试。 大多数问题都是在使用 STL 时出现的,因此我必须确保两个项目只使用自己版本的 STL。 这就是为什么我有一个包装类,它可以用 std::vectors、std::lists 等构建,但只包含一个完全兼容的数组。现在我可以将值包装在一个数组中,并在另一侧将它们解包到一个有效的 STL 对象中。

现在更接近这个问题:有一些类包含 STL,但也需要包装到数组中。因此,我还必须包装内部 STL 对象,这意味着添加一个 标签 并将其保存在关联的数组元素旁边。

构建这个包装类根本没有问题,但是解压它会因向量类中的访问冲突而崩溃:

   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
        }

当时的执行代码是这样的:

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;
};

奇怪的是我正在解压一个 std::list 但它在 std::vector 中崩溃。查看整个事情,我有一个由 std::vector 包含的类,并且这个类包含一些没有 STL 的基本类的 std::list 。因此,解包意味着将外部数组复制到 std::vector 中,并将每个内部数组复制到 std::list 中。

仅当我使用不同的编译器选项时才会出现此错误,在同一个项目中打包和解包效果很好。

我真的希望任何人都可以帮助我,因为我没有任何想法。

问候

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 with 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏夜暖风 2024-11-26 00:16:13

您真的需要两个版本的 STL 吗?我的意思是,您是否使用两个不同的编译器和两个不同的 STL 实现来构建这两个项目?当混合调试和发布版本时,问题通常来自于有两个不同的堆。然后,尝试在另一个模块中释放一个模块中分配的内存时,将导致错误。如果您遇到这种情况,您可以尝试另一种方法 - 让两者使用相同的堆。

如果您可以控制这两个项目,则可以从 DLL 导出分配器(和匹配的解除分配器),并在 EXE 中使用它。这样,内存管理将在单个堆上完成,并且构建类型并不重要。您可以在矢量/列表分配器中的 operator new 中使用它 这可能

无法解决打包问题(谁会更改打包设置?...),但在使用多个堆时这是您想要的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文