Jump to content

bravesofts

Members
  • Content Count

    48
  • Joined

  • Last visited

Everything posted by bravesofts

  1. why this IfThen inline function gives me Object is Allocated in memory !! --- type TMainView = class(TForm) Label1: TLabel; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var MainView: TMainView; implementation type TMyClass = class private // fValue: string; class var ClassValue: string; function GetValue: string; procedure SetValue(const aValue: string); public property Value: string Read GetValue write SetValue; end; { TMyClass } function TMyClass.GetValue: string; begin Result := ClassValue; end; procedure TMyClass.SetValue(const aValue: string); begin ClassValue := aValue; end; { TMainView } function IsAllocated(aObj: TMyClass): Boolean; begin Result := Assigned(aObj); case Result of True: aObj.Value := 'the OBJECT Is Allocated ..'; False: aObj.Value := 'Not Allocated Yet !!'; end; end; procedure TMainView.FormCreate(Sender: TObject); var LObj: TMyClass; begin // LObj := nil; // LObj := TMyClass.Create; Label1.Caption := IfThen(IsAllocated(LObj), LObj.Value, LObj.Value); end; link to github test project here
  2. Did the tone of this conversation lean towards mockery or belittling the person asking the question, or am I mistaken? -- In any case, thank you for the information.
  3. i agree with you @Rollo62 on this term, we should be the first to ask a good questions to get a good answers with best accuracy of the truth .. By the way, the answer from ChatGPT above would have been impossible without the question being, in reality, the answer from the highly respected and dear-to-my-heart @Dalija Prasnikar. If you take a closer look at what ChatGPT provided above, you’ll notice that it did a wonderful job of summarize, re-format and complete ., and beautifully elaborating on the esteemed professor Dalija Prasnikar's response.. The above post from ChatGPT 4.o is the best proof of what you say. --- finally , I want to thank you all once again for your amazing and enthusiastic contributions. Your accuracy in providing information has really helped me understand and sum up a journey that many people might still be struggling with.
  4. i ask GPT for a good Example and gives me this: procedure TMainView.Btn_1Click(Sender: TObject); var LObj: TMyClass; // Declare a local variable of reference type begin // At this point, LObj is just a pointer, and it hasn't been assigned to any object yet. ShowMessage(IntToStr(Integer(LObj))); // This may show some random (garbage) value LObj := TMyClass.Create; // Now, LObj points to a new instance of TMyClass allocated on the heap. ShowMessage(IntToStr(Integer(LObj))); // This will show the actual memory address of the object instance // When you’re done, free the object to avoid memory leaks. LObj.Free; end; Explanation LObj Declaration: LObj is declared as a variable of type TMyClass. This is a reference type, which means LObj is just a pointer (an address) that will eventually point to an object instance. Memory Allocation: LObj itself is a local variable, so it is allocated on the stack. However, the object instance that LObj will point to is allocated on the heap when you create it using TMyClass.Create. Garbage Value: When LObj is declared, it hasn't been initialized to point to anything. The memory allocated on the stack for LObj might contain whatever data was there before, which is why ShowMessage(IntToStr(Integer(LObj))) might display a random (garbage) value. This is because stack memory isn't automatically cleared. Object Creation: When you create a new instance of TMyClass with LObj := TMyClass.Create;, the memory for this instance is allocated on the heap. LObj now holds the memory address of this instance. Managed Types: Only managed types (e.g., strings, dynamic arrays, interfaces) are automatically initialized to nil or equivalent values when declared. Since LObj is not a managed type, it can hold any value until you explicitly initialize it by creating an object. Key Takeaways Uninitialized Pointers: If you don't initialize a reference type variable (like LObj), it might contain a garbage value that points to an undefined memory location. Memory Allocation: Local variables are allocated on the stack, but the actual object instances they point to are allocated on the heap. Automatic Initialization: Only managed types are automatically initialized to nil or similar default values when declared. This example illustrates the importance of initializing your reference variables to nil or creating an object before using them to avoid unexpected behavior. ---- I have a much clearer understanding of the entire process now. Thank you, everyone!
  5. So, the heap might contain some garbage data, and when we declare a variable, the pointer may refer to something that isn’t related to our program at all. Does this happen only in Windows?
  6. Do random values only occur for local variables? Why does uninitialized memory sometimes contain garbage values, especially when an object hasn't been allocated yet? LObj is just a variable that will reference the object once TMyClass creates it. Does TMyClass automatically assign anything to LObj just by declaring it like this? var LObj: TMyClass;
  7. the Purpose of reintroduce is to make a second introduce for same method from base class (not marked as virtual or dynamic) with overload arguments in Drived Class whith inherited ofcourse .. reintroduce let us add inherited in implementation of that overload method that reintroduced in Derrived Class ex: type TBaseClass = class public constructor Create; // no virtual or dynamic end; TDerivedClass = class(TBaseClass) public // constructor Create; overload// introduced as overload but no arguments there !! constructor Create(aArg:T); reintroduce; overload; end; implementation { TBaseClass } constructor TBaseClass.Create; begin // for example No code here !! end; { TDerivedClass } constructor TDerivedClass.Create; begin inherited Create; // reintroduce let us make inherited here //since base method is not marked as virtual or dynamic .. MyCode; // our method here will implement both (the inherited + MyCode) // but since the inherited is empty : the reintroduce here is Totally useless !! // the aim of reintroduce is to let us Merge the old method from base to reintroduce it // as a new method in derived class using directive [inherited] // does the overload necessary ? // if there is no necessary to add new arguments you can add just reintroduce to use // the inherited directive there .. // Is inherited mandatory when using reintroduce ? // a reintroduce without inherited is totally nosense !! end; finally : reintroduce is used especially for two Reasons: make inherited + overload with new parameters for same base method in Drived class make just inherited without overload it with new parameters Q1)does reintroduce work also with base methods marked as virtual or dynamic ? A1) do you heard about override ? Q2) but if i need to introduce it with my custom parameters ? A2) if that case ofcourse you need reintroduce; with overload; Q3) is overload mandatory ? A3) since reintroduce her second work is to hide any warnning compiler i can say yes the overload is a mandatory logically .. ----- if i'm wrong plz tell me (Many thanks in Advance)
  8. unit API.Generics; interface uses System.Classes // , System.Generics.Collections , System.Generics.Defaults ; type TSort = (sNone, sAsc, sDes); TDynamicObjectList<T: class> = class(TObjectList<T>) private fComparer: TComparison<T>; fSortField: string; fSort: TSort; function CompareNumbers(const L, R: Integer): Integer; function CompareObjects(const aLeft, aRight: T): Integer; public constructor CreateWithSort(const aSortField: string; aSort: TSort = sAsc); procedure Sort(aSort: TSort = sAsc); function IsSortedCorrectly: Boolean; end; implementation uses System.SysUtils , System.Rtti , System.TypInfo ; { TDynamicObjectList<T> } constructor TDynamicObjectList<T>.CreateWithSort(const aSortField: string; aSort: TSort); begin inherited Create(True); fSortField := aSortField; fSort := aSort; fComparer := CompareObjects; end; function TDynamicObjectList<T>.CompareNumbers(const L, R: Integer): Integer; begin Result := L - R; end; function TDynamicObjectList<T>.CompareObjects(const aLeft, aRight: T): Integer; var L_Ctx : TRttiContext; L_Typ : TRttiType; L_Prop : TRttiProperty; L_Left : TClass absolute aLeft; L_Right : TClass absolute aRight; L_LeftValue, L_RightValue: TValue; begin if fSortField = '' then begin // Use default comparer if no specific field is specified .. Result := TComparer<T>.Default.Compare(T(L_Left), T(L_Right)); Exit; end; L_Ctx := TRttiContext.Create; try L_Typ := L_Ctx.GetType(T); // Get RTTI for type ( T ) L_Prop := nil; L_Prop := L_Typ.GetProperty(fSortField); if Assigned(L_Prop) then begin L_LeftValue := L_Prop.GetValue(L_Left); L_RightValue := L_Prop.GetValue(L_Right); case L_LeftValue.Kind of tkInteger, tkInt64: case fSort of sAsc: Result := CompareNumbers(L_LeftValue.AsInteger, L_RightValue.AsInteger); sDes: Result := CompareNumbers(L_RightValue.AsInteger, L_LeftValue.AsInteger); else Result := TComparer<T>.Default.Compare(T(L_Left), T(L_Right)); end; tkString, tkWString, tkLString, tkUString: case fSort of sAsc: Result := CompareNumbers(Integer.Parse(L_LeftValue.AsString), Integer.Parse(L_RightValue.AsString)); sDes: Result := CompareNumbers(Integer.Parse(L_LeftValue.AsString), Integer.Parse(L_RightValue.AsString)); else Result := TComparer<T>.Default.Compare(T(L_Left), T(L_Right)); end; else TComparer<T>.Default.Compare(T(L_Left), T(L_Right)); end; end else Result := 0; // Handle case where property is not found finally L_Ctx.Free; end; end; function TDynamicObjectList<T>.IsSortedCorrectly: Boolean; var I: Integer; begin Result := True; for I := 1 to Count - 1 do begin if CompareObjects(Items[I - 1], Items[I]) > 0 then begin Result := False; Break; end; end; end; procedure TDynamicObjectList<T>.Sort(aSort: TSort); begin fSort := aSort; inherited Sort(TComparer<T>.Construct(fComparer)); end; end. using: in separate Unit: MyFonts.pas type TFonType = (ft_TTF, ft_OTF, ft_Unknown); TFontInfo = class strict private fFileID, fFontName : string; fFontType : TFonType; fFontFileName: string; private function Get_FontType: string; procedure Set_FontType(const aValue: string); public constructor Add(const aFileID, aFontName: string; const aFontType: TFonType); destructor Destroy; override; property ID: string read fFileID write fFileID; property FontName:string read fFontName write fFontName; property FontType:string read Get_FontType write Set_FontType; property FontFileName: string read fFontFileName write fFontFileName; end; TFontsList = class(TDynamicObjectList<TFontInfo>); in MainForm: uses MyFonts; procedure TMainView.FormCreate(Sender: TObject); var L_FontInfo: TFontInfo; L_ListItem: TListItem; begin fFontsRes_List := TFontsList.CreateWithSort('ID'); // Add some sample data fFontsLst.Add(TFontInfo.Add('3', 'Courier New', ft_TTF)); fFontsLst.Add(TFontInfo.Add('1', 'Arial', ft_TTF)); fFontsLst.Add(TFontInfo.Add('2', 'Times New Roman', ft_OTF)); // Sort the list by ID in Asc fFontsLst.Sort(sAsc); // Populate the ListView for L_FontInfo in fFontsLst do begin L_ListItem := LV_Fonts.Items.Add; L_ListItem.Caption := L_FontInfo.ID; L_ListItem.SubItems.Add(L_FontInfo.FontName); L_ListItem.SubItems.Add(L_FontInfo.FontType); L_ListItem.SubItems.Add(L_FontInfo.FontFileName); end; end; procedure TMainView.FormDestroy(Sender: TObject); begin fFontsRes_List.Free; end; currentlly it's works only with fields marked as string or integer but real value are in integer and sorted using : CompareNumbers
  9. ok , I totally agree with that definition in dokwiki.. but here: https://stackoverflow.com/questions/53806/reintroducing-functions-in-delphi plese someone tell me Why all this confusion in link above, about something that was created specifically just for that senarion in dokwiki? atleast everyone there has a logic reason for that.. Please, I need to turn this page permanently... Jim McKeeth said: If you declare a method in a descendant class that has the same name as a method in an ancestor class then you are hiding that ancestor method — meaning if you have an instance of that descendant class (that is referenced as that class) then you will not get the behavior of the ancestor. When the ancestor's method is virtual or dynamic, the compiler will give you a warning. Now you have one of two choices to suppress that warning message: Adding the keyword reintroduce just tells the compiler you know you are hiding that method and it suppresses the warning. You can still use the inherited keyword within your implementation of that descended method to call the ancestor method. If the ancestor's method was virtual or dynamic then you can use override. It has the added behavior that if this descendant object is accessed through an expression of the ancestor type, then the call to that method will still be to the descendant method (which then may optionally call the ancestor through inherited). So difference between override and reintroduce is in polymorphism. With reintroduce, if you cast the descendant object as the parent type, then call that method you will get the ancestor method, but if you access it the descendant type then you will get the behavior of the descendant. With override you always get the descendant. If the ancestor method was neither virtual nor dynamic, then reintroduce does not apply because that behavior is implicit. (Actually you could use a class helper, but we won't go there now.) In spite of what Malach said, you can still call inherited in a reintroduced method, even if the parent was neither virtual nor dynamic. Essentially reintroduce is just like override, but it works with non-dynamic and non-virtual methods, and it does not replace the behavior if the object instance is accessed via an expression of the ancestor type. Further Explanation: Reintroduce is a way of communicating intent to the compiler that you did not make an error. We override a method in an ancestor with the override keyword, but it requires that the ancestor method be virtual or dynamic, and that you want the behavior to change when the object is accessed as the ancestor class. Now enter reintroduce. It lets you tell the compiler that you did not accidentally create a method with the same name as a virtual or dynamic ancestor method (which would be annoying if the compiler didn't warn you about). is there a deep example where introducing each case with strong reason individually ? It’s essential for my overall well-being to close this chapter once and for all...
  10. I'm confused...why didn't the Embarcadero take this seriously? Why does every Delphi developer have to install more than one version of Delphi to be able to support almost Android phones? On the other hand we find Android Studio, for example, with only one version of that IDE that still covers all Android phones, starting from android 5 up to the current latest version.!! is there a workaround solution that help to make Delphi 12 support even old Android phones? i mean (a real support not fake..) is there a complete SDK solution where users under delphi12 could support old phones starting from Android 5 to the latest current version ? thank you All
  11. bravesofts

    making Delphi 12 support even old Android phones?

    wow thank you @Dave Nottage that solution from [Delphi World] is working as a charm !
  12. i have this code here: fTask: ITask; fTask := TTask.Create( procedure begin while not True do Application.ProcessMessages; TThread.Synchronize(TThread.Current, procedure begin // update ui end); end); fTask.ExecuteWork; // this will stop here.. until [fTask] finish work !! In android didn't wait untill Task Complete !!
  13. sorry @Remy Lebeau no i didn't at that way !! --- i ask both question at the same time !! --- and with Your big and huge respect of your knowledge, I'm done learning that Method: [ITask.ExecuteWork] from ITask is: runs the task in the calling thread ... ---- again please: Forgive my ignorance and stupidity !! i was just there to ask and not because this site or other didn't help !! --- finally . i thank you all for your replys and especially for taking from your time.. i love you all & respect you all .. @Dalija Prasnikar regardless of others... i respect you a lot .
  14. also i learn that the Method ExecuteWork: [ is used to run the task in the calling thread => in my case the main App thread ] like inject or run a safe thread inside the MainThreadAPP and this exactly what i looking for.... --- so my question here: Why Method ExecuteWork Fail in mobile device (especially ANDROID) ? ----- is this have problem with Android OS ?? ----- Please don't tell me that Method : ExecuteWork is not a regular or illegal or what ever else ... otherwhise why the EMB Group add it inside the interface ITASK ? ---- what i know in language programming : the interface is safer and destinated at-least to users like me .... --- with best Respects & Regards ...
  15. sorry again ... --- last question: is it impossible to create threads in mobile devices while avoiding the main app thread from going to the next line of code until tasks complete ? for example this code here didn't work in android devices: begin _Taskcomplete := false; try fTask := TTask.Create( procedure begin // Here you do your task code while not _Taskcomplete do Lbl_Task.Text := ElapsedSec.ToString; TThread.Synchronize(nil, procedure begin // Here you can call code that needs to run after task is finished Lbl_Task.Text := 'task complete ..'; end); end); fTask.Start; finally while not _Taskcomplete do Application.ProcessMessage; // in Android the APP freeze !! showMessage('we wait until Task Complete to show you this message !!'); end; end; from what i learn above is i can only use wait function to run the SoCalled "After Task Complete Procedures or functions" inside the task thread using synchronize method..!!
  16. Not block the main thread in that definition... !!! I think the stupid sleep function is the unsafe way to block main thread... ---- I mean block main thread using the safe way like executeWork does... ---- I'm afread again : Delphi does not support background threads in Android and iOS devices where the main thread wait smoothly and safe...
  17. Is there a safe solution to block the main thread smoothly without freezing the app while waiting our task to complete "using the right task way above.. " Especially in mobile devices
  18. About my repository didn't help you understand my request... --- I simply if I Don't use itask.ExecuteWork; and use the regular Start; The main thread App will not wait for task completion and execute the next line code... But with executeWork I succeeded to force main thread App to wait smoothly without freezing the app.... I don't like use sleep function inside while loop in order to execute background thread... --- Try to use my wrong code (like I said before it's not complete...) in VCL and FMX windows example and you will find the magic of executeWork method.. --- NB: sorry for my wrong solution in my GIthub..
  19. In android maybe android service could help... Or add some Java activities that play our background threads safely while our main thread App is using while loop application.processmessage;
  20. Unfortunately didn't help at all... I see that post before..
  21. Is there any solution targeting the mobile devices!! ---- I'm afraid that : Delphi does not support background threads in Android and iOS devices !! ---- And if yes... I think it's OS problem or Firemonkey single Main Activity doesn't accept more activities !!!
  22. This is my git repository uses that method executeWork: Wait Solution Here Maybe help to understand my request.. Nb: my repo not complete yet... Wait Solution (VCL/FMX) Stop the Use of Stupid Sleep function !! Now with this Solution : your Main thread Should Wait until your wait function finsh her work without freezing the entire App using the stupid Sleep function !!! other Threads that are runing outside your MainThread (they still could work without Any Problem!!) using the method ExecuteWork from ITask we can force the App to wait until Our WaitThread finish work
  23. First : i have this code: type TClass_Instance = class(TClass_Ancestor) end; what happen at runtime when our program is using this Unit? i mean exactly this Explicit : TClass_Instance = class(TClass_Ancestor) does this Allocate something in Memory ? or system resources .... second: ihave this declaration here: var vObj_Instance: TClass_Instance; begin end; what happen at runtime when we are declaring some Objects ? i mean exactly this Declaration : vObj_Instance: TClass_Instance; does this Allocate something in Memory ? or system resources .... third: i have this code here: type {$M+} TBaseForm = class(TForm) // This my template Base Form procedure Abstracted_Event(Sender: TObject); virtual; abstract; end; TSubBase_1 = class(TBaseForm) // SubBase_1 Members goes here .. published procedure Abstracted_Event(Sender: TObject); Override; end; TSubBase_2 = class(TBaseForm) // SubBase_2 Members goes here .. published procedure Abstracted_Event(Sender: TObject); Override; end; TSubBase_3 = class(TBaseForm) // SubBase_3 Members goes here .. published procedure Abstracted_Event(Sender: TObject); Override; end; i have this super procedure for calling sub forms acording to their Class Names: procedure Get_SubForm(AFormClass: TFormClass; aOwner: TComponent; aParent: TWinControl); var vObjInstance: TBaseForm; // Define Object Instance Type; vRef: TBaseForm; // our Object vRef is the Universal variable for All SubForms that inherited from TBaseForm begin vObjInstance := TBaseForm(AFormClass.NewInstance); //Fill Object [vObjInstance] with [AFormClass.NewInstance] .. vRef := vObjInstance.Create(aOwner); //Fill Object [vRef] with [vObjInstance.Create] .. try vRef.Parent := aParent; vRef.Align := alClient; vRef.BorderStyle := bsNone; vRef.OnShow := vRef.Abstracted_Event; finally vRef.Show; end; end; my question is : what happen here: vObjInstance := TBaseForm(AFormClass.NewInstance); //Fill Object [vObjInstance] with [AFormClass.NewInstance] .. at runtime ...? does this line Above is trying to replace the first declaration Above here: var vObjInstance: TBaseForm; // Define Object Instance Type; in other words: does NewInstance Above is trying to redeclare our variable vObjInstance with Another Type ? if not (so what happen exactlly ?) Another sample Example: procedure TFrmMain.Btn_CreateObjClick(Sender: TObject); var vObj_Instance: TButton; // declaration Type of our Object vRef: TButton; begin vObj_Instance := TButton(TButton.NewInstance); vRef := vObj_Instance.Create(Self); try vRef.Parent := Self; vRef.Name := 'Btn_vObj'+ ComponentCount.ToString; vRef.SetBounds(Round(8 * fScaleFactor), Round(48 * fScaleFactor), Round(185 * fScaleFactor), Round(41 * fScaleFactor)); finally vRef.Visible := True; end; end; here i'm using the same approach Above that using TBaseForm but with one base Class TButton my question is: we used to use this Rule here: <vObject> := <ClassName>.<ConstructorName> ; but here we have an object trying to Allocate the memory (fiil the Class inside that variable) for Another Object ... my Remark: procedure TFrmMain.Btn_CreateObjClick(Sender: TObject); var // vObj_Instance: TButton; vRef: TButton; begin // vObj_Instance := TButton(TButton.NewInstance); vRef := Btn_CreateObj.Create(Self); try vRef.Parent := Self; vRef.Name := 'Btn_vObj'+ ComponentCount.ToString; vRef.SetBounds(Round(8 * fScaleFactor), Round(48 * fScaleFactor), Round(185 * fScaleFactor), Round(41 * fScaleFactor)); finally vRef.Visible := True; end; end; if we click on button Btn_CreateObj the object will be created but the Button Btn_CreateObj will be disappear !!! and when closing the app will have a memory leaks !!! my Approach of using another variable as a mediator, remind me with A & B replacement values var A, B, C: integer; begin A := 10; B := 5; // Solving requiring us Another Variable with the same Type C := A; A := B; B := C; // AND IT'S DONE .. end; finally: what is a Class Instance and an Object Instance? and is my Approach for creating base forms is a good approach or not ? thanks in Advance...
  24. honestly i'm a native Delphi !!! ------- my post here is to understand exactly what delphi is that i was used before without a deep attention to delphi details
  25. and what about the exe it's self ? does A variable having a type equal to a class is actually a pointer to the memory allocated for the class instance inside the exe it's self and not comming from calling the constructor. !!! i mean the delphi is deployed for us a ready to use objects variables that pointer to the exe it's self (pre Allocated Classes)
×