comp.lang.ada
 help / color / mirror / Atom feed
* Anonymous access types
@ 2007-06-08  9:29 adam.betts155
  2007-06-08 12:01 ` Dmitry A. Kazakov
  0 siblings, 1 reply; 4+ messages in thread
From: adam.betts155 @ 2007-06-08  9:29 UTC (permalink / raw)


I am having some trouble with anonymous access types, behaviour which
I do not understand. I have 2 questions:

1) This one is bizarre and quite non-deterministic. I *sometimes* get
a "finalize/adjust raised exception" with the following bit of code:

declare
      t_ptr : tree_pointer := new tree'(get_tree(g));
      anon_t_ptr : access tree'class := new tree'(get_tree(g));
      lca : least_common_ancestor(anon_t_ptr);
begin
end;

with the following type declarations:
 tree_pointer is access tree'class;
 type least_common_ancestor (tree_ptr: access constant tree'class);

It seems that there is sometimes a problem with the lca declaration,
since the exception is never raised when anon_t_ptr is replaced with
t_ptr.

2) I want to return an anonymous access type from a function:

function get_tree (g: graph) return non null access tree is
begin
  return new tree(g.t);
end get_tree;

The access type is not null when I use this function. However, when I
run the code and try to do operations on the access type, it is not
pointing in the right place. For example, the returned tree does not
have vertices and edges even though I know it does.

Any help with either of these issues would be greatly appreciated.




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Anonymous access types
  2007-06-08  9:29 Anonymous access types adam.betts155
@ 2007-06-08 12:01 ` Dmitry A. Kazakov
  2007-06-08 12:09   ` adam.betts155
  0 siblings, 1 reply; 4+ messages in thread
From: Dmitry A. Kazakov @ 2007-06-08 12:01 UTC (permalink / raw)


On Fri, 08 Jun 2007 02:29:48 -0700, adam.betts155@gmail.com wrote:

> I am having some trouble with anonymous access types, behaviour which
> I do not understand. I have 2 questions:
> 
> 1) This one is bizarre and quite non-deterministic. I *sometimes* get
> a "finalize/adjust raised exception" with the following bit of code:
> 
> declare
>       t_ptr : tree_pointer := new tree'(get_tree(g));
>       anon_t_ptr : access tree'class := new tree'(get_tree(g));
>       lca : least_common_ancestor(anon_t_ptr);
> begin
> end;

This leaks, where these objects get deallocated?

> with the following type declarations:
>  tree_pointer is access tree'class;
>  type least_common_ancestor (tree_ptr: access constant tree'class);
> 
> It seems that there is sometimes a problem with the lca declaration,
> since the exception is never raised when anon_t_ptr is replaced with
> t_ptr.

I guess it is because of accessibility checks. t_ptr and anon_t_ptr have
different accessibility levels. When you call to least_common_ancestor,
which probably has its parameter of some access type declared in an
enclosing scope, then anon_t_ptr being converted fails on accessibility
check. In the result you get a snowball of misleading secondary faults.

(never use pointers if you can)

> 2) I want to return an anonymous access type from a function:
> 
> function get_tree (g: graph) return non null access tree is
> begin
>   return new tree(g.t);
> end get_tree;
> 
> The access type is not null when I use this function. However, when I
> run the code and try to do operations on the access type, it is not
> pointing in the right place. For example, the returned tree does not
> have vertices and edges even though I know it does.

   new tree(g.t);

does not initialize the object beyond standard initialization. You probably
meant

   new tree'(<some initial value>);

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



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Anonymous access types
  2007-06-08 12:01 ` Dmitry A. Kazakov
@ 2007-06-08 12:09   ` adam.betts155
  2007-06-08 18:08     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 4+ messages in thread
From: adam.betts155 @ 2007-06-08 12:09 UTC (permalink / raw)


On Jun 8, 1:01 pm, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
wrote:
> On Fri, 08 Jun 2007 02:29:48 -0700, adam.betts...@gmail.com wrote:
> > I am having some trouble with anonymous access types, behaviour which
> > I do not understand. I have 2 questions:
>
> > 1) This one is bizarre and quite non-deterministic. I *sometimes* get
> > a "finalize/adjust raised exception" with the following bit of code:
>
> > declare
> >       t_ptr : tree_pointer := new tree'(get_tree(g));
> >       anon_t_ptr : access tree'class := new tree'(get_tree(g));
> >       lca : least_common_ancestor(anon_t_ptr);
> > begin
> > end;
>
> This leaks, where these objects get deallocated?

It was a deallocation problem, thanks

>
> > with the following type declarations:
> >  tree_pointer is access tree'class;
> >  type least_common_ancestor (tree_ptr: access constant tree'class);
>
> > It seems that there is sometimes a problem with the lca declaration,
> > since the exception is never raised when anon_t_ptr is replaced with
> > t_ptr.
>
> I guess it is because of accessibility checks. t_ptr and anon_t_ptr have
> different accessibility levels. When you call to least_common_ancestor,
> which probably has its parameter of some access type declared in an
> enclosing scope, then anon_t_ptr being converted fails on accessibility
> check. In the result you get a snowball of misleading secondary faults.
>
> (never use pointers if you can)
>
> > 2) I want to return an anonymous access type from a function:
>
> > function get_tree (g: graph) return non null access tree is
> > begin
> >   return new tree(g.t);
> > end get_tree;
>
> > The access type is not null when I use this function. However, when I
> > run the code and try to do operations on the access type, it is not
> > pointing in the right place. For example, the returned tree does not
> > have vertices and edges even though I know it does.
>
>    new tree(g.t);
>
> does not initialize the object beyond standard initialization. You probably
> meant
>
>    new tree'(<some initial value>);
>

Yep, sorry, was that a typo on my part. I actually did have:
      new tree'(g.t);
and not:
      new tree(g.t);

Given that, why does the pointer not point to the correct place? Where
is it pointing to since it is clearly not null and must be pointing to
a variable of type tree?




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Anonymous access types
  2007-06-08 12:09   ` adam.betts155
@ 2007-06-08 18:08     ` Dmitry A. Kazakov
  0 siblings, 0 replies; 4+ messages in thread
From: Dmitry A. Kazakov @ 2007-06-08 18:08 UTC (permalink / raw)


On Fri, 08 Jun 2007 05:09:59 -0700, adam.betts155@gmail.com wrote:

> Yep, sorry, was that a typo on my part. I actually did have:
>       new tree'(g.t);
> and not:
>       new tree(g.t);
> 
> Given that, why does the pointer not point to the correct place? Where
> is it pointing to since it is clearly not null and must be pointing to
> a variable of type tree?

Why if it really does? Are you using debugger to verify that?

If g.t is controlled you might need to check what its Adjust does. Is it
deep or shallow copy?

BTW, it is a strange looking design. Provided that get_tree has the copy
semantics, why don't you return the copy, but a pointer to?

function get_tree (g: graph) return tree['Class] is
begin
  return g.t;
end get_tree;

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



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-06-08 18:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-08  9:29 Anonymous access types adam.betts155
2007-06-08 12:01 ` Dmitry A. Kazakov
2007-06-08 12:09   ` adam.betts155
2007-06-08 18:08     ` Dmitry A. Kazakov

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