[SOLVED] Worried about memory usage. PaintBox, TRects lazarus / 61-Beginners ---------------------------------------------------------------------- TomTom · 2019-01-25 14:35 As You propably know I'm working on game with tiles Now Im refining my code to make it easier to work with, so... I wrote new generator of world (In the futer it will be Game Map Editor for players). I've discovered that my program uses more and more memory whenever I click generate world button. How can I free memory that my program uses? For example. Whenever I press click Generate world button it frees memory used by Paintbox (it's my canvas) etc. For now my program looks like this It's kinda messy because I'm trying random things and I don't know what I'm actually doing Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,  StdCtrls,typinfo; type     { TTile }   TTileTypes = (ttLand,ttSea,ttDesert);  TTile = class    private      FTileType: TTileTypes; //ttLand,ttSea,ttDesert      FRect: TRect;//    FBuilding: TBuilding;     public      property TileType: TTileTypes read FTileType write FTileType;      property Rect: TRect read FRect write FRect;      function GetTileEnum(ATile: TTile): string;  end;   { TForm1 }   TForm1 = class(TForm)    Bevel1: TBevel;    Button1: TButton;    Button2: TButton;    Edit1: TEdit;    eww: TEdit;    ewh: TEdit;    etw: TEdit;    eth: TEdit;    Scene: TPaintBox;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure SceneMouseDown(Sender: TObject; Button: TMouseButton;      Shift: TShiftState; X, Y: Integer);    procedure SceneMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer      );   private    procedure ScenePaint(Sender: TObject);   public   end; var  Form1: TForm1;  World: Array of Array of TTile;  WorldWidth,WorldHeight,TileWidth,TileHeight: Integer; implementation {$R *.lfm} { TTile } function TTile.GetTileEnum(ATile: TTile): string;begin  case ATile.TileType of  ttSea: Result:='Morze';  ttLand: Result:='Ląd';  ttDesert: Result:='Pustynia';  //ttDesert: Result:='Pustynia';  end;end; { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  rndTile, Row,Col: Integer; // for FOR loop to create tilesbegin Randomize;  //Set array dimensions reset to [0,0]  SetLength(World,0,0); //Get World size   WorldWidth:=StrToInt(eww.text);   WorldHeight:=StrToInt(ewh.text);   TileWidth:=StrToInt(etw.text);   TileHeight:=StrToInt(eth.text);   Scene.Width:=WorldWidth*TileWidth;   Scene.Height:=WorldHeight*TileHeight;   SetLength(World,WorldWidth,WorldHeight); //Fill world with TTiles for Row:=0 to WorldWidth-1 do begin   for Col:=0 to WorldHeight-1 do begin     rndTile:=Random(3);     World[Row,Col]:=TTile.Create;     Wo rld[Row,Col].Rect:=TRect.Create(Row*TileWidth,Col*TileHeight,Row*TileW idth+TileWidth, Col*TileHeight+TileHeight);     // SET Tile to Sea     if rndTile=0 then begin       World[Row,Col].TileType:=ttSea;     end;      // SET Tile to desert     if rndTile=2 then begin       World[Row,Col].TileType:=ttDesert;     end;     end; end; Scene.OnPaint:=@ScenePaint; Scene.Refresh;  end; procedure TForm1.Button2Click(Sender: TObject);var Row,Col: Integer; // for FOR loop to create tilesbegin  for Row:=0 to WorldWidth-1 do begin     for Col:=0 to WorldHeight-1 do begin       //Determin Color       Case World[Row,Col].TileType of       ttSea: Scene.Canvas.Brush.Color:=RGBToColor(0,160,240);       ttLand: Scene.Canvas.Brush.Color:=RGBToColor(160,240,0);       ttDesert: Scene.Canvas.Brush.Color:=RGBToColor(250,190,10);       end;       Scene.Canvas.Rectangle((World[Row,Col].Rect));     end; end;     WorldWidth:=StrToInt(eww.text);   WorldHeight:=StrToInt(ewh.text);   TileWidth:=StrToInt(etw.text);   TileHeight:=StrToInt(eth.text);   Scene.Width:=WorldWidth*TileWidth;   Scene.Height:=WorldHeight*TileHeight;  end; procedure TForm1.FormCreate(Sender: TObject);begin     WorldWidth:=StrToInt(eww.text);   WorldHeight:=StrToInt(ewh.text);   TileWidth:=StrToInt(etw.text);   TileHeight:=StrToInt(eth.text);   Scene.Width:=WorldWidth*TileWidth;   Scene.Height:=WorldHeight*TileHeight;   SetLength(World,WorldWidth,WorldHeight);end; procedure TForm1.SceneMouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);var mx,my:integer; tmpRect:TRect;begin mx:=(x div TileWidth);//*TileWidth;my:=(y div TileHeight);//*TileHeight;if World[mx,my]<>nil then begin  tmpRect:=World[mx,my].Rect; World[mx,my] .Free; World[mx,my]:=TTile.Create; World[mx,my].TileType:=ttLand; Worl d[mx,my].Rect:=tmpRect;  World[mx,my].Rect:=TRect.Create(World[mx,my]. Rect); Scene.Canvas.Rectangle( tmpRect); Scene.OnPaint:=@ScenePaint; Edit1.Text:=mx.ToString+' '+my.tostring; end;Scene.Refresh;end; procedure TForm1.SceneMouseMove(Sender: TObject; Shift: TShiftState; X,  Y: Integer);var  mx, my: Integer;  value: String;beginmx:=(x div TileWidth);//*TileWidth;my:=(y div TileHeight);//*TileHeight; if World[mx,my]<>nil then begin //Writeln('GetEnumName           : ',GetEnumName(TI,Ord(O.MyEnumField))); Edit1.Text:=mx.ToString+' '+my.tostring+' '+ World[mx,my].GetTileEnum(World[mx,my]); end;end;   procedure TForm1.ScenePaint(Sender: TObject);var Row,Col: Integer; // for FOR loop to create tiles beginscene.Canvas.Clear; for Row:=0 to WorldWidth-1 do begin     for Col:=0 to WorldHeight-1 do begin       //Determin Color       Case World[Row,Col].TileType of       ttSea: Scene.Canvas.Brush.Color:=RGBToColor(0,160,240);       ttLand: Scene.Canvas.Brush.Color:=RGBToColor(160,240,0);       ttDesert: Scene.Canvas.Brush.Color:=RGBToColor(250,190,10);       end;       Scene.Canvas.Rectangle((World[Row,Col].Rect));     end; end; end; end. ---------------------------------------------------------------------- Handoko · 2019-01-25 15:10 I checked you code quickly. I saw you call TTile.Create; in a stacked for-do loop in Button1Click event but I found no code for freeing it. It means if an user clicks the Button1 several times, that the memory grow and grow without freeing them. That's not good. ---------------------------------------------------------------------- TomTom · 2019-01-25 17:45 Eh, you're right! This is one of my bad habits, When I code, very often when I want to achieve something quickly, I forget about basic matters. And if it works, then I go on and I do not correct it anymore: P. That's why I did not release TTile. Thanks, I'll check how the case looks then. I've done different things from resetting each array element, then resetting array dimensions ... I gained only 2-3MB of memory Fixed it ... Thx Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  // at the beginning of generating new terrain... Quite impresive map size 2000x5000 tiles  almost killed my laptop :P for Row:=0 to WorldWidth-1 do begin   for Col:=0 to WorldHeight-1 do begin   World[Row,Col].Free;   end;   end;     end;SetLength(World,0,0); ---------------------------------------------------------------------- Akira1364 · 2019-01-25 18:20 As Handoko suggested, you don't seem to be freeing the TTile instances in your multi-dimensional array at the beginning of Button1Click before creating new ones, which causes a memory leak as the old ones are still around (but no longer directly accessible.) That said, your TTile would really be much better off as an "advanced record" type than a class for both performance and memory-usage reasons. I've attached a sample project based on the code you posted (with various modifications and additions, although I tried to maintain what you seemed to be going for in general) to this comment, which shows how to use advanced records that way, amongst other things. Hopefully it's at least somewhat helpful! ---------------------------------------------------------------------- TomTom · 2019-01-25 19:00 Thank You Akira1364 Looks nice but dunno why Your program takes about 20MB of RAM with 100x100tiles and 24x24 while mine takes about 5MB of RAM with same settings. Can it be because of use of this Buffer TBitmap? I've also noticed that in your program leaks are quite bigger than in my version. Like in this situation: Yours TileEditor||My World EditorSet 100 by 100 tiles map and each tile 24x24px - ~20MB||Set 100 by 100 tiles map and each tile 24x24px - ~5MBSet 400 by 400 tiles map and each tile 24x24px - ~500MB||Set 400 by 400 tiles map and each tile 24x24px - ~15MBSet 100 by 100 tiles map and each tile 24x24px - ~300MB||Set 100 by 100 tiles map and each tile 24x24px - ~5-6MB ---------------------------------------------------------------------- Akira1364 · 2019-01-25 20:27 It's specifically the buffer bitmap that increases it, yeah. I also kept in the "overdraw" that you had, meaning setting the size of it to "WorldWidth * TileWidth" and "WorldHeight * TileHeight" as opposed to just "WorldWidth" and "WorldHeight", which is not technically actually necessary depending on how you want the image to scale up and down. There's definitely no memory leaks though, as like I said it uses an array of records as opposed to an array of class instances, and I do free the buffer at the end. The process of modifying and drawing things like tilesets is also faster overall if buffered like that, generally speaking. Lastly, here's another somewhat improved version of what I posted before, that uses a BGRAVirtualScreen (from the BGRAControls library) instead of a TPaintBox. For more complex stuff I'd definitely recommend looking into BGRABitmap/BGRAControls over any of the LCL defaults as they tend to perform significantly better. ---------------------------------------------------------------------- TomTom · 2019-01-25 20:33 Maybe I'm using wrong term...maybe it's not memory leak... but try it yourself . Create 100x100 map look at mem usage... then create bigger map look at mem usage. and then set map back to 100x100. I would appreciate if You do this test . ---------------------------------------------------------------------- TomTom · 2019-01-25 21:06 I'm not saying that Your version is worse than my version. All I'm saying is that when You create a map for example with defaults settings (50 by 50 tiles and 5 by 5px for tile)  and then without closing program generate another bigger map... ofcourse it will take more memory than previous... but when You get back to default settings (50x50tiles 5x5px) program  will use much more memory than it was using in the beginning with default settings. So Im guessing something is not freed. I'm ofcourse referring  the  first version You posted. Maybe this in overall is better approach to subject but it has very similar problem that I had before. Anyway I WILL try to install BGRAControls and see this other version [EDIT] I also noticed that when You create map that is smaller than form window and move mouse over the map and then move it away program crashes. Quote from: Akira1364 on January 25, 2019, 09:27:33 pmIt's specifically the buffer bitmap that increases it, yeah. There's definitely no memory leaks, as like I said it uses an array of records as opposed to an array of class instances, and I do free the buffer at the end. The process of modifying and drawing things like tilesets is also faster overall if buffered like that, generally speaking. Lastly, here's another slightly improved version of what I posted before, that uses a BGRAVirtualScreen (from the BGRAControls library) instead of a TPaintBox. For more complex stuff I'd definitely recommend looking into BGRABitmap/BGRAControls over any of the LCL defaults as they tend to perform significantly better. ---------------------------------------------------------------------- Akira1364 · 2019-01-25 21:19 Quote from: TomTom on January 25, 2019, 10:06:42 pm[EDIT] I also noticed that when You create map that is smaller than form window and move mouse over the map and then move it away program crashes. The one in the original copy of the 7Z file I uploaded did have that problem, yeah. I noticed the issue like five minutes later and uploaded a new one though, so the copy that's there now should be fine. I didn't think anyone had downloaded it that quickly. As far as the memory usage, it does go back down, just not immediately as I didn't specifically reset the array to zero-length every time (as that's not particularly efficient.) I'm also not sure that your original code was creating images that were actually as large as the calculations intended, as for example 400 * 400 tiles * 24 pixels each = 9600 pixels both horizontally and vertically. Uncompressed, that will indeed be >300MB. ---------------------------------------------------------------------- TomTom · 2019-01-25 21:25 Well I'm fast ;P. I also had this problem in my version     Now I'm quite tired, but tomorrow I'll try to mix Your way with buffer with my TTile class .. Will see what comes out of that ---------------------------------------------------------------------- Akira1364 · 2019-01-26 04:27 Quote from: TomTom on January 25, 2019, 10:25:12 pmWell I'm fast ;P. I also had this problem in my version     Well, yeah, the whole thing was based directly on the code you posted at the top of this thread after all. ---------------------------------------------------------------------- TomTom · 2019-01-27 16:26 [Edit] So it seems it's because of this TBuilding.Create. But I just don't know how can I free memory before generating new map. With TTile it was simple. But... well I have no clue how to solve this ------------------------ I've added feature to my World Editor. Now one can place building. But again I have problem with memory. Now it looks like this Classes Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type   { TGameBuilding }  //Class for buildings {factories,power plants,tree etc;}  TBuildingTypes = (btNull,btWindPlant, btCoalPlant, btPowerPlant, btShoeFactory, btChocolateFactory, btCarFactory, btCannedFoodFactory, btSandFactory,btPark);  TBuilding = class    private     FBuildingType: TBuildingTypes; //Building type to determin tile texture     FBLevel: Integer;               //Level of building in fact income multiplier and pollution multiplier;     FBRect:  TRect;                 //Canvas for texture     FBPollution: Double;         //Polution generating indicator. For trees/parks it will be negative for clearing pollution ?     FBIncome: Extended;             //Basic income    public     property BuildingType: TBuildingTypes read FBuildingType write FBuildingType;     property BuildingLevel: Integer read FBLevel write FBLevel;     property BRect: TRect read FBRect write FBRect;     property BuildingPollution: Double read FBPollution write FBPollution;     property Income: Extended read FBIncome write FBIncome;   end;    { TTile }   TTileTypes = (ttLand,ttSea,ttDesert);   TTile = class    private      FTileType: TTileTypes; //ttLand,ttSea,ttDesert      FRect: TRect;    //  function SetTileTexture(ATile: TTile): TColor;    FBuilding: TBuilding;     public      property TileType: TTileTypes read FTileType write FTileType;      property Rect: TRect read FRect write FRect;      property Building: TBuilding read FBuilding write FBuilding;      function GetTileEnum(ATile: TTile): string;      function GetTileBuilding(ATile: TTile): string;      function SetTileTexture(ATile: TTile): TColor;  end; Generating world: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  rndTile, Row,Col: Integer; // for FOR loop to create tiles  tmpTile:TTile;  tmpBuilding: TBuilding;  begin Randomize;  //Set array dimensions reset to [0,0] Quite important... saving memoryif Length(World)>0 then begin for Row:=0 to WorldWidth-1 do begin   for Col:=0 to WorldHeight-1 do begin    World[Row,Col].Free;   end;   end;     end;SetLength(World,0,0); //Set World size   WorldWidth:=StrToInt(eww.text);   WorldHeight:=StrToInt(ewh.text);   TileWidth:=StrToInt(etw.text);   TileHeight:=StrToInt(eth.text);   Scene.Width:=WorldWidth*TileWidth;   Scene.Height:=WorldHeight*TileHeight;   SetLength(World,WorldWidth,WorldHeight);    //Fill world with TTiles for Row:=0 to WorldWidth-1 do begin   for Col:=0 to WorldHeight-1 do begin     rndTile:=Random(3);      World[Row,Col]:=TTile.Create;     World[Row,Col].Rect:=TRect.Create(R ow*TileWidth,Col*TileHeight,Row*TileWidth+TileWidth, Col*TileHeight+TileHeight);      World[Row,Col].Building:=TBuilding.Create;     World[Row,Col].Building.BuildingType:=btNull; //=empty/no building.   // SET Tile to Sea     if rndTile=0 then begin       World[Row,Col].TileType:=ttSea;     end;      // SET Tile to desert   if rndTile=2 then begin       World[Row,Col].TileType:=ttDesert;     end;     end; end; Scene.OnPaint:=@ScenePaint; Scene.Refresh;  end;  And especially this part of above code gives me concerns: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if Length(World)>0 then begin for Row:=0 to WorldWidth-1 do begin   for Col:=0 to WorldHeight-1 do begin    World[Row,Col].Free;   end;   end;     end;SetLength(World,0,0);     Shouldn't this World[Row,Col].Free free everything (including World[Row,Col].Building) ? I'm experiencing same problem as before when generating map over and over. Memory usage grows but it's not freed. Oh and here is part for rendering the map... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  procedure TForm1.ScenePaint(Sender: TObject);var Row,Col: Integer; // for FOR loop to create tiles TileColor: TColor;beginscene.Canvas.Clear;if Length(world)>0 then begin for Row:=0 to WorldWidth-1 do begin     for Col:=0 to WorldHeight-1 do begin       //Determin Color       TileColor:=World[Row,Col].SetTileTexture(World[Row,Col]);       Scene.Canvas.Brush.Color:=TileColor;       Scene.Canvas.Rectangle((World[Row,Col].Rect));       if World[Row,Col].Building.BuildingType=btWindPlant then       world[Row, Col].Building.BRect:=TRect.Create(world[Row,Col].Rect.Left+2,world[Row ,Col].Rect.top+2,world[Row,Col].Rect.Right- 2,world[Row,Col].Rect.Bottom-2);       Scene.Canvas.Brush.color:=RGBToColor(255,0,0);       Scene.Canvas.Rectangle(World[Row,Col].Building.BRect);    end;     end; end; end; ---------------------------------------------------------------------- Handoko · 2019-01-27 17:26 I saw you free the tiles by using this code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    World[Row,Col].Free;That's good. But, unfortunately the tiles won't automatically free their building. You can make it automatically free its building by adding: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TTile = class  public     destructor Destroy; override;  end; // ................. destructor TTile.Destroy;begin  FBuilding.Free;  inherited Destroy;end; ---------------------------------------------------------------------- TomTom · 2019-01-27 17:37 Yes ofcourse . I thought that it will be something with destructors inheritance etc. But didn't know how to do that.  I need to learn about that sometime soon. Thank You Handoko. Works like a charm. But since I'm not quite sure HOW IT WORKS exactly I neet to find some good guide. Yes I know about the wiki but ... I'll try to do my best to understand that . ---------------------------------------------------------------------- Handoko · 2019-01-27 17:39 I've told you, maintaining a class is harder than you thought. ---------------------------------------------------------------------- lucamar · 2019-01-27 19:24 Quote from: TomTom on January 27, 2019, 06:37:13 pm[...] since I'm not quite sure HOW IT WORKS exactly I neet to find some good guide. It is actually quite easy: whatever your own class creates it must destroy before it's, in turn,  itself destroyed. If you don't destroy/free it then all the memory it reserved is kept reserved even if there is not actual usable reference to it. ---------------------------------------------------------------------- TomTom · 2019-01-27 20:00 @Handoko Yes You did . @lucamar That is clear I knew that, but I had/have problem with freeing this TBuilding class in my TTile class. When I did like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if Length(World)>0 then begin for Row:=0 to WorldWidth-1 do begin   for Col:=0 to WorldHeight-1 do begin // ---------------------------------  World[Row,Col].Building.free; // <-- This was giving me an error, yet it seemed logical to me.// ---------------------------------   World[Row,Col].Free;   end;   end;     end;  Now I have another question. I have this: World: array of array of TTile; for World array. I want to write a procedure GenWorld. How can I add this as parameter? ---------------------------------------------------------------------- lucamar · 2019-01-27 20:19 Quote from: TomTom on January 27, 2019, 09:00:18 pmNow I have another question. I have this: World: array of array of TTile; for World array. I want to write a procedure GenWorld. How can I add this as parameter? Something like this (untested!): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TTileArray = array of TTile;  TWorld = array of TTileArray; procedure GenWorld(var World: TWorld);beginend; ---------------------------------------------------------------------- Handoko · 2019-01-28 02:41 Quote from: TomTom on January 27, 2019, 09:00:18 pm I knew that, but I had/have problem with freeing this TBuilding class in my TTile class. When I did like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if Length(World)>0 then begin for Row:=0 to WorldWidth-1 do begin   for Col:=0 to WorldHeight-1 do begin // ---------------------------------  World[Row,Col].Building.free; // <-- This was giving me an error, yet it seemed logical to me.// ---------------------------------   World[Row,Col].Free;   end;   end;     end; What was the error message? Or can you provide the screenshot? I made a simple test, no error showing up: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);const  Dimension = 1000;var  Row, Col: Integer;begin  SetLength(World, Dimension, Dimension);  for Row := 0 to Dimension-1 do begin    for Col := 0 to Dimension-1 do begin    World[Row,Col] := TTile.Create;      World[Row,Col].Building := TBuilding.Create;    end;  end;   for Row := 0 to Dimension-1 do begin    for Col := 0 to Dimension-1 do begin      World[Row,Col].Building.free;      World[Row,Col].Free;    end;  end;  SetLength(World,0,0);end; FYI, if you have to manually free the items that a class generated (in your case, it is FBuilding), then it is a bad design. When you free a class, the class must automatically frees all the items and memory it used. Quote from: TomTom on January 27, 2019, 09:00:18 pmWorld: array of array of TTile; for World array. I want to write a procedure GenWorld. How can I add this as parameter? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TWorld = array of array of TTile; // ........ procedure GenWorld(World: TWorld);beginend; ---------------------------------------------------------------------- TomTom · 2019-01-28 07:50 Thanks Handoko So with this destructor which You showed me... is it ok now? I mean in terms of desing. And about that error, its strange. In fact I used World[Row,Col].Building.Free with success within procedure  (PaintBox.OnMouseDown) that adds New Building to tile. First I free previous TBuilding on tile and then I was creating new type of building on that tile. Problem with World[Row,Col].Building.Free appeard when I tried to generate map for the first time after launching my World Editor. So I thought this is because when generate map for the first time there is NO TBuilding in my World[Row,Col].Building but I had this IF-THEN which checks if my World is not empty and only then runs freeing part. Anyway,  later today I'll post whole project as attachment. Now (at work) I don't have that version. Maybe You could point me with something else that I need to correct/add with this. I don't want final solution, just point me what do I need to look for... that way maybe I could learn more and understand better. . Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type   { TBuilding }   //Building types  TBuildingTypes = (btNull,btWindPlant, btCoalPlant, btPowerPlant, btShoeFactory, btChocolateFactory, btCarFactory, btCannedFoodFactory, btSandFactory,btPark);   { TBuilding }   TBuilding = class    private     FBuildingType: TBuildingTypes;  //Building type to determine tile color (in future texture/image)     FBLevel: Integer;               //Level of building in fact income multiplier and pollution multiplier;     FBRect:  TRect;                 //Canvas for texture     FBPollution: Double;          //Polution generating indicator. For trees/parks it will be negative for clearing pollution ?     FBIncome: Extended;             //Basic income    public     property BuildingType: TBuildingTypes read FBuildingType write FBuildingType;     property BuildingLevel: Integer read FBLevel write FBLevel;     property BRect: TRect read FBRect write FBRect;     property BuildingPollution: Double read FBPollution write FBPollution;     property Income: Extended read FBIncome write FBIncome;    end;    { TTile }   //Tile types  TTileTypes = (ttLand,ttSea,ttDesert);  TTile = class    private      FTileType: TTileTypes; //ttLand,ttSea,ttDesert      FRect: TRect;    FBuilding: TBuilding;     public       //Learn about this      destructor Destroy; override;       property TileType: TTileTypes read FTileType write FTileType;      property Rect: TRect read FRect write FRect;      property Building: TBuilding read FBuilding write FBuilding;      function GetTileEnum(ATile: TTile): string;      function GetTileBuilding(ATile: TTile): string;      function SetTileTexture(ATile: TTile): TColor;   end; ---------------------------------------------------------------------- Handoko · 2019-01-28 08:10 The code for freeing items (used by the class) should be put inside the destructor. Did you get any error if you modify your code to use the destructor? I believe, it should works without error. But, I'm more interested why calling Building.Free manually can cause error on your test. I cannot reproduce the issue. Can you provide a compilable example? No need to show all the code, just the code that can compile and showing the error. So I can check, because I think the bug is not present on the code you've already shown. ---edit--- Something just appeared on my mind. You may get error when freeing the FBuilding if it hasn't been created. Because you said the new building is created inside OnMouseDown. If my guess is correct, you should set FBuilding := nil; in TTile.Create event;. And then your code should change to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if Length(World)>0 then begin  for Row:=0 to WorldWidth-1 do begin    for Col:=0 to WorldHeight-1 do begin      if assigned(World[Row,Col].Building) then World[Row,Col].Building.free;    World[Row,Col].Free;    end;  end;end; ---------------------------------------------------------------------- TomTom · 2019-01-28 08:22 I modified mu current project to reproduce this error (i've commented out destructor and added World[Row,Col].Building.Free; Now i think this is because at start I don't have empty World array (i'm setting on Form1Create) so it's empty but have dimensions set but ther is no TBuilding classes in it. So I would need to check if there are TBuildings  and if there are only the free them... But don't know how to exactly check if there are TBuildings --> Code: [Select]if World[Row,Col].Building<> nil then Begin ... end; Above gives me SIGSEGV at line with that code. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,  StdCtrls, ComCtrls,typinfo; type   { TBuilding }   //Building types  TBuildingTypes = (btNull,btWindPlant, btCoalPlant, btPowerPlant, btShoeFactory, btChocolateFactory, btCarFactory, btCannedFoodFactory, btSandFactory,btPark);   { TBuilding }   TBuilding = class    private     FBuildingType: TBuildingTypes;  //Building type to determin tile texture     FBLevel: Integer;               //Level of building in fact income multiplier and pollution multiplier;     FBRect:  TRect;               //Canvas for texture     FBPollution: Double;            //Polution generating indicator. For trees/parks it will be negative for clearing pollution ?     FBIncome: Extended;             //Basic income    public     {destructor Destroy; override;}     property BuildingType: TBuildingTypes read FBuildingType write FBuildingType;   property BuildingLevel: Integer read FBLevel write FBLevel;     property BRect: TRect read FBRect write FBRect;     property BuildingPollution: Double read FBPollution write FBPollution;     property Income: Extended read FBIncome write FBIncome;    end;    { TTile }   //Tile types  TTileTypes = (ttLand,ttSea,ttDesert);  TTile = class    private      FTileType: TTileTypes; //ttLand,ttSea,ttDesert    FRect: TRect;      FBuilding: TBuilding;     public       //Learn about this     { destructor Destroy; override;}       property TileType: TTileTypes read FTileType write FTileType;      property Rect: TRect read FRect write FRect;      property Building: TBuilding read FBuilding write FBuilding;      function GetTileEnum(ATile: TTile): string;      function GetTileBuilding(ATile: TTile): string;    function SetTileTexture(ATile: TTile): TColor;   end;  type  TWorld = array of array of TTile;  type   { TForm1 }   TForm1 = class(TForm)    Bevel1: TBevel;    Bevel2: TBevel;    Bevel3: TBevel;    Button1: TButton;    Button2: TButton;    Button3: TButton;    Edit1: TEdit;  eww: TEdit;    ewh: TEdit;    etw: TEdit;    eth: TEdit;    Label1: TLabel;    Label2: TLabel;    rgTileType: TRadioGroup;    rgBuildings: TRadioGroup;    Scene: TPaintBox;    ScrollBox1: TScrollBox;    ToggleBox1: TToggleBox;    procedure Button1Click(Sender: TObject);  procedure Button2Click(Sender: TObject);    procedure Button3Click(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure SceneMouseDown(Sender: TObject; Button: TMouseButton;      Shift: TShiftState; X, Y: Integer);    procedure SceneMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer    );    procedure ToggleBox1Change(Sender: TObject);   private    procedure PlaceBuildings(Sender: TObject; Button: TMouseButton;      Shift: TShiftState; X, Y: Integer);    procedure PlaceTerrain(Sender: TObject; Button: TMouseButton;      Shift: TShiftState; X, Y: Integer);    procedure ScenePaint(Sender: TObject);   public   end; var  Form1: TForm1;  World: TWorld;  WorldWidth,WorldHeight,TileWidth,TileHeight: Integer;  BuildingMode: boolean; implementation {$R *.lfm} { TBuilding } {destructor TBuilding.Destroy;begin   inherited Destroy;end;    }   { TTile } {destructor TTile.Destroy;begin FBuilding.free;inherited Destroy;end;    } function TTile.GetTileEnum(ATile: TTile): string;begin  case ATile.TileType of   ttSea               : Result:='Morze';  ttLand              : Result:='Ląd';  ttDesert          : Result:='Pustynia';   end;end; function TTile.GetTileBuilding(ATile: TTile): string;begin      case ATile.Building.BuildingType of  btWindPlant: Result:='Elektrownia wiatrowa';  btShoeFactory: Result:='Fabryka butów';  btNull: Result:='Nie ma niczego';  //ttDesert: Result:='Pustynia';  end;  end; function TTile.SetTileTexture(ATile: TTile): TColor;begin  case ATile.TileType of       ttSea:      Result:=RGBToColor(0,160,240); //Sea color       ttLand:     Result:=RGBToColor(160,240,0); //Land color       ttDesert:   Result:=RGBToColor(211,188,95); //Desert Color  end;end;   { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  rndTile, Row,Col: Integer; // for FOR loop to create tiles  tmpTile:TTile;  tmpBuilding: TBuilding;  begin Randomize;  // ---------------------  if Length(World)>0 then begin  //Set array dimensions reset to [0,0] Quite important... saving memory for Row:=0 to WorldWidth-1 do begin   for Col:=0 to WorldHeight-1 do begin      World[Row,Col].Building.Free;   FreeAndNil(World[Row,Col]);     //.Free;    end;     end; end;SetLength(World,0,0); //Set World size   WorldWidth:=StrToInt(eww.text);   WorldHeight:=StrToInt(ewh.text);   TileWidth:=StrToInt(etw.text);   TileHeight:=StrToInt(eth.text);   Scene.Width:=WorldWidth*TileWidth;   Scene.Height:=WorldHeight*TileHeight;   SetLength(World,WorldWidth,WorldHeight);    //Fill world with TTiles for Row:=0 to WorldWidth-1 do begin   for Col:=0 to WorldHeight-1 do begin     rndTile:=Random(3);      World[Row,Col]:=TTile.Create;     World[Row,Col].Rect:=TRect.Create(R ow*TileWidth,Col*TileHeight,Row*TileWidth+TileWidth, Col*TileHeight+TileHeight);     World[Row,Col].Building:=TBuilding.Create;     World[Row,Col].Buildin g.BRect:=TRect.Create(world[Row,Col].Rect.Left+2,world[Row,Col].Rect.t op+2,world[Row,Col].Rect.Right-2,world[Row,Col].Rect.Bottom-2);     World[Row,Col].Building.BuildingType:=btNull;      // SET Tile to Sea     if rndTile=0 then begin       World[Row,Col].TileType:=ttSea;     end;     // SET Tile to Sea     if rndTile=1 then begin       World[Row,Col].TileType:=ttLand;     end;      // SET Tile to desert     if rndTile=2 then begin       World[Row,Col].TileType:=ttDesert;   end;     end; end; Scene.OnPaint:=@ScenePaint; Scene.Refresh;  end; ---------------------------------------------------------------------- TomTom · 2019-01-28 08:50 Yes I also thought that is the case but as I mentioned in previous post I didn't knew how to check if it is empty.  Now I've changed map generation code so now it's not creating Building (btNull) on Sea tiles since there is no point for that. Quote from: Handoko on January 28, 2019, 09:10:58 am---edit--- Something just appeared on my mind. You may get error when freeing the FBuilding if it hasn't been created. Because you said the new building is created inside OnMouseDown. If my guess is correct, you should set FBuilding := nil; in TTile.Create event;. And then your code should change to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if Length(World)>0 then begin  for Row:=0 to WorldWidth-1 do begin    for Col:=0 to WorldHeight-1 do begin      if assigned(World[Row,Col].Building) then World[Row,Col].Building.free;    World[Row,Col].Free;    end;  end;end; ---------------------------------------------------------------------- Handoko · 2019-01-28 11:06 You can know if a pointer/class has been or has not been defined by using assigned: https://www.freepascal.org/docs-html/rtl/system/assigned.html Or using: if something <> nil then // do something But in your case, to make it works you should first set the FBuilding := nil: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TTile.Create;begin  FBuilding := nil;end; ~ off topic ~ I saw you use FreeAndNil. Using FreeAndNil sounds good because it is defensive. But it may make your code harder to debug. I do not against it because I know in some very race cases you need to use FreeAndNil. I also do not recommend using FreeAndNil. Do you know what is the different between them, if not don't use it until you really know and need it. Using FreeAndNil or not is a controversial topic. I do not want to talk about it, peace ... ---------------------------------------------------------------------- Pascal · 2019-01-28 11:12 SIGSEGV can also happen if you free the same Bulding twice. Use FreeAndNil(). It sets reference to Nil after Free. FYI: Free checks for Nil and does nothing when reference is Nil. So no need to check for Nil yourself! Setting FBuilding to Nil in constructor should not be necessary as class memory is erased with $00 on allocation. ---------------------------------------------------------------------- Handoko · 2019-01-28 11:41 Quote from: Pascal on January 28, 2019, 12:12:06 pmSetting FBuilding to Nil in constructor should not be necessary as class memory is erased with $00 on allocation. I never knew about it. I created a simple test and yes, you're right. Thank you for telling it. ---------------------------------------------------------------------- TomTom · 2019-01-28 11:51 I'm still experiencing some memory loses and I don't know what could be not freed when generating map. For now I'm freeing  TTiles and TBuildings from TWorld. In this example case: 1. I run my program (we.exe ~3.2MB) 2. I generate new map size 24x24 and 24x24px per tile (we.exe ~3.3MB) 3. I generate new map size 500x500 and 24x24px per tile (we.exe ~30.7MB) 4. I generate new map size 24x24 and 24x24px per tile (we.exe 6.7MB) So there's something left to be freed. But don't know what could it be. If I repeat above procedure few times, while increasing map size,  memory usage can get quite big even if finally I set my map to something small. ---------------------------------------------------------------------- Handoko · 2019-01-28 11:57 Can you provide the compilable source code (+ images)? I'm interested to debug the issue. If you do not wish to publicize your project, you can email me or make a demo project showing the issue and attach the zip file here. ---------------------------------------------------------------------- TomTom · 2019-01-28 12:06 @Handoko here You go   Thanks [edit] some text is in Polish... i hope this is not a problem Ląd = Land Pustynia = Desert Morze = Sea items in rgBuildings (radio group) are not important at the moment as those are never used for now. ---------------------------------------------------------------------- balazsszekely · 2019-01-28 12:21 @TomTom Just free the objects on form destroy: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormDestroy(Sender: TObject);var  Row,Col: Integer;begin  if Length(World)>0 then begin    for Row:=0 to WorldWidth-1 do begin    for Col:=0 to WorldHeight-1 do begin        World[Row,Col].Free;      end;    end;  end;end; ---------------------------------------------------------------------- TomTom · 2019-01-28 12:26 @GetMem You got me wrong When I restart program everthing is OK .. But when I generate new maps (bigger than previous) over and over and finally I generate small map (1 by 1 tile) Memory usage is smaller than with biggest generated map but much bigger than at start of program. ---------------------------------------------------------------------- balazsszekely · 2019-01-28 12:41 Your program was leaking, the code in form destroy is a must. You can check it by enabling Heaptrc(Project Options-->Debugging). Other then this, I'm under the impression that you rely on Task Manager to see the memory compsumption of your program, which is wrong. In short don't worry if the memory goes up in TM and does not decrease immediately. ---------------------------------------------------------------------- Pascal · 2019-01-28 12:45 Use "Use Heaptrc unit" in "Compiler Options/Debugging". It will tell you where the leaked memory is allocated: ---------------------------------------------------------------------- TomTom · 2019-01-28 12:52 Thank You GetMem In this project, really big maps are pointless anyway as they would be unplayable . So is this OK ? ---------------------------------------------------------------------- balazsszekely · 2019-01-28 13:02 QuoteSo is this OK ? Yes. It looks perfectly fine. While you're developing the program, leave the heaptrc on. If there is a leak you will notice it immediately. As a test, just create a button, then close the application and see how the heap dump changes: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  Button: TButton;begin  Button := TButton.Create(nil);end ---------------------------------------------------------------------- Handoko · 2019-01-28 13:24 @TomTom My test show no memory leak. Your code runs slow on my computer. So I decided to make it automatically clicks for me: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button4Click(Sender: TObject);var  i: Integer;begin  for i := 1 to 100 do  begin    eww.TextHint := '24';    ewh.TextHint := '24';    Button1.Click;    eww.TextHint := '500';    ewh.TextHint := '500';    Button1.Click;    Caption := i.ToString;    Application.ProcessMessages;  end;end; Yes, I saw there are some rising of the memory usage but it will go down after a while. I think that is how OS memory manager works. I run the code above, the memory usage seems to be stable on my Ubuntu System Monitor. If you want, you can set the for-do loop 10000. @ GetMem Quote from: GetMem on January 28, 2019, 01:21:12 pm@TomTom Just free the objects on form destroy: That is not okay. Because the objects are in an array an it is called again and again (when user click the button) without freeing it. My mistake. ---------------------------------------------------------------------- balazsszekely · 2019-01-28 13:28 @Handoko QuoteThat is not okay. Because the objects are in an array an it is called again and again (when user click the button) without freeing it. I did not spent to much time studying the code, but it looks like the array is freed on Button1Click: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if Length(World)>0 then begin //Set array dimensions reset to [0,0] Quite important... saving memory for Row:=0 to WorldWidth-1 do begin    for Col:=0 to WorldHeight-1 do begin      World[Row,Col].Free;       end;  end; end; Or are you refering to something else? ---------------------------------------------------------------------- Handoko · 2019-01-28 13:37 I meant, the TS code is working correctly. So, it is not okay if freeing the objects on FormDestroy event. Please ignore this. ---------------------------------------------------------------------- balazsszekely · 2019-01-28 13:39 Did you try it with heaptrc on? Without the code(Form Destroy) the program leaks at my side. ---------------------------------------------------------------------- TomTom · 2019-01-28 13:53 Yes You're right Handoko, it's kinda slow when world size is big. I'm thinking intensely about using this buffer thingy and to  render only that what is visible on the screen (within ScrollBox).Especially that I'll be adding images to this, but that's future. I also need to look at refreshing/updating Scene (PaintBox). Now my code is messy ... I aware of that Anyways, I'm happy that memory leaks were eliminated. @GetMem Oh yes... I was flooded with error messages .. Now it all good Thank You Quote from: Handoko on January 28, 2019, 02:24:08 pm@TomTom My test show no memory leak. Your code runs slow on my computer. So I decided to make it automatically clicks for me: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button4Click(Sender: TObject);var  i: Integer;begin  for i := 1 to 100 do  begin    eww.TextHint := '24';    ewh.TextHint := '24';    Button1.Click;    eww.TextHint := '500';    ewh.TextHint := '500';    Button1.Click;    Caption := i.ToString;    Application.ProcessMessages;  end;end; Yes, I saw there are some rising of the memory usage but it will go down after a while. I think that is how OS memory manager works. I run the code above, the memory usage seems to be stable on my Ubuntu System Monitor. If you want, you can set the for-do loop 10000. @ GetMem Quote from: GetMem on January 28, 2019, 01:21:12 pm@TomTom Just free the objects on form destroy: That is not okay. Because the objects are in an array an it is called again and again (when user click the button) without freeing it. ---------------------------------------------------------------------- Handoko · 2019-01-28 13:55 Okay, I was wrong. I misunderstood what you previously said. I thought you're suggesting to move the code to FormDestroy. I checked it again, you didn't said that. I was posting that post while doing some other thing. ---------------------------------------------------------------------- Handoko · 2019-01-28 14:02 Quote from: TomTom on January 28, 2019, 02:53:51 pmYes You're right Handoko, it's kinda slow when world size is big. FYI, objects and strings are expensive, use them only when needed. They are okay for common GUI programs, but you should reconsider when using them in games. You used TTile in a huge array of array, that is not good for performance. Some players like big maps. Did you know Daggerfall? The game world is as big as Great Britain: https://en.wikipedia.org/wiki/The_Elder_Scrolls_II:_Daggerfall ---------------------------------------------------------------------- TomTom · 2019-01-28 14:29 Yes I know Daggerfall... I'm quite retro games lover Yes that was(still is) impressive world. But in my case, since I'm beginner etc. etc. for now it will good enough. In fact You helped me improve performace guiding me not to use TImage as tiles . I know this is not best approach to game programming and even with that could be better. But my goal is NOT to create game. My goal is to learn something. And I did learned new things (it's not that hard to learn something new when You're beginner  )). I think map 128x128 will be big enough for now ---------------------------------------------------------------------- Handoko · 2019-01-28 14:44 Quote from: TomTom on January 28, 2019, 03:29:04 pmit's not that hard to learn something new when You're beginner  Same as playing RPG games. It is very easy for leveling up for beginners, just kill some skeletons. But if you're on level 20, you need to slays several dragons. ---------------------------------------------------------------------- TomTom · 2019-01-28 16:10 And with that You gave idea for my another 'enemy' to slay...  some day. Roguelike would be nice. Always wanted to do that. But firs,t this game. I think that when I'll finish code for placing objects/buildings I'll try to design some more complex random generator so I could get proper island etc. Quote from: Handoko on January 28, 2019, 03:44:34 pmQuote from: TomTom on January 28, 2019, 03:29:04 pmit's not that hard to learn something new when You're beginner  Same as playing RPG games. It is very easy for leveling up for beginners, just kill some skeletons. But if you're on level 20, you need to slays several dragons. ---------------------------------------------------------------------- flowCRANE · 2019-01-28 16:13 @TomTom: you should reorganize the code of your project. The logic of the game should not be placed in the events of the form, and entirely separated into a set of classes. Besides, I see that you have a problem with proper class design (generally with OOP). Things like the space occupied by the building should not be stored in the building class. The building always occupies one tile, and to repaint it, coordinates in the array are enough. I suggest spending a little more time to design classes. I also suggest creating a class of the game engine that would manage the level surface and its contents (land and buildings). After moving the entire logic to the appropriate engine classes, you will only be called high-level methods in the form's events. Powodzenia. ---------------------------------------------------------------------- TomTom · 2019-01-28 16:35 Oh yes I know that I really need to work more on subjects You mentioned. Maybe this is too big project to start with :S. I struggle with this every day in my mind. Language (English) is also quite a barrier  for me when it comes to learning, reading docs and wiki. So sometimes I'm using trial and error method and keep trying until I think I understand. This is first project where I use my own classes and I don't understand everything (that's the reason of slow progress) So You're saying that I should do sth like this ? Create a class ... e.g. TGame class define properties, functions, procedures for it. and Then use only these procedures/fucntions etc. (e.g GenWorld(arg1, arg2,arg3); instead of writing whole GenWorld procedure inside Button1Click ... Did I get that right? I see advantages of that clearly, but I have bad habits from past so it's hard but I'm trying (GetTileTexture ) Nie dziękuję, Żeby nie zapeszyć ---------------------------------------------------------------------- Handoko · 2019-01-28 17:41 Separating code for logic and code for UI has many advantages. It is not very important on small projects that contain only few hundreds line of code. But if your project contains thousands LOC, it will make the code easier to maintain. When some buggy things happen you can easily to locate which part you need to inspect. Mixing code for logic and code for UI will lead it to become spaghetti code: https://en.wikipedia.org/wiki/Spaghetti_code OOP is like a double-edged sword. If you use it properly, it can help you easier to develop complex projects. But it also can hurt you, it may make simple thing becomes complicated. You can search the web, there is a quote that says: "You wanted a banana but what you got was a gorilla holding the banana and the entire jungle." I usually will only start a class if I'm sure I will develop it to have some subclasses. And that requires some proper planning before starting to write any code. Class that does not have any subclass (for example your TBuilding), usually can be easily to rewritten using the classic procedural way. And I believe you will gain more benefit instead. ---------------------------------------------------------------------- TomTom · 2019-01-28 18:17 Well I can confirm that I have moments when I think that I could get rid of this TBuilding and store every information I need in TTile (Income that is being generated, presence of Building, Building type, Tile type etc). Well now as I write this post it's becoming even more clear to me that this is right thing to do. (fascinating ) It's quite logical. I also must admit that sometimes it's hard to find part in my code which I need , even though it's so simple code. But somehow (for now ) I can deal with it (for how long? ). Maybe I should get rid of this bad habit as soon as possible. Is there any place (website,chat etc) to have that kind of conversations like we're doing here in this topic? I don't want to make mess on this forum with this "blah blah talk", yet I find it very inspirating and instructive   It would also improve my English ---------------------------------------------------------------------- flowCRANE · 2019-01-28 21:01 How to design classes should be mainly dependent on the functionality of your game. You can start from the bottom, first create classes describing a single building, then the map (level) and the whole game. You can also start from the top, in the reverse order than mentioned above. But before you start writing the code, you should plan the game well, describe each element in detail, and how it will interact with other objects and with the user. Otherwise, you will have to constantly modify the existing code instead of implementing further functionalities. If you want to have a good code, then you need to spend more time learning OOP. ---------------------------------------------------------------------- TomTom · 2019-01-29 12:52 @furious programming I Will do that Just after this project. I think the next one will be actual game, since this project is just map editor Today I've made better random generator which actually produce proper isles   But somehow I messed up building placing :S... So... now I have opportunity to experience  how hard is to debug such 'beautiful' code