Jump to content

aehimself

Members
  • Content Count

    1053
  • Joined

  • Last visited

  • Days Won

    23

Everything posted by aehimself

  1. aehimself

    TDBImage descendant

    If any future visitor is wondering, a simple Picture.Assign(DataLink.Field); solved the problem, there was no need for a middle TJPEGImage. It's still a mystery for me though, why it is not working with a TJPEGImage and working without. Especially since they were all loaded correctly. Especially since the above works fine with a normal TImage, but not from inside a TDBImage descendant.
  2. aehimself

    TDBImage descendant

    I know. This is why I am attempting to get rid of it. Well, it is working if we are leaking TJPEGImages, which I find really strange. Also attempting to Self.Invalidate / Self.Repaint / Self.Refresh after loading the picture makes no difference.
  3. aehimself

    TDBImage descendant

    Good idea. Commented the .Free out: jpeg := TJPEGImage.Create; Try jpeg.Assign(DataLink.Field); Picture.Assign(jpeg); Finally // jpeg.Free; End; Bad thing is, it still does not work (picture isn't shown) and it leaks a TBitmapImage, a TJPEGImage, a TBitmap, a TJPEGData and a TMemoryStream object upon destruction 😞
  4. aehimself

    TDBImage descendant

    Yep. As I'm .nil-ing that, it'd throw an AV. The loading has been re-written, and is now: Picture.Assign(jpeg);
  5. Procedure TAEMultiSQLMainForm.ConnectionClick(inSender: TObject); Var ts: TTabSheet; sqf: TSQLConnectionFrame; s: String; Begin s := (inSender As TMenuItem).Caption; ts := TTabSheet.Create(Display); ts.Caption := s; ts.ImageIndex := 1; ts.PageControl := Display; sqf := TSQLConnectionFrame.Create(ts, Settings.Connections[s], Log); sqf.Parent := ts; RecolorSQLFrame(ts); Display.ActivePage := ts; ChangeFocus; sqf.ReconnectButton.Click; End; This is the code snipplet I use in a tabbed SQL browser to open a new connection; which is also a frame. I am creating a tabsheet and a frame without a name and never got an error concerning component names. That's the only reason I asked.
  6. aehimself

    Function with just underscore _() seems to be valid

    Came just to say it reminds me of BrainF*ck. You were faster though 🙂
  7. Based on your luck, you can leak 10 TListBoxItem obects with one run. Don't do this. Just because it did not fail during debugging, it might fail in real life. Random(9900) will give you a number between 0 and 9899, leaving the last one (#9900) unused 100% of the times. Consider using Random(High(messageList) + 1) instead of a burned-in number; so if you decide to resize your array later on - you only have to do it at one place. If you really like With that much, you can just say With TFrame1.Create(Self) Do Begin Name := IntToStr(randNumb); [...] End; Burning CPU cycles for FindComponent is completely useless, if you just created that object. I'd use a local variable here, but that's only my taste. As for the rest of your fears - they are all valid. I guess @David Heffernan has an alert set up if you write SetLength(myArray, Length(myArray) + 1) anywhere, as he will appear and tell you that it is a REALLY bad practice. Indexing will also never automatically shrink, items will not be moved - you have to do it manually if you want to... ... oooor instead of an array you can say messageList: TObjectList<TListBoxItem>; [...] messageList := TObjectList<TListBoxItem>.Create(True); ...this way you will get an automatically expanding and shrinking list, which will automatically .Free your object if you delete it from the list. With this, the above With block could be replaced by... Var index: Integer; Begin index := messageList.Add(TListBoxItem.Create(Self)); messageList[a].Name := xxx; [...] which looks cleaner imo. P.s.: Is there a purpose you are naming dynamically created objects? I never ever had to do it so far; thus can not imagine a reason why I'd need to do so.
  8. aehimself

    Notification custom look

    In this case I keep my mouth shut - this is how we all experiment and learn at the end. I still do dislike custom notifications, though 🙂 Custom, as in notifications at all; or custom-themed notifications? People using computers nowadays are lazy. We don't want to flip a switch if we expect the program to know to keep quiet. Furthermore, if I already set "Quiet hours in Cortana".... You can add a button like "Don't show notifications when [Insert currently active window title] is active". At least you have to click once, then never again. I'm cursed to write my own - even - experimenting apps with my own comfort in sight. If I cannot do it with a feature I want to see in a finished product, it's a no-go for me. P.s.: yes, I have hundreds of unfinished test projects 😞
  9. aehimself

    Delphi Closedown Error

    So. Damn. True. I'll always smile on the EULA of Win2k, which clearly stated that it's not an error-prone operating system and therefore it's not recommended for appliances such as in airplanes or rocket guidance. But - as always - you'll always find gems. Once one of our customer called us and said that they "found" a running server under a desk in a closed-down section of the building. If my memory is correct, it had Windows 2003 installed and when we checked it it was up for 3,5 years straight. Easy without any real tasks, though.
  10. aehimself

    Notification custom look

    I am against custom notifications. I disliked MSN messenger, early Outlook (2003 era, maybe; can't recall) now Viber and all the others doing this as it completely breaks a computers look-and-feel. Especially since later OSes have their native notification system. I half-implemented this once, but soon I realized that it's not that easy. A notification only worth it if it's above everything else on your screen - except some situations: you'll have to check for full-screen apps (gaming, video); possibly add DnD intervals and maybe allow the user that if a specific application is running - don't show anything. What happens if your notification covers the native notification or vice versa...? Would be nice to have some syncing to make sure it never happens. That's a lot of extra work, and the OS already knows it all.
  11. A VPN is overkill in my opinion; a simple port forwarding is sufficient. Even if you have dynamic IP addresses there are free / cheap solutions to tie that to a domain name (just google for free dynamic dns). But as @Tom Chamberlain mentioned, it is a REALLY bad idea to directly expose a database to the Internet, so definitely do use a REST-like server in between.
  12. aehimself

    Issue with variant in record in array

    Do you get the same leak report if you don't debug the code, just let it to run? It should not be the case, but try to call VarClear on the variant before exiting, and finalizing the array.
  13. I'm not saying there's something wrong with this type of helpers (I have close to the same at work) it's just an "inconvenient" - or should I say unusual - format to follow. And - as @Attila Kovacs mentioned and your example shows - you have to be careful; as there's no way (according to my knowledge) in Delphi to see if a TObject is initialized or not.
  14. aehimself

    Need to convert varbinary to hex string in a dll

    The first and the third example starts with 0x30 so bintoStr will not be called, but the byte array seems to be binary; not ASCII. If you are sure that the field always contains binary data, drop BinToAscii and call BinToStr only.
  15. aehimself

    Need to convert varbinary to hex string in a dll

    BinToHex should work, but you can give this a try: Function MyBinToHex(inBinary: TArray<Byte>): String; Var b: Byte; Begin Result := '0x'; For b In inBinary Do Result := Result + IntToHex(Ord(b), 2); End; ..,in older Delphi's: Function MyBinToHex(inBinary: Array Of Byte): String; Var a: Integer; Begin Result := '0x'; For a := Low(inBinary) To High(inBinary) Do Result := Result + IntToHex(Ord(b), 2); End;
  16. aehimself

    Delphi 10.3 Rio on Windows Server 2008 R2

    Talking about a virtual environment, especially Xen; I'd give the how-to written by @Dalija Prasnikar a shot: https://dalijap.blogspot.com/2019/05/disable-delphi-rio-ide-theme.html There were multiple reported painting issues solved by disabling theming of the IDE.
  17. aehimself

    License key system

    I am strictly talking about security. By not a valid license I don't mean an expired; a crafted one which is known to be not from the author. As for code execution, it's not that easy. Of course if you are corrupting with (or you did not initialize your buffer, and it contains) the exact binary representation of a call to DeleteFile - it will work. procedure TForm1.Button1Click(Sender: TObject); Type TProcedure = Procedure; PProcedure = ^TProcedure; Var p: PProcedure; buf: Pointer; begin GetMem(buf, 1024); Try p := buf; p^; Finally FreeMem(buf); End; end; Project Project1.exe raised exception class $C0000005 with message 'access violation at 0x00000000: read of address 0x00000000'. Execution denied. Same, if you try to execute a differently allocated memory area: procedure TForm1.Button2Click(Sender: TObject); Type TProcedure = Procedure; PProcedure = ^TProcedure; Var p: PProcedure; x: TObject; begin p := Addr(x); p^; end; Project Project1.exe raised exception class $C0000005 with message 'access violation at 0x02f788b0: write of address 0x060904ec'. No luck. Out-of-bounds? procedure TForm1.Button3Click(Sender: TObject); Type TProcedure = Procedure; PProcedure = ^TProcedure; Var p: PProcedure; begin p := Pointer($ABABABAB); p^; end; Project Project1.exe raised exception class $C0000005 with message 'access violation at 0x005fd31b: read of address 0xabababab'. The OS is attempting to take measures against this, and if it's possible (somewhat how iPhone / PS4 jailbreaks used to work until they patched their browsers) - you found an exploit. With not invasive memory corruption you'll turn some output Chinese, or crash the application at a point. Do it carelessly, and you can face charges.
  18. For a long time I am using a custom settings class in most of my applications to store it's data. They all have a .AsJSON property to export / import everything, which is automatically called by .Load and .Save. It's working perfectly. The only drawback is that I had to write the getter and setter methods for each instance, even if it's only a sub-class of the main settings. And it was getting tiring. I started to experiment with RTTI to make the class detect it's own properties in the getter and setter so I finally can forget about these in general situations. Exporting works like a charm; I only could not properly handle TLists but I wrote a custom list wrapper for the rescue. The importing (loading from JSON) part caused some headache, which I can't seem to solve. One part is left, if the property / field is an array type. The inner helper method looks like this: Function TApplicationSettings.JSONToValue(inJSON: TJSONValue; inTypeInfo: PTypeInfo): TValue; Var enum: TJSONArray.TEnumerator; valarray: TArray<TValue>; a: Integer; Begin Result := TValue.Empty; Case inTypeInfo.Kind Of [...] tkArray, tkDynArray: Begin enum := TJSONArray(inJSON).GetEnumerator; SetLength(valarray, TJSONArray(inJSON).Count); If Assigned(enum) Then Try a := 0; While enum.MoveNext Do Begin valarray[a] := JSONToValue(enum.Current, inTypeInfo); Inc(a); End; Finally enum.Free; End; Result := TValue.FromArray(inTypeInfo, valarray); End; End; End; ..and the outer helper: Procedure TApplicationSettings.JSONToRttiMember(inJSON: TJSONValue; inRttiMember: TRttiMember); Var val: TValue; pi: PTypeInfo; ppi: PPropInfo; Begin If Not Assigned(inJSON) Then Exit; ppi := GetPropInfo(Self, inRttiMember.Name); If Not Assigned(ppi) Then Exit; pi := ppi^.PropType^; val := JSONToValue(inJSON, pi); If val.IsEmpty Then Exit; If inRttiMember Is TRttiProperty Then (inRttiMember As TRttiProperty).SetValue(Self, val) Else If inRttiMember Is TRttiField Then (inRttiMember As TRttiField).SetValue(Self, val); End; This most possibly where my mistake is, as GetPropInfo always returns nil. Before this solution I passed the (PropertyType / FieldType).TypeKind as a parameter to the inner helper, but in that case I won't have access to PTypeInfo, which is needed for the TValue.FromArray. So the question is; how can I extract the PTypeInfo of an RTTI field / property? Because the code above doesn't, that's for sure 🙂
  19. I saw handle in the list; I just quickly passed by it as it usually means something different... Thank you!
  20. And this is something I will never EVER argue about. For me it also makes the code harder to read, as I already got used to Create - Try - Finally - Free - End. I'll always be alarmed if this pattern is not followed. In my case unfortunately it's a bit different, as we have (I'd call them as) protocol descriptors in our custom dataset descendants, no direct SQL queries. But I hate setting the variables to nil before.
  21. If you mean you have to set your variable to nil before first calling this method there's no question about it. As for freeing, that should be handled in the same method, which calls this helper. Like... Var myquery: TFDQuery Begin myquery := nil; Try createMyQuery(myquery, 'SELECT * FROM USERS'); [...] Finally myquery.Free; End; End; What I mean is, whether if var is declared in the definition of createMyQuery or not; it will not leak and will function correctly, as "myquery" will be passed as a reference (the object itself) and not a copy of it. Edit: I'm an idiot. Instead of asking I could have made a test case to confirm. Will come back soon with my results. Edit-edit: Disregard everything. Var is needed. Procedure TForm1.SLCreateAdd(inSL: TStringList); Begin If Not Assigned(inSL) Then inSL := TStringList.Create; inSL.Add('Hello'); inSL.Add('World'); End; procedure TForm1.Button1Click(Sender: TObject); Var sl: TStringList; begin sl := nil; Try SLCreateAdd(sl); ShowMessage(sl.Count.ToString); Finally sl.Free; End; end; Causes a nullreference exception and leaks a TStringList object. Procedure TForm1.SLCreateAdd(Var inSL: TStringList); Begin If Not Assigned(inSL) Then inSL := TStringList.Create; inSL.Add('Hello'); inSL.Add('World'); End; does not. I am obviously wrong with my memories and most probably used Var in my helper 🙂
  22. You are absolutely right in this, I'm always declaring local variables though (and as it's my helper I'll not call it with nil) it seems to be irrelevant in my case. My real surprise is/was the leaking part. If my memory doesn't cheat and class instances are indeed passed as references to methods, there should not be any leaks in the above code, with var or not.
  23. aehimself

    License key system

    As network traffic is really easy to be misdirected, I am strongly against network-based authentication. As @Sherlock pointed out, they will simply fail to launch (or fall back to demo mode) in most of the real-world customer scenarios, where networks are controlled as they should be. Local license authentication is the way to go in my opinion, but there is no fool-proof way. Everything can be (and if it worth, will be) hacked no matter what. You only can make the job of the pirate harder with obfuscation, fake no-op assembly blocks, custom multi-level encryption, on-the-fly method assignments and so on. One thing for sure, delay checking the license and NEVER use something like If Not TMyLicense.IsValid Then Halt as on assembly level that's a modification of one JMP to bypass everything. I started to learn the proper use of pointers and if my license is not valid, I'm simply corrupting memory on purpose. It might (that's the beauty in it, it's not guaranteed) start to crash or malform data at the most random places / times. If you hide it well enough, even the hacker might think that it's a piece of junk and does not worth the effort...
  24. aehimself

    Where did I come from

    I am actively using this solution (usually with an atomic value) but it always felt hacky. Strange to see that it seems to be THE way. At least I did not write smelly code. At least in this part... 🙂
  25. I thought class instances are always passed as references (pointers to the actual instance)...? I also do have a similar method to create and initialize the dataset and I think I didn't use var there. I do have to doublecheck it; I don't have access to that piece of code right now.
×