Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4026734
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:00:31+00:00 2026-05-20T11:00:31+00:00

Given an Ada protected type: protected type A is procedure Foo; … private M

  • 0

Given an Ada protected type:

protected type A is
    procedure Foo;
    ...
private
    M : Map;
    ...
end A;

How would you implement or emulate a Finalize procedure that is called when
the protected object is finalized?

Basically I need to do some house keeping using the private members of the protected type (iterating over some map and so on).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:00:32+00:00Added an answer on May 20, 2026 at 11:00 am

    Wrap the private members that have a part in the finalization in one or more records derived from Ada.Finalization.Controlled or Limited_Controlled. When the protected object is finalized, those private members will be correspondingly finalized as well.

    Here’s a quick, working(!) example:

    with Text_IO; use Text_IO;
    with Ada.Finalization;
    with Ada.Containers.Ordered_Maps;
    with Ada.Unchecked_Deallocation;
    
    procedure Protected_Final is
    
       Instance_Counter : Natural := 1;
    
       package Int_Map is new Ada.Containers.Ordered_Maps (Integer, Integer);
       subtype Map is Int_Map.Map;
    
       type Map_Wrapper is new Ada.Finalization.Controlled with record
          ID : Natural;
          M  : Map;
       end record;
    
       overriding procedure Initialize(Item : in out Map_Wrapper);
    
       overriding procedure Finalize(Item : in out Map_Wrapper);
    
       procedure Initialize(Item : in out Map_Wrapper) is
       begin
          Item.ID := Instance_Counter;
          Instance_Counter := Instance_Counter + 1;
          Put_Line("Initialize the Map part as instance" & Natural'Image(Item.ID));
       end Initialize;
    
       procedure Finalize(Item : in out Map_Wrapper) is
       begin
          Put_Line("Clean up the Map stuff for instance" & Natural'Image(Item.ID));
       end Finalize;
    
       protected type A is
          procedure Foo;
       private
          M : Map_Wrapper;
       end A;
    
       protected body A is
    
          procedure Foo is 
          begin
             null;
          end Foo;
       end A;
    
       Test_Item : A;
    
       type A_Handle is access A;
    
       procedure Free is new Ada.Unchecked_Deallocation(A, A_Handle);
    
       Test_Item_Handle : A_Handle;
    
    begin
       Test_Item_Handle := new A;
    
       Free(Test_Item_Handle);
    end Protected_Final;
    

    Running this I get:

    C:\sandbox\protected_final
    Initialize the Map part as instance 1
    Initialize the Map part as instance 2
    Clean up the Map stuff for instance 2
    Clean up the Map stuff for instance 1
    [2011-03-04 08:37:29] process terminated successfully (elapsed time: 00.21s)
    

    The “outer” Initialize/Cleanup messages come are a result of the statically declared Test_Item instance, while the inner pair are from the dynamically allocated and deallocated Test_Item_Handle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a valid, arbitrary xmlNodePtr, I would like the string representation of that node,
Given an absolute or relative path (in a Unix-like system), I would like to
Given below is some code in ada with TYPE_VECT_B; use TYPE_VECT_B; Package TEST01 is
Being new to Ada, I'm exploring its syntax and rules and I would like
Given a table that has a column of string timestamps (yyyyMMddHHmmssSSS format), I want
given a hierarchical view on my asp.net mvc page is it possible to type
Given that acronym is removed in HTML 5, how does one get standards support
Given the following hash: {7=>Ada (gnat-4.3.2), 13=>Assembler (nasm-2.07), 45=>Assembler (gcc-4.3.4), 104=>AWK (gawk) (gawk-3.1.6), 105=>AWK
Given a list, I would like to apply some set of operations to a
Given a tree, how to find the centre node in the tree so that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.