C ++:什么时候需要std :: vector的共享内存分配器? [英] C++ : When do I need a shared memory allocator for std::vector?

查看:139
本文介绍了C ++:什么时候需要std :: vector的共享内存分配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

First_Layer



我有一个win32 dll写在VC ++ 6服务包6.让我们称这个dll为FirstLayer。我没有访问FirstLayer的源代码,但我需要从托管代码调用它。问题是FirstLayer大量使用std :: vector和std :: string作为函数参数,没有办法直接将这些类型合并到C#应用程序中。



Second_Layer



我可以想到的解决方案是首先创建另一个在VC ++ 6服务包6中编写的win32 dll。让我们调用这个dll作为SecondLayer。 SecondLayer充当FirstLayer的包装器。此层包含std :: vector的包装类,因此std :: vector不会在此层的所有函数参数中公开。让我们将std :: vector的这个包装类称为StdVectorWrapper。



此图层不使用任何新的或删除操作来分配或释放内存,因为这是由std :: vector内部处理的。



Third_Layer



我还创建了一个VC ++ 2005类库SecondLayer的包装器。这个包装器完成将非托管SecondLayer转换为托管代码的所有脏工作。让我们将此层称为ThirdLayer。



与SecondLayer类似,此图层在处理StdVectorWrapper时不使用新和删除。



strong> Fourth_Layer



最后,我创建了一个C#2005控制台应用程序来调用ThirdLayer。让我们将此C#控制台应用程序称为FourthLayer。



调用序列汇总



FourthLayer(C#2005) - > ThirdLayer(VC ++ 2005) - > SecondLayer(VC ++ 6) - > FirstLayer(VC ++ 6)



strong>问题



我注意到 System.AccessViolationException:尝试读取或写入受保护的内存抛出,我怀疑是由于SecondLayer的内部std :: vector分配内存,这是非法的ThirdLayer访问。



这是确认我认为,因为当我在VC ++ 2005中重新编译FirstLayer(模拟)和SecondLayer时,问题完全消失。但是,重新编译的第一层的生产版本是不可能的,因为我没有源代码。



我听说为了摆脱这个问题,我需要在C ++中为SecondLayer的std :: vector写一个共享内存分配器,它在StdVectorWrapper类中找到。我不完全明白为什么我需要一个共享内存分配器,它是如何工作的?任何想法?



在互联网上有没有任何现成的源代码,我可以编译和使用我的代码在SecondLayer?



请注意,我无法使用boost库。

解决方案

我找到了一个问题的解决方案。基本上,我写的StdVectorWrapper类不实现深度复制。所以我需要做的是将以下内容添加到StdVectorWrapper类中以实现深层复制。




  • 复制构造函数

  • 分配操作员

  • 解构函数



更好的解决方案是使用 clone_ptr 用于std :: vector中包含的所有元素以及std :: vector本身。这消除了对复制构造函数,赋值运算符和解构函数的需要。


First_Layer

I have a win32 dll written in VC++6 service pack 6. Let's call this dll as FirstLayer. I do not have access to FirstLayer's source code but I need to call it from managed code. The problem is that FirstLayer makes heavy use of std::vector and std::string as function arguments and there is no way of marshaling these types into a C# application directly.

Second_Layer

The solution that I can think of is to first create another win32 dll written in VC++6 service pack 6. Let's call this dll as "SecondLayer". SecondLayer acts as a wrapper for FirstLayer. This layer contains wrapper classes for std::vector so std::vector is not exposed in all function parameters in this layer. Let's call this wrapper class for std::vector as StdVectorWrapper.

This layer does not make use of any new or delete operations to allocate or deallocate memory since this is handled by std::vector internally.

Third_Layer

I also created a VC++2005 class library as a wrapper for SecondLayer. This wrapper does all the dirty work of converting the unmanaged SecondLayer into managed code. Let's call this layer as "ThirdLayer".

Similar to SecondLayer, this layer does not make use of new and delete when dealing with StdVectorWrapper.

Fourth_Layer

To top it all, I created a C#2005 console application to call ThirdLayer. Let's call this C# console application as "FourthLayer".

Call Sequence Summary

FourthLayer(C#2005) -> ThirdLayer(VC++2005) -> SecondLayer(VC++6) -> FirstLayer(VC++6)

The Problem

I noticed that the "System.AccessViolationException: Attempted to read or write protected memory" exception is being thrown which I suspect to be due to SecondLayer's internal std::vector allocating memory which is illegal for ThirdLayer to access.

This is confirmed I think because when I recompile FirstLayer (simulated) and SecondLayer in VC++2005, the problem disappears completely. However, recompiling the production version of FirstLayer is not possible as I do not have the source code.

I have heard that in order to get rid of this problem, I need to write a shared memory allocator in C++ for SecondLayer's std::vector which is found in the StdVectorWrapper class. I do not fully understand why I need a shared memory allocator and how it works? Any idea?

Is there any readily available source code for this on the internet that I can just compile and use together with my code in SecondLayer?

Note that I am unable to use the boost library for this.

解决方案

I have found a solution for the problem. Basically, the StdVectorWrapper class which I wrote do not implement deep copy. So all I need to do is to add the following to the StdVectorWrapper class to implement deep copy.

  • Copy Constructor
  • Assignment Operator
  • Deconstructor

Edit: Alternative Solution

An even better solution would be to use clone_ptr for all the elements contained in std::vector as well as for std::vector itself. This eliminates the need for the copy constructor, assignment operator and deconstructor.

这篇关于C ++:什么时候需要std :: vector的共享内存分配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆