问题
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
回答1:
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
}
}
来源:https://stackoverflow.com/questions/12288660/segmentation-fault-core-dumped-on-server-side-in-corba-c-java-application