2012-09-05 1 views
1

나는 그런 코드를 가지고 :분할 오류 CORBA C의 서버 측에서 (코어 덤프) ++/Java 응용 프로그램

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

서버를 실행할 때 clien t는, 클라이언트 측에서 나는 참조 :

스미스에게 대신

:

Doe 
Smith 
Brown 

와 클라이언트가 메시지를 받았을 때, 서버 측에서 내가 참조 :

segmentation fault (co 덤프 됨)

및 서버가 충돌합니다. 내 코드 사람이 뭐라구? 나는 쿠분투에서 omniORB와 idlj를 사용하고 g ++과 javac을 사용하여 파일을 컴파일합니다.

을 Heres 내 전체 프로젝트 :http://www44.zippyshare.com/v/60244821/file.html

+1

업데이트 된 답변보기 –

답변

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

모든 C++으로 헤닝 & Vinowski 책 고급 CORBA 프로그래밍에 설명되어 있습니다.

다른 문제는 벡터에 1부터 시작하는 인덱스로 인덱싱하는 것입니다. 그러나 벡터는 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 
    } 
} 
+0

+1 및 +1을 추천합니다. –

+1

색인 생성 문제를보고 나서 제 대답을 업데이트했습니다. –

+0

@Brian Neal : 정말 고마워요. 나는 여전히 stackoverflow와 Corba에서 친절한 초보자이다 :) 그리고 때로는 나는 명백한 것을 보지 않는다 : P 감사, 그것은 물론 도움이되었다;) – yak

관련 문제