

Kas Ob.
Members-
Content Count
570 -
Joined
-
Last visited
-
Days Won
10
Everything posted by Kas Ob.
-
Avoid parameter evaluation
Kas Ob. replied to Maxime Janvier's topic in RTL and Delphi Object Pascal
There is few ways i can think of like these 2 approaches type TLogLevel = (llInfo, llTrace, llError); procedure Log(LogLevel: TLogLevel; const LogText: string); overload; begin end; procedure Log(LogLevel: TLogLevel; const Text: array of string); overload; var Count: Integer; begin Count := Length(Text); case LogLevel of llInfo: ; llTrace: begin if Count > 0 then begin end; end; llError: ; end; end; procedure Log(LogLevel: TLogLevel; const LogText: string = ''; const Error: string = ''; const Additional: string = ''); overload; begin end; procedure Log(LogLevel: TLogLevel; const LogText: string = ''; ErrorCode: Integer = 0; const Additional: string = ''); overload; begin end; procedure TForm10.FormCreate(Sender: TObject); var BuildStringFromContext: string; begin Log(llTrace, ['Test', IntToStr(GetLastError), BuildStringFromContext]); Log(llError, 'Test', GetLastError, BuildStringFromContext); Log(llError, 'Simple Text', 0); Log(llInfo, 'Simple Text', 0, 'Additional Info'); Log(llInfo, 'Simple Text', ''); end; Just food for thoughts for you, using array of string is easy and will make you life easier, but you should remember the order, while the overloaded versions with default empty values, are more readable and harder to confuse. Each had its own shortcoming, like i had to add 0 do the ambiguity, but if you make you mind you can build them right and remove the need for these extra 0 or ''. It is up to your need to think of a solution that you are comfortable with. -
Access violation errors while running without the debugger
Kas Ob. replied to christos papapostolou's topic in Algorithms, Data Structures and Class Design
Yes , and to be more accurate explaining this case with both addresses are the same, The EIP register pointing to a page-protected address, means the CPU is already try to fitch an instruction while the virtual mode triggered a page fault, hence the same address of the execution and accessing, this happens with few cases, like the stack is corrupted and a ret instruction caused the CPU to pick an invalid address to return to, or a hook or resolved API addresses pointing to a library, and that library has been already freed and its memory returned to the OS where marked again as protected or not allocated. Not long ago there was similar bug discussed in the German forum comes from broken hooking that corrupt the stack after calling the hook, sorry can't find it now ! may be someone can help with that. @christos papapostolou i would suggest to check and refine your uses clauses and find any exotic units or library that are outdated, also try to capture the stack list, it will help. -
“Transitive” type redefinitions in interface section
Kas Ob. replied to Dmitry Onoshko's topic in Algorithms, Data Structures and Class Design
Frankly speaking, this part is quite unexpected for me, since I never had a chance to dive deep into RTTI, and the “classical” Pascal treated types T1 and T2 equal whenever there ould be a T1 = T2 declaration. I think I understand the reasons, but I’ll have to read more on topic to be sure. Well, hold your horse here for minute. I might made a mistake here, or did i ? Things seems a little different between Delphi 2010 and XE8, so it might be different for you with your version. My mistake is between these type TSomeType = MyTypes.TSomeType; type TSomeType = type MyTypes.TSomeType; the latter does have different RTTI for sure, but for the first is does not, and that is my mistake, yet helpers is confused as hell between the two type and my two already old Delphi versions compiler, one allow it and the other is buggy and confuse them, and from there my mistake did come. i rarely used similar approach for such renaming or like this var LDefObject: TObject; LObject: TMyObject; LMyObjectEx: TMyObjectEx absolute LObject; begin Memo1.Lines.Add(LDefObject.HelperName); Memo1.Lines.Add(LObject.HelperName); Memo1.Lines.Add(LMyObjectEx.HelperName); To have my own string helper with casting and without losing the default string helper, and i don't know if this is working on the newer versions, yet now i trying to add a helper for string on my Delphi 2010 and it does allow it -
“Transitive” type redefinitions in interface section
Kas Ob. replied to Dmitry Onoshko's topic in Algorithms, Data Structures and Class Design
Hi, Few thoughts on this: 1) If MyTypes is used through out the project then no point of renaming or redefining its types in other units, it will be there and everywhere. 2) From what i understand, UnitB is to somehow can be named as UnitAEx or UnitAHighLevel, this will draw clear path of using and remind of adding UnitA where it extended version is used, the one (not/ex) called UnitB. 3) About this I don't think i ever used such renaming easily as it is easily can tangle things and cause more and different problem in the future, the one you missed now, see TSomeType on the left is a rename or redefine of MyTpes.TSomeType, yes they are the same, but in you view not for the compiler, example RTTI is different for them and they are not equal ! also in some places you will see the compiler might complain about the definition, usage and passing them as parameters .... also it might broke things badly in case such units used in an Design Time package. I see if that is needed then use it where is the impact is smallest, like in UnitB to rename types from UnitA instead of redefining types from MyType unit into UnitA, try to narrow the scope in it is needed. Now to the questions I am no aware of such best practice if it is exist, i follow best practice and simple and short logic, keep it stupid simple, and also untangled as much as possible. OK it is, yet it depends on the case and what could be changed in the future, rarely one can see that today, i made so many mistakes, not real mistakes as this is not a mistake per se, it is just short sighting and not leaving doors open but .... not tangle many things together. No sure i do understand the question, but i hope my points above could give you some insights. The above is my personal opinion, and i am sure many in the forum can have helpful insights on this. -
@#ifdef Well , while i hate my life nowadays, and while just waiting for the blackout to brighten my day after only 3 hours of power, i tried to build you something Far from finished or polished, take it as example or prototype for what you can do with custom drawing, notice it is 10000 strings ! ListBoxAsStringList.zip My suggestion is to use VirtualTreeView, i couldn't find working copy of VTV on my PC now, and don't want to waste time downloading my encrypted archives from the cloud, so i used TListBox, it is simple easy and pretty straight forward. Notes: 1) i didn't use FillRec on the whole Listbox, while it is more efficient and faster to clear the background for all the items in one go, this will introduce an ugly flicker in the bottom half, do the system rendering which doesn't wait for anyone, by clearing the background for each item in time, we prevent this flicker, but will introduce small performance hit in overall drawing. 2) i disabled the selected item highlighting, because it does need little different approach, yet it is easy, just adjust the canvas brush color before the FillRect, this will be up to you to implement, as i said i don't have time and don't have the skins on the latest IDE versions to test and play with. 3) customize as you wish, like ... can you this ? ( there is no noticeable performance change) Hope that helps, also never underestimate Windows custom drawing, it is fucking blazing fast when done right.
-
How to debug a Not Responding program element
Kas Ob. replied to Willicious's topic in Delphi IDE and APIs
This access violation is classic corrupted stack, the stack either being overwritten or wrong casting being used with some procedure/function led to not aligned call/ret addresses on the stack. On sidenote: that address is more like the EXE is not there at all, like it gone, yet the debugger/OS reporting a failed execution on no execution memory address, ... ( it also could be a DLL though). I can't compile the code and don't have experience with MadExcept, but this doesn't sound right at all, and this brings me again to the why MadExcept failed to report the hundreds of leaks and stopped at string with 3 refCount ? There is something wrong with MadExcept or miss configuration in its usage, are you using it with FastMM or any other unit as first use in the dpr file uses clauses ? -
How to debug a Not Responding program element
Kas Ob. replied to Willicious's topic in Delphi IDE and APIs
That is 58 object leak of TGadgetMetaAccesseor ! This should be easy to track and fix, fixing this part most likely will fix most of the others in that screenshot. Now, and from the pasted code, i can't figure out where "GadgetAccessor: TGadgetMetaAccessor;" is freed, these are local variables so where are they used and to whom they are shipped, notice and follow how these variables are referencing each other. procedure TGadgetMetaInfo.Load(aCollection,aPiece: String; aTheme: TNeoTheme); var .. GadgetAccessor: TGadgetMetaAccessor; NewAnim: TGadgetAnimation; .. begin .. GadgetAccessor := GetInterface(false, false, false); // <----- .. NewAnim := TGadgetAnimation.Create(0, 0); // <----- GadgetAccessor.Animations.AddPrimary(NewAnim); NewAnim.Load(aCollection, aPiece, Sec.Section['PRIMARY_ANIMATION'], aTheme); fFrameCount := NewAnim.FrameCount; PrimaryWidth := NewAnim.Width; // Used later Sec.DoForEachSection('ANIMATION', procedure (aSection: TParserSection; const aIteration: Integer) begin NewAnim := TGadgetAnimation.Create(GadgetAccessor.Animations.PrimaryAnimation.Width, GadgetAccessor.Animations.PrimaryAnimation.Height); GadgetAccessor.Animations.Add(NewAnim); NewAnim.Load(aCollection, aPiece, aSection, aTheme); end ); ... end; Where and how these are released, that what you should find and fix, as for the UnicodeString leak, it might be simply a confusion/confliction in madshi with MemoryManager, that lead to miss identifying the leaks and their types. -
By the way Notepad++ with 14mb text file with longs lines, very fast without Word Wrap, does stutter on resizing with Word Wrap enabled.
-
It is owner drawn in full. Well i said i can't compile your project, and if all of these text segments are ... What ? (i don't care) TLable or TMemo, then use 50-80 and just update their content simulating hundreds or thousands, instead of creating hundreds, update their contents based on the scroll bar. You could also try to create the needed amount and try to clear the non visible and fill the few visible when resizing, i am throwing ideas here and hope might help.
-
So these are TMemo(s) in the list ! not even simple and plain text lines in a list box. Anyway, same recommendation as for single Memo or ListBox, switch to owner drawing, then limit what you are using/rendering/showing to the visible ones, while simulate/emulate a scroll bar on the side, or it might support its own scrollbar, who knows.
-
@#ifdef Well i can't compile your project nor i have an idea what TControlList is, yet from your last post i can deduce the problem. Se, same behavior is visible with Windows Notepad, you can try it, open a big text file something around few MBs and make sure that Word Wrap is enabled, and see for your self, same behavior, disabling the Word Wrap will make it fast like it was few lines. Calculating the height of paragraph is demanding process, because it only possible with a font and a width, after that rendering will happen again with the font applying device context parameters, so it might be doable but not like that it must involve caching. The only components i know of that do this right (or lets say fast) is the controls from Delphi HTML components. Yet, there is may be a workaround in your case, which is switching to virtual drawing, and rendering only what should be visible while emulating the scrollbar position at side, in other words you need to switch to owner draw and draw text manually what is enough to fill the control.
-
Thank you !
-
I am sorry too, but look at this code as example type TFileCopyClass = class private fFileSrc: TFileStream; fFileDst: TFileStream; public constructor Create(const Source, Destination: string); destructor Destroy; override; end; { TFileCopyClass } constructor TFileCopyClass.Create(const Source, Destination: string); begin fFileSrc := TFileStream.Create(Source, fmOpenRead); fFileDst := TFileStream.Create(Source, fmOpenWrite); end; destructor TFileCopyClass.Destroy; begin fFileDst.Free; fFileSrc.Free; inherited; end; What are the value at the "begin" in that constructor ? The answer is arbitrary and random even if you see it as nil most the time, these are unmanaged fields/variables. So if TFileStream raised an exception then the value of lets say fFileSrc is undefined and might be not nil, hence causing another unknown exception in the TFileCopyClass.Destroy; Am i right there or i am missing something ?
-
or: https://stackoverflow.com/a/39110161 You are missing the point of best practice to ensure memory integrity, Yes if an exception is raised in the constructor then the class itself is nil and will not leak memory, as pointed by @Dalija Prasnikar but if it is already allocated/created stuff then this stuff will leak so instead of depending on extra work to ensure non of the "stuff" had leaked just use best practice. On other hand, what pointed by @dummzeuch and @Dalija Prasnikar it does ignore the fact that that FSomeHelperObject is not managed variable hence it might have any value initially (random from the memory), rendering the following worst case scenario by raising unexplained and hard to track exceptions if Assigned(FSomeHelperObject) then FSomeHelperObject.Free; In other words, when an exception is raised in the constructor, you can't control or know where the stopping point was, unless considered this at designing, thus if you want to raise exception in the constructor then you need to do the extra work, like assigning FSomeHelperObject to nil before creating it ! and live with the compiler complaining about useless line and most likely the compiler will remove it, again rendering the code vulnerable to unexplained exception.
-
Because there is a very popular of miss practice with Pascal and Delphi which is (ab)using the constructor and destructor for everything, i do it, and every one is doing it, even the RTL since Borland is did it .. and to be clear the ones that should be used for any sort memory initialization, object creation, or what ever other than initialize values for the unmanaged object fields are procedure AfterConstruction; virtual; procedure BeforeDestruction; virtual; These should be used for creating object fields/variables and/or initializing values for managed fields...etc , with these you are free to raise what ever you want, a nice exception will be raised and a nice memory leaks will be reported, in other words the fail will be graceful and easy to locate. Another approach: Also as in OP class above, if you need to raise an exception then in my opinion the class design can be written better by separate Create and adding an Init (or Start...), Create to just create and another for functionality that might fail, this will %100 be memory leak proof no matter what is the case, and if you think about it, create doesn't raise and will not fail, while the failure might come form something like an Init procedure, here failed or exception raised or what ever happen, Create didn't (and will not) break the logic flow of the application and Free is guaranteed to work. In case of wondering what will happen with Out of Memory, then either Create will fail from the RTL, or will not fail because we don't allocate or create anything in the constructor, and that is the point of this suggestion.
-
Hi, Don't concern yourself with this one, it is irrelevant to your code, you should for/at the calls after it. Both allocation are reported from this line This where you registered packages being loaded, this means it might be one package (or two) had been loaded and not unloaded in orderly manner, or/and this package had allocated memory in its initialization and never freed it. Find the runtime package you are using and fix it. Also as @Tommi Prami pointed, try to not raise exception in constructors and destructor. Your TTCPEchoDaemon code you pasted is irrelevant to the memory leak, but if you are doubting it then i inclined to remind you, to check how and where are declared and initialize this EchoDaemon, it could be the leak as the size of the leak is conveniently close enough to the size of your class. In other and short words, the leak is happening in initialization section in one unit (most likely one unit).
-
Threadvar "per object"
Kas Ob. replied to chkaufmann's topic in Algorithms, Data Structures and Class Design
Well, i still can't see how threadvar will help here. I mean you wrote "I call DisableNotifications", how and from where ? see, again threadvar are invisible to other threads and to reach them you need this thread to get some value in like receiving signal (notified to update) or reading from somewhere fixed or predefined, or accessing a already known list, to get the needed value then store it in threadvar. again in that case and after going through all this troubles to get these values, threadvar is not providing any thing useful, because you already implemented the data passage and sharing/signaling/locking to the specific thread. As a solution then just have a list or an array with objects or records, example : an object like your TBSItemProvider that is created by the main thread (the origin doesn't matter) and attached/passed to the thread, this will simplify the whole thread communication process. Of course you need to take care of thread synchronization in case you will do access them randomly, but again in case of threadvar will work for you then such a record or object will be the same. -
Threadvar "per object"
Kas Ob. replied to chkaufmann's topic in Algorithms, Data Structures and Class Design
Threadvar(s) are invisible to anything else than this thread (current), and each thread have its copy. So if you are going to use them from different thread like the main or whatever, you need to store them or store a reference to them somewhere and this somewhere will definitely need a thread locking or thread safe storage, thus this storage will negate all the trouble to use them in first place, use global list or something, by global i mean centralized list or container with adequate thread safety. in other words You don't store data block per thread and per object at the same time, attach one and reference the other. -
No, and LIB files is not supported by Delphi for good reason, which is hell of naming and scope, doable though. LIB files are merely a package of object files (.o or .obj), which does have COFF format only with files/sections, in other words it is an uncompressed container of list of files, and that is it. You can use LIB in Delphi but you need to do some conversion first: 1) unpack all the object files form the LIB, there is many tools on the net to do so, there is many tools to do so but you need to search for them and try the one that work with your lib file as different compilers tend to produce a little different lib format or tweaks, while most such tools are built for specific format/tweaks. 2) link the unpacked objects files into one object file, many linkers do this with the command -relocatable (or -r) , though not all linkers support wildcard like *.o and you need to list all the names of object files, and the -o parameter will give you the one linked (combined) one object. 3) use the one object file from (2) same as you use o files, in this case it is the lib itself. extra info https://stackoverflow.com/questions/3811437/whats-the-format-of-lib-in-windows
-
Hi, @Jim McKeeth The survey is missing Nexus Quality Suite ! Edit: didn't notice that there is a special field for the missed ones.
-
Changes in System.sysutils.pas were not reflecting in other unit in Delphi 11
Kas Ob. replied to sp0987's topic in General Help
Well, bugs happen and knowing more about edge cases will save time, it is good thing to know more. Learning English is important, learning programing on deeper level is importanter. -
Changes in System.sysutils.pas were not reflecting in other unit in Delphi 11
Kas Ob. replied to sp0987's topic in General Help
Right as (2) Because on Win64 the default calling convention is the same for Windows OS API and Delphi 64bit, stdcall does nothing, the stack is not used for the first 4 parameters. -
Changes in System.sysutils.pas were not reflecting in other unit in Delphi 11
Kas Ob. replied to sp0987's topic in General Help
Well, this is sort of bugs are the worst because in some context they stay hidden for long time even for ever, and then one change in the code (a correct, innocent and valid) in different place might trigger this one to show its face wasting hours of bug hunting in the wrong places. Take an example of this code {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils; type TDoSomethingProc = procedure(var Value: TDateTime); stdcall; procedure DoSomething(var Value: TDateTime); stdcall; begin Value := 1; end; procedure DoSomething2(var Value: TDateTime); begin Value := 2; end; procedure Test; var dt: TDateTime; Proc1,Proc2:TDoSomethingProc; begin Proc1 := @DoSomething; Proc2 := @DoSomething2; Proc1(dt); Proc2(dt); end; begin Test; Readln; end. As you said it is wrong and should not work, yet this code on my Delphi XE8 does work fine with no problem. The stack is clearly broken in Test() right after Proc2(dt) and in theory returning to main form Test() should be broken, yet it does return like nothing happen ! The secret is Stack Framing and this is one of the power of not depending on the stack in the first place but on a pointer to it with minimum or no push/pop at all ( unless it is needed), to get the big picture we need to look at the assembly for this code and the stack been fixed right in place. So in this case no harm detected and that because proc2 does need the parameter in eax and in fact it is there, because the compiler used eax on push. When this can go wrong and break ? we i can think of at least two (other of course do exist) 1) If Test() has more local variable or just more code, and the compiler did use different register other than eax to load the pointer to the variable dt and push it on the stack. 2) Any code after Proc2() will have the stack (the real stack on esp) broken, so calling functions after it and their func/proc called from there might raise unexplained exceptions or show undefined behavior or logic. ps: this use of ebp as stack storage is not exclusive to Delphi neither its invention, but in Delphi we don't have the extreme optimization setting like in Visual Studio when remove this protection to gain speed, hence exposing such bugs right away. -
Changes in System.sysutils.pas were not reflecting in other unit in Delphi 11
Kas Ob. replied to sp0987's topic in General Help
Not sure about this, but the following works fine var windows_GetLocalTime: procedure(var st: TSystemTime); stdcall; procedure getTbtTime(var st: TSystemTime); stdcall; var dt: TDateTime; begin dt := strtodatetime('12/06/2025'); DateTimeToSystemTime(dt, st); //DecodeDateTime(dt, st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, // st.wSecond, st.wMilliseconds); end; procedure TForm10.Button1Click(Sender: TObject); var dt, value: TDateTime; s: string; begin dt := Now; Memo1.Lines.Add('before hook:- ' + datetimetostr(dt)); windows_GetLocalTime := InterceptCreate(@GetLocalTime, @getTbtTime); // if it is only for local (here and now) then we need to clean it up locally try dt := Now; // Returns error "Invalid date to Encodedate" s := FormatDateTime('mm/dd/yyyy : hh:nn:ss.zzz - ', dt); Memo1.Lines.Add(s); finally InterceptRemove(@windows_GetLocalTime); // cleanup the hook and restore end; end; procedure TForm10.FormCreate(Sender: TObject); begin windows_GetLocalTime := GetLocalTime; end; See that stdcall modifier for getTbtTime, it is essential here when intercepting Windows API, without it all the parameters are wrong. -
Hi @Uwe Raabe , I saw this bad behavior from MMX code explorer for years now, yet it didn't annoy me that much as i do restart my IDE frequently, but today was with a client sharing his desktop with me, while i am helping him to track a bug in his project on his PC and on his client PC who was also sharing his desktop, so the process went for some times and among few tools being used one reported something that caught my eye, so at the end of this semi-teaching session i tried to confirm that behavior (the one i am familiar with) on his Delphi 10.3. Now to the details, my IDE is XE8 and i have MMCE v13.1.0 build 2220 while his IDE was 10.3 and i forgot to write his MMCE version and build but it was v15 for sure, he said it was the latest, but after visiting https://www.mmx-delphi.de/ i can't know what is what and where is the latest version or if there is beta and stable.... Anyway, to the problem at hand : any already opened then closed file using the IDE MMX will keep tracking and monitoring its datetime, and here i mean it will do it only if closed after opening, this accumulate with what looks like no limit, i opened 50 files and opened and closed many different projects and the IDE with nothing opened keep checking these file datetime (namely FileAge) exactly every 1 minute, and on top of that the same query for FileAge happen every time the IDE was activated (like restored or even had focus after losing it) Again the following on my XE8 and the deprecated v13, yet i saw the same behavior on v15 and newer version but i couldn't ask my client to waste time for something might not concern him. This capture is the simplest using Process Monitor and one opened and closed file after activating the IDE and this does show the 1 minute timer triggered FileAge These are with one file opened and closed, also i can't make much sense from the call stack !!! I mean triggered by DoActivate makes sense, yet these are closed files and should not be monitored, because they are monitored, here an example of that file being renamed then opened and closed then renamed back About the stack and make sense, this sequence AddClipboardFormatListener -> TDataModule.WriteHeight ???!!!!!!!(WTF) -> MMCE then MMCE even with non existed file keep trying to query its information, though there is a hook and the timed query should not be needed to be begin with, ( not with a timer and not on IDE activation) Something wrong here and there is no clipboard what so every on my PC now, and to be honest i not sure if the stack is correct at this point. Anyway, hope i am not wasting your time and please forgive me if i am missing something obvious like some settings for such caching and keep it even after closing, as i couldn't find settings to control this behavior . PS : today i had blackout twice and every time lost around half an hour of debugging session deep into the IDE and MMCE, so i gave up and decide to report it, between these files query, there is a loop and to walk the files but one forward and the other downward (may be while-do vs for-to) the code responsible for FileAge in knownache or chached ... etc i really can't remember, and doubt it is useful, as going after FileAge should be more than enough. PS2: I am not expecting any update on v13 also don't have the right to ask for, but it might help v15 users if the mentioned here problem is in fact a problem and still exist in the latest version. PS3: i successfully solved or removed the my problem on v13 by hooking, though not sure what did break if any, no signs of MMCE failure with few restarts and few fast tests. PS4: there is difference in the call stack for these closed files may be about 30 seconds then it will revert to different stack call (deeper/longer one) Hope that is not waste of time.