comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Preventing Unchecked_Deallocation?
Date: Mon, 6 Feb 2012 08:21:24 -0800 (PST)
Date: 2012-02-06T08:21:24-08:00	[thread overview]
Message-ID: <4350713b-6ac3-4b22-b221-8da2bac52fea@t5g2000yqk.googlegroups.com> (raw)
In-Reply-To: f4c34323-d0d8-4315-aaaa-31c0119e2ba4@n6g2000vbz.googlegroups.com

On Feb 5, 8:42 am, Simon Belmont <sbelmont...@gmail.com> wrote:
> On Feb 4, 9:40 am, AdaMagica <christ-usch.gr...@t-online.de> wrote:
>
> > Don't understand your problem. However, keep in mind that UD with a
> > type different from the one with which the objects were allocated is
> > erroneous.
>
> When you say erroneous, do you mean forbidden by the language (i.e. an
> exception) or that it will cause undefined operation?  My concern is
> that if a unit exposes an access value to other units, any of them may
> use UD to delete the object at any time.  Obviously this sort of
> behavior would cause the program to quickly crash due to a null
> pointer, but all things being equal I would prefer a compile-time
> mechanism to prevent the UD outright.

I think the solution is not to expose the access type, but have your
package define a private type.  Ada 2012 is supposed to have new
mechanisms that allow packages to define "dereference" operations that
could allow package users to use the same kind of syntax on objects of
private types that they can use on access types.  If you do this, then
it's likely that the package that defines the type can control what
operations are available and not make Unchecked_Deallocation
available.  However, I haven't yet studied the new feature so I can't
say for sure.

As a general rule, though, I believe that if a package does make an
access type visible, users of the package should not try to deallocate
any objects of the type themselves, when objects of the type are
supplied by the package.  Instead, the package should provide
operations to do any needed deallocations (ideally not a "Deallocate"
operation, but perhaps a "No Longer Needed" operation so that it's the
package that defines the type that decides when it's OK to
deallocate).  I don't think the language can prevent package users
from deallocating, except by using private types.  But I'd say that
doing your own Unchecked_Deallocation on an object allocated by
another package (if the documentation doesn't explicitly say it's OK)
is just poor programming, and there's a limit to how much Ada can do
to prevent poor programmers using Unchecked routines from doing
damage.  (Even if you used private types, Ada can't prevent a user
from using Unchecked_Conversion to convert to an access type and then
deallocating it.  And I think I've known some programmers who wouldn't
see anything wrong with doing that, if they believed it would solve
their problem.)

Conceivably, it might be possible to add a "not deallocatable" aspect
to an access type and add language rules to Unchecked_Deallocation and
to the rules on type conversions so that this could be prevent.
However, since the language seems to have been moving in the direction
of making it simpler not to have visible access types at all, I doubt
that ARG would deem worthwhile to add this kind of feature.

                                -- Adam



  parent reply	other threads:[~2012-02-06 17:22 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-02 23:41 Preventing Unchecked_Deallocation? Simon Belmont
2012-02-04 14:40 ` AdaMagica
2012-02-05 16:42   ` Simon Belmont
2012-02-06 12:52     ` Julian Leyh
2012-02-06 16:05       ` Adam Beneschan
2012-02-06 16:21     ` Adam Beneschan [this message]
2012-02-06 18:34       ` AdaMagica
2012-02-07  0:14       ` Simon Belmont
2012-02-07  2:19         ` Shark8
2012-02-07 16:39           ` Robert A Duff
2012-02-07 11:42         ` Gautier write-only
2012-02-07 21:11           ` Simon Belmont
2012-02-07 21:30             ` Robert A Duff
2012-02-07 22:23               ` Simon Wright
2012-02-07 23:07                 ` Robert A Duff
2012-02-08  8:43                   ` Simon Wright
2012-02-08 15:06                     ` Robert A Duff
2012-02-09  2:22                     ` Randy Brukardt
2012-02-09  7:28                       ` Simon Wright
2012-02-07 23:06               ` Alan Copeland
2012-02-07 23:10                 ` Robert A Duff
2012-02-09  2:49         ` Randy Brukardt
2012-02-09  3:39           ` Jeffrey Carter
2012-02-09 15:47             ` Adam Beneschan
2012-02-09 19:16               ` Jeffrey Carter
2012-02-09  7:37           ` Simon Wright
2012-02-10  1:08             ` Randy Brukardt
2012-02-10  7:35               ` Simon Wright
2012-02-07  6:26     ` Jeffrey Carter
2012-02-08  8:49       ` Maciej Sobczak
2012-02-08 23:40         ` BrianG
2012-02-09  2:57         ` Randy Brukardt
2012-02-09  7:13           ` Pascal Obry
2012-02-10  1:12             ` Randy Brukardt
2012-02-09  8:08           ` Maciej Sobczak
2012-02-10  1:18             ` Randy Brukardt
2012-02-07 16:27     ` Robert A Duff
replies disabled

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