【发布时间】:2026-01-16 16:55:01
【问题描述】:
我正在使用boost::interprocess 运行最简单的进程间通信程序设置:
#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib> //system
#include <iostream>
using namespace std;
using namespace boost::interprocess;
typedef pair<double, int> MyType;
int main(int argc, char *argv[]) {
if (argc==1) { //Parent process
struct shm_remove {
shm_remove() {shared_memory_object::remove("MySharedMemory");}
~shm_remove() {shared_memory_object::remove("MySharedMemory");}
} remover;
managed_shared_memory segment(create_only,"MySharedMemory",65536);
MyType* instance=segment.construct<MyType>("MyType instance")(0.5,2);
string s(argv[0]);
s+=" child ";
if(system(s.c_str())!=0) {
cout<<"Parent: Child process returned non-zero"<<endl;
return 1;
}
cout<<"Parent: Child process finished successfully"<<endl;
segment.destroy<MyType>("MyType instance");
} else { //Child process
pair<MyType*, managed_shared_memory::size_type> res;
// try {
managed_shared_memory segment(open_only, "MySharedMemory");
res=segment.find<MyType>("MyType instance");
// } catch (interprocess_exception &e) {
// cerr<<"Error while opening the segment"<<endl;
// return 1;
// }
cout<<"Child: Segment of length "<<res.second<<" is found at "<<res.first<<endl;
cout<<"Child: "<<res.first->first<<", "<<res.first->second<<endl;
}
return 0;
}
这行得通,我明白了:
Child: Segment of length 1 is found at 0x106a15148
Child: 0.5, 2
Parent: Child process finished successfully
但是,当我取消注释 try-catch 块时,我看到以下内容:
Child: Segment of length 1 is found at 0x10a8fd148
Parent: Child process returned non-zero
如果我将二进制文件分成两部分(生成段和永远休眠的父级和读取段的子级),它在没有 try-cacth 的情况下再次工作,但子级崩溃
Segmentation fault: 11
因此,我有两个问题:
-
try-catch的引入是什么原因?它的行为就像块有自己的地址空间一样。但为什么会这样呢? - 如何安全地检查所需段的存在并生成未找到的错误消息?
编辑:
我已将块更改为:
if (1) {
managed_shared_memory segment(open_only, "MySharedMemory");
res=segment.find<MyType>("MyType instance");
cout<<"Child: Segment of length "<<res.second<<" is found at "<<res.first<<endl;
cout<<"Child: "<<res.first->first<<", "<<res.first->second<<endl;
}
我遇到了同样的错误:
Child: Segment of length 1 is found at 0x108c15148
Child: 0.5, 2
Child: Segment of length 1 is found at 0x108c15148
Parent: Child process returned non-zero
因此,该行为确实是{}造成的,推测是segment被破坏所致。但是,如果 res.first 已经填充了正确的指针,这又有什么关系呢?
【问题讨论】:
-
segment析构函数是做什么的?看起来它释放了您稍后尝试访问的内容。