comp.lang.ada
 help / color / mirror / Atom feed
* Possible Memory Leak
@ 2004-11-19 17:27 R A Matthews
  2004-11-22 23:00 ` Brian May
  2004-11-23 21:17 ` Randy Brukardt
  0 siblings, 2 replies; 6+ messages in thread
From: R A Matthews @ 2004-11-19 17:27 UTC (permalink / raw)


This is probably one for the language lawyers...

Consider a type having a pointer to itelf - that
is each variable of this type contains its own
address; it's not limited so it needs to be 
controlled - as exemplified by:

type My_Type;

type Access_My_Type is access all My_Type;

type My_Type is new Ada.Finalization.Controlled with
record
   Self : Access_My_Type;
end record;

Initialize will set Self, and Adjust will
maintain it.

In addition, the type can point to some data:

type My_Type is new Ada.Finalization.Controlled with
record
   Self : Access_My_Type;
   Data : Access_Some_Data_Type;
end record;

Each variable of My_Type points to its own set of data
so Adjust must copy that data and Finalize must deallocate
it.

Now the Annotated Reference Manual at 7.6(21 and 21.b) allows
a compiler to perform assignment on such a data type
via an intermediate temporary variable:

The temporary is overwritten from the source variable,
then Adjust called for the temporary.
The temporary is copied to the target variable then
Adjust is called for the target - but only if the data
type has an aliased component, so we need to
have our data type redeclared as:

type Inner_Type is
record
   Self : Access_My_Type;
   Data : Access_Some_Data_Type;
end record;

type My_Type is new Ada.Finalization.Controlled with
record
   Inner : aliased Inner_Type;
end record;

This ensures that the target of the assignment
is adjusted, so Self is set correctly and gives
the target its own copy of the referenced data.

BUT the Standard says that the temporary variable need
not be finalized, in which case the temporary's
data is not deallocated and so we have a memory leak.

It seems that the Standard is OK when we have
1) Just the Self component, or
2) Just the Data component, though without the use
   of "aliased".

However the Standard is not OK when we have:
3) Both Self and Data (whether or not we have
   used "aliased").
4) Just the Data component and also have an
   aliased component.

Is the Standard wrong?

Enlightenment please!

Robert A. Matthews

ada at ramatthews dot free-online dot co dot uk



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

end of thread, other threads:[~2004-11-25 17:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-19 17:27 Possible Memory Leak R A Matthews
2004-11-22 23:00 ` Brian May
2004-11-23 19:09   ` RAMatthews
2004-11-23 21:21     ` Randy Brukardt
2004-11-23 21:17 ` Randy Brukardt
2004-11-25 17:49   ` RAMatthews

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