comp.lang.ada
 help / color / mirror / Atom feed
From: markus.knauss@gmx.net (Markus Knauss)
Subject: Interfacing Ada95 to C++ - Object passing problem
Date: 25 Apr 2002 04:44:00 -0700
Date: 2002-04-25T11:44:00+00:00	[thread overview]
Message-ID: <6c12025b.0204250344.5944d770@posting.google.com> (raw)

Hello,

I'm going to use Ada95 libraries in C++. During evaluation of the Ada
interfacing capabilities I found an interessting problem when passing
objects.

I hope that anybody knows about the described problem and can me tell
where I made an error.

First I will introduce snippets of the sources and then describe the
problem and its solution.

The body and spec of the Ada implementation defines a class with a
single attribute. The attribute is an unbounded_string. The attribute
of an object of this class can be set with the "set" method. Below
excerpts from the spec and body file:

<snip (spec-file)>
type s_type is record
  name : ada.strings.unbounded.unbounded_string;
end record;
pragma convention (c, s_type);
type s_obj is access s_type;
<snap>

<snip (body-file)>
function create
  return s_obj is
begin
  return new s_type;
end create;

procedure set (obj : in out s_obj; name : string) is
begin
  obj.name := to_unbounded_string (name);
end set;
procedure set_c (obj : in out s_obj; name :
interfaces.c.pointers.chars_ptr) is
begin
  set (obj, value (name));
end set_c;
pragma export (c, set_c, "set");
<snap>
The class and the exported function is reflected in a C-Header file.
The class and function is accessed from a C++ main. Below are excerpts
of the header and the main file.

<snip (header file)>
struct s_type {};
typedef s_type* s_obj;
extern "C" s_obj create();
extern "C" void set(s_obj obj, char* name);
<snap>

<snip (file including main)>
extern "C" void adainit();
int main(int argc, char** argv) {
  adainit();
  s_obj obj = create();  set(obj, "Hello world!");
}
<snap>
The sources were compiled using gnat 3.13p and gcc 2.95.3.

Now the problem: When converting the string to an unbounded string, in
the set procedure, the program crashes with an segmentation fault. To
this point everything works fine. When omitting the attributes "in
out" to the obj parameter in the procedures set_c and set everything
works fine.

As I investigated, the Segmentation fault occurs when freeing some
memory in the implementation of To_Unbounded_String. I tested the same
implementation with Bounded_Strings and the result was: If using 'in
out' attributes for obj, the objects attribute was empty but
assignment and conversion worked. Omitting the 'in out' attributes
everything worked fine.

Does anyone know about this or knows why the attributes 'in out' must
be omitted?

Thanks for your answers,
  Markus



             reply	other threads:[~2002-04-25 11:44 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-04-25 11:44 Markus Knauss [this message]
2002-04-25 12:39 ` Interfacing Ada95 to C++ - Object passing problem Neil Butterworth
2002-04-25 17:50 ` Stephen Leake
2002-04-26  6:20   ` Markus Knauss
2002-04-27 13:50     ` Jacob Sparre Andersen
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox