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

时间:2012-09-05 19:45:06

标签: c++ segmentation-fault corba omniorb

我有这样的代码:

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()); }
    }
}

当我在客户端运行Server,然后运行客户端时,我看到:

  

史密斯

而不是:

Doe
Smith
Brown

当客户端收到消息时,在服务器端我看到:

  

分段错误(应对倾销)

和服务器崩溃。我的代码家伙怎么了?我在Kubuntu上使用omniORB和idlj,使用g ++和javac来编译我的文件。

继承我的整个项目: http://www44.zippyshare.com/v/60244821/file.html

1 个答案:

答案 0 :(得分:5)

您没有遵循有关参数传递的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&amp; amp; 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
    }
}