CORBA C++/Java应用程序中服务器端的分段故障(核心转储)
Segmentation fault (core dumped) on Server side in CORBA C++/Java application
我有这样的代码:
interface Employee
{
string getLastname();
};
#include "Employee.idl"
interface Work
{
Employee getEmployee(in short id);
};
服务器文件:
#include "Employee.hh"
class EmployeeImpl : public POA_Employee
{
private:
char* lastname;
int id;
public:
EmployeeImpl(const char* lastname, int id);
char* getLastname();
};
#include "EmployeeImpl.h"
EmployeeImpl::EmployeeImpl(const char* lastname, int id)
{
this->lastname = const_cast<char*>(lastname);
this->id = id;
}
char* EmployeeImpl::getLastname()
{
return this->lastname;
}
#include "Work.hh"
#include <vector>
#include "EmployeeImpl.h"
using namespace std;
class WorkImpl : public POA_Work
{
private:
vector<EmployeeImpl> employees;
public:
WorkImpl();
Employee_ptr getEmployee(::CORBA::Short id);
};
#include "WorkImpl.h"
WorkImpl::WorkImpl()
{
EmployeeImpl ei1("Doe", 1);
EmployeeImpl ei2("Smith", 2);
EmployeeImpl ei3("Brown", 3);
employees.push_back(ei1);
employees.push_back(ei2);
employees.push_back(ei3);
}
Employee_ptr WorkImpl::getEmployee(::CORBA::Short id)
{
return employees[id]._this();
}
客户端文件:
import java.util.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;
public class Client
{
public static void main(String [] args)
{
try
{
org.omg.CORBA.ORB clientORB = org.omg.CORBA.ORB.init(args, null);
if (clientORB == null)
{
System.out.println("Problem while creating ORB");
System.exit(1);
}
org.omg.CORBA.Object objRef = clientORB.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
Work work = WorkHelper.narrow(ncRef.resolve_str("WorkService"));
Employee e = work.getEmployee((short)1);
System.out.println(e.getLastname());
e = work.getEmployee((short)2);
System.out.println(e.getLastname());
e = work.getEmployee((short)3);
System.out.println(e.getLastname());
}catch(Exception e){ System.out.println(e.getMessage()); }
}
}
当我运行服务器,然后运行客户端时,在客户端我看到:
Smith
而不是:
Doe Smith Brown
当客户端收到消息时,在服务器端我看到:
分段故障(作用域转储)
以及服务器崩溃。我的代码人员出了什么问题?我在Kubuntu上使用omniORB和idlj,并使用g++和javac来编译我的文件。
这是我的整个项目:http://www44.zippyshare.com/v/60244821/file.html
关于参数传递,您没有遵循IDL到C++的映射规则。特别是在服务器上:
char* EmployeeImpl::getLastname()
{
return this->lastname; // this is the problem
}
您需要返回动态分配的内存,因为骨架代码将在通过导线将内存封送至客户端后解除分配内存(使用CORBA::string_free
)。
这应该是:
char* EmployeeImpl::getLastname()
{
return CORBA::string_dup(this->lastname);
}
这一切都在Henning&Vinowski的书用C++进行高级CORBA编程。
您遇到的另一个问题是,您正在使用基于1的索引对向量进行索引。但vector使用基于0的索引方案。要解决此问题,可以更改客户端调用,也可以将服务器实现更改为以下内容:
Employee_ptr WorkImpl::getEmployee(::CORBA::Short id)
{
if (id >= 1 && id <= employees.size())
{
return employees[id - 1]._this(); // convert to 0-based indexing
}
else
{
// throw some type of exception
}
}
相关文章:
- 分段故障(堆芯转储)矢量
- 数组的指针从不分段故障
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 分段故障 运行C++代码时出现 SIGSEGV
- 分段故障背包问题
- 分段故障 11,从类函数显示动态 C 字符串
- 面临分段故障 使用 ffmpeg 读取视频时,因为"pFormatCtx-> streams [i]-> codecpar"的地址0x00
- 在C++中,当指向删除和指向不同对象时,分段故障指针
- 对程序故障进行分段
- 分段故障说明
- 分段故障(核心转储)-不知道为什么
- 分段故障线程
- hiredis SET遇到分段故障
- 分段故障,合并排序算法
- 多线程程序中的分段故障和gdb回溯上的不完整信息
- 到达主C++之前分段故障
- 分段故障核心使用 IF流转储
- 使用向量的移动键盘排列(分段故障)
- 在二进制树插入和遍历期间,我得到了分段故障
- 分段故障在类之间返回整数