From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.50.47.10 with SMTP id z10mr15262452igm.0.1411449572697; Mon, 22 Sep 2014 22:19:32 -0700 (PDT) X-Received: by 10.182.122.230 with SMTP id lv6mr405135obb.4.1411449572578; Mon, 22 Sep 2014 22:19:32 -0700 (PDT) Path: border1.nntp.dca1.giganews.com!nntp.giganews.com!h15no4461139igd.0!news-out.google.com!bc9ni4973igb.0!nntp.google.com!h15no4461134igd.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Mon, 22 Sep 2014 22:19:32 -0700 (PDT) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=66.176.73.77; posting-account=yiWntAoAAAC1KqC_shmxJYv07B9l6LNU NNTP-Posting-Host: 66.176.73.77 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <4b9c517f-9c78-44f5-bda1-8e7647f06851@googlegroups.com> Subject: Using Class wide types as factories, is this legit? From: David Botton Injection-Date: Tue, 23 Sep 2014 05:19:32 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: number.nntp.dca.giganews.com comp.lang.ada:189097 Date: 2014-09-22T22:19:32-07:00 List-Id: I have a pattern I have used in the past to make Classwides act as factorie= s for objects. This worked in versions of GNAT about a year and a half ago. Is this a comp= iler bug or bad Ada? The key line is: Row =A0: Active_Record'Class :=3D Template; This is meant to act as a factory for new objects matching the Template cla= ss by duplicating Template on assignment to Row. Code: type Active_Record (Table_Name : access constant String; Connection : access Gnoga.Server.Database.Connection'Class) is new Ada.Finalization.Controlled with record Is_New : Boolean :=3D True; Fields : Gnoga.Types.Data_Array_Type; Values : Gnoga.Types.Data_Map_Type; end record; =A0 =A0function Find_All =A0 =A0 =A0(Template : Active_Record'Class; =A0 =A0 =A0 Like =A0 =A0 : String :=3D ""; =A0 =A0 =A0 Order_By : String :=3D "") =A0 =A0 =A0 return Active_Record_Array.Vector =A0 =A0is =A0 =A0 =A0 use Ada.Strings.Unbounded; =A0 =A0 =A0 use Ada.Strings; =A0 =A0 =A0 SQL =A0: Unbounded_String :=3D =A0 =A0 =A0 =A0 To_Unbounded_String ("select * from ") & Template.Table_Nam= e.all; =A0 =A0begin =A0 =A0 =A0 if Like /=3D "" then =A0 =A0 =A0 =A0 =A0SQL :=3D SQL & " where " & Like; =A0 =A0 =A0 end if; =A0 =A0 =A0 if Order_By /=3D "" then =A0 =A0 =A0 =A0 =A0SQL :=3D SQL & " order by " & Order_By; =A0 =A0 =A0 end if; =A0 =A0 =A0 declare =A0 =A0 =A0 =A0 =A0RS =A0 : Gnoga.Server.Database.Recordset'Class :=3D =A0 =A0 =A0 =A0 =A0 =A0Template.Connection.Query (To_String (SQL)); =A0 =A0 =A0 =A0 =A0Rows : Active_Record_Array.Vector; =A0 =A0 =A0 =A0 =A0Row =A0: Active_Record'Class :=3D Template; =A0 =A0 =A0 begin =A0 =A0 =A0 =A0 =A0Row.Is_New :=3D False; =A0 =A0 =A0 =A0 =A0while RS.Next loop =A0 =A0 =A0 =A0 =A0 =A0 Row.Values :=3D RS.Field_Values; =A0 =A0 =A0 =A0 =A0 =A0 Rows.Append (Row); =A0 =A0 =A0 =A0 =A0end loop; =A0 =A0 =A0 =A0 =A0RS.Close; =A0 =A0 =A0 =A0 =A0return Rows; =A0 =A0 =A0 end; =A0 =A0end Find_All; On run the following error appears: db_active(14597,0x7fff775fb310) malloc: *** error for object 0xc00007fe659d= 040f: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug Abort trap: 6 Using this work around removes the error, but limits the use of derived typ= es of Active_Record being created by the assignment: so replacing - Row =A0: Active_Record'Class :=3D Template; with: =A0=A0 =A0 =A0 =A0 Row =A0: Active_Record (Template.Table_Name, Template.Co= nnection); All works.