CORBA C++/Java应用程序中服务器端的分段故障(核心转储)

Segmentation fault (core dumped) on Server side in CORBA C++/Java application

本文关键字:故障 分段 核心 转储 服务器端 C++ Java 应用程序 CORBA      更新时间:2023-10-16

我有这样的代码:

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
    }
}