From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,955187ea690ddfda,start X-Google-Attributes: gid109fba,public X-Google-Thread: 103376,955187ea690ddfda,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-04-25 04:44:00 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: markus.knauss@gmx.net (Markus Knauss) Newsgroups: comp.lang.ada,comp.lang.c++ Subject: Interfacing Ada95 to C++ - Object passing problem Date: 25 Apr 2002 04:44:00 -0700 Organization: http://groups.google.com/ Message-ID: <6c12025b.0204250344.5944d770@posting.google.com> NNTP-Posting-Host: 129.69.217.182 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1019735040 32324 127.0.0.1 (25 Apr 2002 11:44:00 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 25 Apr 2002 11:44:00 GMT Xref: archiver1.google.com comp.lang.ada:23101 comp.lang.c++:141675 Date: 2002-04-25T11:44:00+00:00 List-Id: 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: 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; 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"); 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. struct s_type {}; typedef s_type* s_obj; extern "C" s_obj create(); extern "C" void set(s_obj obj, char* name); extern "C" void adainit(); int main(int argc, char** argv) { adainit(); s_obj obj = create(); set(obj, "Hello world!"); } 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