在进程之间移动 STL 对象
我知道这很奇怪,但我只是玩得开心。
我正在尝试通过两台机器之间的套接字在两个进程之间传输 std::map
(使用固定内存区域中的放置新实例化):Master
和 奴隶
。我使用的地图具有以下 typedef
:
// A vector of Page objects
typedef
std::vector<Page*,
PageTableAllocator<Page*> >
PageVectorType;
// A mapping of binary 'ip address' to a PageVector
typedef
std::map<uint32_t,
PageVectorType*,
std::less<uint32_t>,
PageTableAllocator<std::pair<uint32_t, PageVectorType*> > >
PageTableType;
PageTableAllocator
类负责将 STL 容器可能想要/需要的任何内存分配到内存中的固定位置。例如,所有Page
对象和STL内部结构都在这个固定的内存区域中实例化。这确保了 std::map 对象和分配器都放置在固定的内存区域中。我使用 GDB 来确保映射和分配器行为正确(使用的所有内存都在固定区域中,应用程序的正常堆上没有任何内容)。
假设 Master 启动,初始化所有 STL 结构和特殊内存区域,会发生以下情况。 Slave
启动,打印出其页表版本,然后查找Master
。 Slave
找到一个 master,删除它的页表版本,复制 Master
的页表版本(以及特殊内存区域),然后成功< /strong> 将其打印出Master
的页表版本。根据我在 GDB 中所做的操作,我可以执行许多只读操作。
当尝试添加到新复制的 PageTableType
对象时,Slave
在分配器的 void 构造(指针 p,const T& value)
方法中出错。作为 p
传入的值指向已分配的内存区域(根据 Master
的 std::map
版本)。
我对 C++ 对象结构一无所知,但我猜测即使在我替换所有内容之后,Slave
版本的 PageTableType
中的对象状态也必须保留下来PageTableType
及其分配器使用的内存。 我的问题是这是否是一个合理的担忧。 C++ 是否在对象实例化的内存区域之外维护某种对象状态?
映射中使用的所有对象都是非 POD。对于分配器来说也是如此。
I know this is strange but I'm just having fun.
I am attempting to transmit a std::map
(instantiated using placement new in a fixed region of memory) between two processes via a socket between two machines: Master
and Slave
. The map I'm using has this typedef
:
// A vector of Page objects
typedef
std::vector<Page*,
PageTableAllocator<Page*> >
PageVectorType;
// A mapping of binary 'ip address' to a PageVector
typedef
std::map<uint32_t,
PageVectorType*,
std::less<uint32_t>,
PageTableAllocator<std::pair<uint32_t, PageVectorType*> > >
PageTableType;
The PageTableAllocator<T>
class is responsible for allocating whatever memory the STL containers may want/need into a fixed location in memory. E.g., all Page
objects and STL internal structures are being instantiated in this fixed memory region. This ensures that both the std::map
object and the allocator are both placed in a fixed region of memory. I've used GDB to make sure the map and allocator behave correctly (all memory used is in the fixed region, nothing ever goes on the application's normal heap).
Assuming Master
starts up, initializes all of it's STL structures and the special memory region, the following happens. Slave
starts, prints out its version of the page table, then looks for a Master
. Slave
finds a master, deletes its version of the page table, copies Master
's version of the page table (and the special memory region), and successfully prints it out the Master
's version of the page table. From what prodding I've done in GDB I can perform many read-only operations.
When trying to add to the newly copied PageTableType
object, Slave
faults in the allocator's void construct (pointer p, const T& value)
method. The value passed in as p
points to an already allocated area of memory (as per Master
's version of the std::map
).
I don't know anything about C++ object structure, but I'm guessing that object state from Slave
's version of the PageTableType
must be hanging around even after I replace all of the memory that the PageTableType
and its allocator used. My question is if this is a valid concern. Does C++ maintain some sort of object state outside of the area of memory that object was instantiate din?
All of the objects used in the map are non-POD. Same is true for the allocator.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答您的具体问题:
答案是否。没有设置其他数据结构来“跟踪”对象或任何类似的东西。 C++ 使用显式内存分配模型,因此如果您选择负责分配和释放,那么您就拥有完全的控制权。
我怀疑您的代码中的某个地方有问题,但既然您相信代码是正确的,您就发明了代码可能失败的其他原因,并遵循该路径。我会退后一步,仔细检查有关代码当前工作方式的所有内容,看看是否可以确定问题所在。尽管 STL 类很复杂(尤其是
std::map
),但它们最终只是代码,并且其中没有隐藏的魔法。To answer your specific question:
The answer is no. There are no other data structures set up to "track" objects or anything of the sort. C++ uses an explicit memory allocation model, so if you choose to be responsible for allocation and deallocation, then you have complete control.
I suspect there's something wrong in your code somewhere, but since you believe the code is correct you're inventing some other reason why your code might be failing, and following that path instead. I would pull back, and carefully examine everything about the way your code is working right now, and see if you can determine the problem. Although the STL classes are complex (especially
std::map
), they're ultimately just code and there is no hidden magic in there.