在CORBA C ++ / Java应用程序中的服务器端的分段故障(核心转储) [英] Segmentation fault (core dumped) on Server side in CORBA C++/Java application
问题描述
我有这样的代码:
interface Employee
{
string getLastname
};
#includeEmployee.idl
interface Work
{
Employee getEmployee(简称ID);
};
服务器文件:
#includeEmployee.hh
class EmployeeImpl:public POA_Employee
{
private:
char *姓;
int id;
public:
EmployeeImpl(const char * lastname,int id);
char * getLastname();
};
#includeEmployeeImpl.h
EmployeeImpl :: EmployeeImpl(const char * lastname,int id)
{
this-> lastname = const_cast< char *>(lastname);
this-> id = id;
}
char * EmployeeImpl :: getLastname()
{
return this-> lastname;
}
#includeWork.hh
#include< vector>
#includeEmployeeImpl.h
using namespace std;
class WorkImpl:public POA_Work
{
private:
vector< EmployeeImpl>雇员;
public:
WorkImpl();
Employee_ptr getEmployee(:: CORBA :: Short id);
};
#includeWorkImpl.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]。
}
客户档案:
$ b
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(Creating while 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; //这是问题
}
您需要返回动态分配的内存,因为骨架代码将通过线路将其调度到客户端之前释放它( CORBA :: string_free
)。
这应该是:
char * EmployeeImpl :: getLastname()
{
return CORBA: :string_dup(this-> lastname);
}
这在Henning& Vinowski book使用C ++进行高级CORBA编程
。你所遇到的另一个问题是你用一个基于1的索引索引到你的矢量。但是vector使用基于0的索引方案。要解决此问题,请更改您的客户端调用,或更改您的服务器实现,如下所示:
Employee_ptr WorkImpl :: getEmployee :: CORBA :: Short id)
{
if(id> = 1&& id< = employees.size())
{
return employees [ id-1] ._ this(); //转换为基于0的索引
}
else
{
//抛出某种类型的异常
}
}
I have such code:
interface Employee
{
string getLastname();
};
#include "Employee.idl"
interface Work
{
Employee getEmployee(in short id);
};
Server files:
#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();
}
Client files:
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()); }
}
}
when I run Server, and then client, on client side I see:
Smith
instead of:
Doe Smith Brown
and when the client got the message, on server side I see:
segmentation fault (cope dumped)
and server crashes. Whats wrong with my code guys? I use omniORB and idlj on Kubuntu, and g++ and javac to compile my files.
Heres my whole project: http://www44.zippyshare.com/v/60244821/file.html
You aren't following the IDL to C++ mapping rules about parameter passing. In particular, on the server:
char* EmployeeImpl::getLastname()
{
return this->lastname; // this is the problem
}
You need to return dynamically allocated memory because the skeleton code is going to deallocate it (with CORBA::string_free
) after it marshals it over the wire to the client.
This should be:
char* EmployeeImpl::getLastname()
{
return CORBA::string_dup(this->lastname);
}
This is all explained in the Henning & Vinowski book Advanced CORBA Programming with C++.
The other problem you are having is you are indexing into your vector with a 1-based index. But vector uses a 0-based index scheme. To fix this, either change your client calls, or change your server implementation to something like this:
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
}
}
这篇关于在CORBA C ++ / Java应用程序中的服务器端的分段故障(核心转储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!