comp.lang.ada
 help / color / mirror / Atom feed
* deallocating class wide types
@ 2003-05-10 12:02 alfonso acosta
  2003-05-10 12:11 ` alfonso acosta
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: alfonso acosta @ 2003-05-10 12:02 UTC (permalink / raw)


hi:

I provide a simplified example of my problem:

package spec:
----------------------------------------------------------------
package My_Class is

type My_Class is abstract tagged null record;
type My_Class_Access is access all My_Class'Class;
procedure Destroy (Object: in out My_Class_Access);

end My_Class;
----------------------------------------------------------------

first body approach:
----------------------------------------------------------------
with Ada.Unchecked_Deallocation;
package body My_Class is
procedure Free is
    new Ada.Unchecked_Deallocation(Name => My_Class_Access,
				  Object => My_Class'Class);

procedure Destroy (Object: in out My_Class_Access) renames Free;

end My_Class;
----------------------------------------------------------------

I get:
my_class.adb:7:59: subprogram used in renaming_as_body cannot be intrinsic

second approach:
----------------------------------------------------------------
with Ada.Unchecked_Deallocation;
package body My_Class is
procedure Free is
    new Ada.Unchecked_Deallocation(Name => My_Class_Access,
				  Object => My_Class'Class);

procedure Destroy (Object: in out My_Class_Access) is
begin
    Free(My_Class_Access);
end Destroy;

end My_Class;
------------------------------------------------------------------

I get:
my_class.adb:9:09: Invalid use of subtype mark in expression or call

Can someone tell me what Im doing wrong?
And, should Destroy work with any type which extends My_Class?

Thanks in advance:

Alfonso Acosta




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

* Re: deallocating class wide types
  2003-05-10 12:02 deallocating class wide types alfonso acosta
@ 2003-05-10 12:11 ` alfonso acosta
  2003-05-10 12:41 ` David C. Hoos
  2003-05-12  0:12 ` Robert I. Eachus
  2 siblings, 0 replies; 4+ messages in thread
From: alfonso acosta @ 2003-05-10 12:11 UTC (permalink / raw)


alfonso acosta wrote:
> procedure Destroy (Object: in out My_Class_Access) is
> begin
>    Free(My_Class_Access);
> end Destroy;
> 
> end My_Class;
> ------------------------------------------------------------------
> 
> I get:
> my_class.adb:9:09: Invalid use of subtype mark in expression or call

Kind of stupid here, Free(Object) works fine (of course :$)


> And, should Destroy work with any type which extends My_Class?
> 
> Thanks in advance:
> 
> Alfonso Acosta
> 




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

* Re: deallocating class wide types
  2003-05-10 12:02 deallocating class wide types alfonso acosta
  2003-05-10 12:11 ` alfonso acosta
@ 2003-05-10 12:41 ` David C. Hoos
  2003-05-12  0:12 ` Robert I. Eachus
  2 siblings, 0 replies; 4+ messages in thread
From: David C. Hoos @ 2003-05-10 12:41 UTC (permalink / raw)



"alfonso acosta" <alfonso_acosta_mail@yahoo.es> wrote in message
news:3ebcea42$1_4@news.arrakis.es...
> hi:
>
> I provide a simplified example of my problem:
>
> package spec:
> ----------------------------------------------------------------
> package My_Class is
>
> type My_Class is abstract tagged null record;
> type My_Class_Access is access all My_Class'Class;
> procedure Destroy (Object: in out My_Class_Access);
>
> end My_Class;
> ----------------------------------------------------------------
>
> first body approach:
> ----------------------------------------------------------------
> with Ada.Unchecked_Deallocation;
> package body My_Class is
> procedure Free is
>     new Ada.Unchecked_Deallocation(Name => My_Class_Access,
>   Object => My_Class'Class);
>
> procedure Destroy (Object: in out My_Class_Access) renames Free;
>
> end My_Class;
> ----------------------------------------------------------------
>
> I get:
> my_class.adb:7:59: subprogram used in renaming_as_body cannot be intrinsic
The error message means exactly what it says -- you cannot rename an
intrinsic subprogram.  If you wish to directly use an instantiation of
Ada.Unchecked_Deallocation, then you must instantiate it in the
package specification -- e.g.

procedure Destroy is
    new Ada.Unchecked_Deallocation(Name => My_Class_Access,
  Object => My_Class'Class);

>
> second approach:
> ----------------------------------------------------------------
> with Ada.Unchecked_Deallocation;
> package body My_Class is
> procedure Free is
>     new Ada.Unchecked_Deallocation(Name => My_Class_Access,
>   Object => My_Class'Class);
>
> procedure Destroy (Object: in out My_Class_Access) is
> begin
>     Free(My_Class_Access);
> end Destroy;
>
> end My_Class;
> ------------------------------------------------------------------
>
> I get:
> my_class.adb:9:09: Invalid use of subtype mark in expression or call
Again, the message means exactly what it says -- you cannot use a type
as the argument in a subprogram call.  You should have written
     Free (Object);
>
> Can someone tell me what Im doing wrong?
> And, should Destroy work with any type which extends My_Class?
The answer is maybe.
If none of the extensions to your class will ever use an access object
as one of its components, it will work.

But if an extension of your class uses access to a type not derived
from Ada.Finalization, then the memory for the object designated by
that access component will be "orphaned" -- i.e. you will have a
"memory leak."
>
> Thanks in advance:
>
> Alfonso Acosta
>
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>
>





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

* Re: deallocating class wide types
  2003-05-10 12:02 deallocating class wide types alfonso acosta
  2003-05-10 12:11 ` alfonso acosta
  2003-05-10 12:41 ` David C. Hoos
@ 2003-05-12  0:12 ` Robert I. Eachus
  2 siblings, 0 replies; 4+ messages in thread
From: Robert I. Eachus @ 2003-05-12  0:12 UTC (permalink / raw)


alfonso acosta wrote:
> procedure Destroy (Object: in out My_Class_Access) renames Free;
> 
> end My_Class;
> ----------------------------------------------------------------
> 
> I get:
> my_class.adb:7:59: subprogram used in renaming_as_body cannot be intrinsic

The error tells you that since the calling convention for Destroy 
doesn't match the calling convention for Free, you can't use renaming as 
body.  You could either put the renaming in the package spec, or
another solution is to declare Destroy as a "wrapper" subroutine:

procedure Destroy (Object: in out My_Class_Access) is
begin Free(Object); end Destroy;

If you are really worried about the possibility that this will add run 
time overhead, you can put:
    pragma Inline(Destroy);
in the package spec.  It is usually put in the private part, because the 
user of the package doesn't need to know about it.




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

end of thread, other threads:[~2003-05-12  0:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-05-10 12:02 deallocating class wide types alfonso acosta
2003-05-10 12:11 ` alfonso acosta
2003-05-10 12:41 ` David C. Hoos
2003-05-12  0:12 ` Robert I. Eachus

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