comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: generic provokes segmentation fault
Date: Mon, 18 May 2020 23:30:35 +0200
Date: 2020-05-18T23:30:35+02:00	[thread overview]
Message-ID: <r9uupp$32m$1@gioia.aioe.org> (raw)
In-Reply-To: hig8olFrhghU1@mid.individual.net

On 2020-05-18 22:16, hreba wrote:
> On 2020-05-18 14:36, Dmitry A. Kazakov wrote:
> 
>> What is global variables you are talking about? gsl_odeiv2_driver 
>> seems to be allocated and initialized using library functions.
> 
> Initially, I tried the following:
> 
>   - the library defines a type with that driver as a private component
>   - the client declares a variable of that type
>   - the client calls the library's initialization function, which
>     instanciates this variable (and the driver within)
> 
> I just was not able to get around that local/non-local object/pointer 
> problem, and for now the driver is a global variable hidden in the 
> library, initialized as a side effect of Init_Solve, as a workaround.

This what possibly crashes it because you don't not use official 
"constructors".

As I said it should be kind of

    type Driver is new Ada.Finalization.Limited_Controlled with private;
    procedure Apply (Object : in out Driver; ...);
    procedure Set (Object : in out Driver; ...); -- Set standard driver
    procedure Set (Object : in out Driver; ...); -- Set scaled driver
    ...
    overriding procedure Finalize (Object : in out Driver);
private
    type gsl_odeiv2_driver_struct is record ... end record;
    pragma Convention (C, gsl_odeiv2_driver_struct);
    type gsl_odeiv2_driver is access all gsl_odeiv2_driver_struct;
    pragma Convention (C, gsl_odeiv2_driver);

    type Driver is new Ada.Finalization.Limited_Controlled with record
       odeiv2 : gsl_odeiv2_driver;
    end record;

    procedure Set (Object : in out Driver; ...) is
    begin
       if Object.odeiv2 /= null then              -- Delete old, if any
          gsl_odeiv2_driver_free (Object.odeiv2); -- Could store and
          Object.odeiv2 := null;                  -- check its type
       end if;
       Object.odeiv2 := gsl_odeiv2_driver_alloc_standard_new (...);
    end Set ;

    procedure Finalize (Object : in out Driver) is
    begin
       if Object.odeiv2 /= null then
          gsl_odeiv2_driver_free (Object.odeiv2);
          Object.odeiv2 := null;
       end if;
    end Initialize;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  reply	other threads:[~2020-05-18 21:30 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-17 14:19 generic provokes segmentation fault hreba
2020-05-17 17:51 ` Dennis Lee Bieber
2020-05-17 18:34 ` Dmitry A. Kazakov
2020-05-18 11:29   ` hreba
2020-05-18 12:36     ` Dmitry A. Kazakov
2020-05-18 20:16       ` hreba
2020-05-18 21:30         ` Dmitry A. Kazakov [this message]
2020-05-17 21:02 ` Jeffrey R. Carter
2020-05-18 20:21   ` hreba
replies disabled

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