android - Dynamic controls gives Segmentation fault (11) -


i’m working on multiplatform project have create lots of controls dynamically result of major calculations. when changing conditions, need remove dynamically created controls , make recalculations , create controls again.

i handle first dynamically create tscrollbox (iofplayout) on top of ttabitem. move predefined header ttoolbar ttabitem tscrollbox changing parent. create arrays of tlabel, tedit , tbutton controls on tscrollbox. (i need interact dynamically created controls in code) part works fine on platforms. when remove controls use code below.

on windows x86, x64 , os x seems work fine. on android works fine first time tscrollbox created , filled dynamically created controls. after tscrollbox has been removed , recreated “access violation” or “segmentation fault (11)” error @ random point when dynamic arrays of tlabel controls created on top of tscrollbox.

i feeling has arc. have read can arc , tested every suggestion can find nothing seem work. see what’s wrong?

procedure ttabbedform.removeofp(); begin   // move toolbar2 tabitem3 won’t destroyed   toolbar2.parent := tabitem3;     if assigned(iofplayout)   begin     // iofplayout holds lot of dynamically created controls     iofplayout.release;     iofplayout.disposeof;     iofplayout := nil;     application.processmessages;   end; end; 

here complete minimal working example.

unit unit1;  interface  uses   system.sysutils, system.types, system.uitypes, system.classes, system.variants,   fmx.types, fmx.controls, fmx.forms, fmx.graphics, fmx.dialogs, fmx.stdctrls, fmx.controls.presentation, fmx.layouts;  type   tmainform = class(tform)     toolbar1: ttoolbar;     create: tbutton;     destroy: tbutton;     procedure createclick(sender: tobject);     procedure destroyclick(sender: tobject);   private     sb: tscrollbox;     lbl: array of tlabel;   end;  var   mainform: tmainform;  implementation  {$r *.fmx}  procedure tmainform.createclick(sender: tobject); var   i: integer; begin   // create tscollbox on mainform   sb := tscrollbox.create(self);   sb.align := talignlayout.client;   sb.showscrollbars := true;   sb.parent := mainform;   // set number of labels put on sb   setlength(lbl, 1000);   // create these labels , set properties   := low(lbl) high(lbl)   begin     // on first run on android devices causes no problems.     // after destroyclick has been run after first createclick      // "access violation / segmentation fault (11) here.     // happens after 800+ labels has been created.     lbl[i] := tlabel.create(sb);     lbl[i].text := 'label ' + inttostr(i);     lbl[i].position.x := 10;     lbl[i].position.y := * 20;     lbl[i].parent := sb;   end; end;  procedure tmainform.destroyclick(sender: tobject); begin   if assigned(sb)   begin     sb.release;     sb.disposeof;     sb := nil;   end; end; end. 

the simple answer question call disposeof each dynamically created control has tscrollbox parent before calling disposeof tscrollbox itself.

procedure tmainform.destroyclick(sender: tobject); var   i: integer; begin   if assigned(sb)   begin     // first call disposeof each child control     := low(lbl) high(lbl)     begin       lbl[i].disposeof;     end;     // call disposeof parent control     sb.release;     sb.disposeof;     sb := nil;   end; end; 

Comments