Delphi 10.4 + msbuild = unstable binary - delphi

I just implemented a CI/CD environment with Delphi 10.4+gitlab+build machine
Everything worked apparently correct, with the build process running smoothly, but when using the program, it randomly show errors that never occurs compared if I build manually via "shift+F9" (build) inside Delphi itself.
The build process:
call "C:\\Program Files (x86)\\Embarcadero\\Studio\\21.0\\bin\\rsvars.bat"
msbuild /target:rebuild /property:config=Debug;DCC_BuildAllUnits=true /p:platform=Win32 project.dproj
Also, I tried other msbuild variations like:
msbuild /target:rebuild /property:config=Debug /p:platform=Win32 project.dproj
(and also with 2 steps (target:clean and target:build)
I also compared msbuild dcc32.exe command with "Delphi" dcc32.exe command, and they are basically equal but for an additional (inexistent) folder for msbuild output:
-I"c:\program files (x86)\embarcadero\studio\21.0\lib\Win32\release\EN";...
The weirdest thing is: after doing a msbuild build, if I open my project with Delphi, do a clean+compile the program became weirder, giving other errors, but if I do full build (shift+F9) then it runs smoothly again
To me, it looks like some of the components used aren't properly compiled under msbuild or are used with other parameters, but I have no idea of what could it be or how to find it.
Error I got after building with cmd msbuild + clean at Delphi + compile at Delphi:
First chance exception at $0019FB95. Exception class $C0000096 with message 'privileged instruction at 0x0019fb95'. Process project.exe (11324)
Followed by:
First chance exception at $0040AE84. Exception class $C0000005 with message 'access violation at 0x0040ae84: read of address 0x0000000b'. Process project.exe (11324)
Looking at the error, it is raised in this code:
procedure TAdvancedField.asImageLoadToBitMap(var outBitmap:TBitmap); var FS : TMemoryStream;
FirstBytes: AnsiString;
Graphic : TGraphic;
begin try
FS := TMemoryStream.Create;
(Self as TBlobField).SaveToStream(FS);
fs.Position := 0;
SetLength(FirstBytes, 8);
FS.Read(FirstBytes[1], 8);
//Graphic := nil; //I add this line after my debug, and commented to confirm
//if Copy(FirstBytes, 1, 2) = 'BM' then
//begin
// Graphic := TBitmap.Create;
//end else
//if FirstBytes = #137'PNG'#13#10#26#10 then
//begin
// Graphic := TPngImage.Create;
//end else
//if Copy(FirstBytes, 1, 3) = 'GIF' then
//begin
// Graphic := TGIFImage.Create;
//end else
//if Copy(FirstBytes, 1, 2) = #$FF#$D8 then
//begin
// Graphic := TJPEGImage.Create;
// TJPEGImage(Graphic).CompressionQuality := 100;
//end;
if Assigned(Graphic) then
begin
try
FS.Seek(0, soFromBeginning);
Graphic.LoadFromStream(FS); //The error raised HERE
outBitmap.Assign(Graphic);
except
end;
Graphic.Free;
end
else
begin
outBitmap.Assign(nil);
end;
finally
fs.Free;
end;
end;
As you can see, the Graphic variable isn't initialized anywhere. But with a clean Delphi build it runs perfectly with Assigned(Graphic) always returning false, but after msbuild it returns as true. If this is correct to other similar cases it'll lead to unexpected behavior all across the project.
Another situation:
I put a button at my main form with a single raise exception, + application exception handler + JCVStackTrace handler, and this is the captured stack:
[017B7DE5] pcnConversao.RegTribISSQNToStr (Line 1301, "pcnConversao.pas" + 1) + $104
[006B6973] Vcl.Controls.TWinControl.ArrangeControl + $147
[006BB36F] Vcl.Controls.TWinControl.DoKeyUp + $73
[005A4998] Vcl.StdCtrls.TCustomCheckBox.SetChecked + $0
[006BB4CF] Vcl.Controls.TraverseControls + $37
[006BB36F] Vcl.Controls.TWinControl.DoKeyUp + $73
[0067E227] Vcl.Forms.TCustomForm.CreateWnd + $97
[006BA8BC] Vcl.Controls.TWinControl.WMInputLangChange + $8
[004F4A18] System.Classes.TStreamWriter.WriteLine + $C
[006BB47A] Vcl.Controls.TWinControl.WMChar + $2
[006BB36F] Vcl.Controls.TWinControl.DoKeyUp + $73
[005A4998] Vcl.StdCtrls.TCustomCheckBox.SetChecked + $0
[004F4A18] System.Classes.TStreamWriter.WriteLine + $C
By the way, pcnConversao is only used indirectly by my project
The correct would be:
[01709661] UModulo.TModulo.BitBtn3Click (Line 568, "UModulo.pas" + 0) + $11
[006BB3E7] Vcl.Controls.TWinControl.WndProc + $693
[005A4A10] Vcl.StdCtrls.TButtonControl.WndProc + $6C
[006BB547] Vcl.Controls.DoControlMsg + $23
[006BB3E7] Vcl.Controls.TWinControl.WndProc + $693
[0067E29F] Vcl.Forms.TCustomForm.WndProc + $6DB
[006BA934] Vcl.Controls.TWinControl.MainWndProc + $2C
[004F4A90] System.Classes.StdWndProc + $14
[006BB4F2] Vcl.Controls.TWinControl.DefaultHandler + $E6
[006BB3E7] Vcl.Controls.TWinControl.WndProc + $693
[005A4A10] Vcl.StdCtrls.TButtonControl.WndProc + $6C
[004F4A90] System.Classes.StdWndProc + $14
Note:
Looks like the different stack is directly related with Stack Frames checked at Delphi Compiler->Compiling->Code Generation, still no related with the diverging way of the compiler at not initialized variable

The problem was exactly that uninitialized variable. Still don't know why the behavior was different from the default build trough the IDE, but initializing as nil solved the problem

Related

Delphi seems to ignore some of my lines of code?

I have a VCL Delphi application I am working on, but Delphi seems to refuse to execute some of my lines of code, DrawLine is a private function...
if not(FirstCoords) then
begin
firstcoords := true;
xCo1 := xCoPre + LeftOffset;
yCo1 := yCoPre + TopOffset;
end
else
begin
xCo2 := xCoPre + LeftOffset;
yCo2 := yCoPre + TopOffset;
DrawLine(xCo1, xCo2, yCo1, yCo2);
bbtFieldScale.Click;
end;
when I step through debugging, it executes the If, then proceeds to set "firstcoords" to true, but then just jumps to the end of If, without even touching the other two lines... If I add a line such as the code below, then it seems to execute the code...
if not(FirstCoords) then
begin
firstcoords := true;
xCo1 := xCoPre + LeftOffset;
yCo1 := yCoPre + TopOffset;
showmessage(inttostr(xCo1+yCo1));
end
else
begin
xCo2 := xCoPre + LeftOffset;
yCo2 := yCoPre + TopOffset;
DrawLine(xCo1, xCo2, yCo1, yCo2);
bbtFieldScale.Click;
end;
Please help, I would really appreciate it :)
I have disabled optimization, but it still seems to refuse...
What you describe happens when the operations in question are optimized away because the variables being assigned to are not being used anywhere else in your code, and the compiler sees no noticeable side effects in eliminating those operations.
As soon as you added the ShowMessage(), the variables in question became relevant, so their assignments could not be eliminated anymore.

Clientdataset Append/Post fails intermittently

I have an external message coming in every second.
The message payload is saved in a ClientDataSet and displayed in a dbGrid.
No data base is involved. RAM storage only.
This works fine,
BUT,
I have intermittent problems when the dataset is empty and populated the first time.
The code is as follows:
procedure TCtlCfg_DM_WarningsFaults_frm.DecodeRxFrame(Protocol: TProtocolSelection;
// PROVA UTAN VAR VAR Frame : CAN_Driver.TCAN_Frame);
Frame : CAN_Driver.TCAN_Frame);
var
OldRecNo : integer;
// OldIxname : string;
// bMark : TBookMark;
WasFiltered : Boolean;
IdBitFields : TCanId_IdBitFields;
Msg : TCan_Msg;
MsgType : integer;
GlobalNode : TCanId_GlobalNode;
LocalNode : TCanId_LocalNode;
SubNode : TCanId_SubNode;
EntryType : integer;
SubSystemType : integer;
SubSystemDeviceId : integer;
IsActive : Boolean;
IsAcked : Boolean;
begin
with cdsWarningsFaults do
begin
if not Active then Exit;
Msg := Frame.Msg;
IdBitFields := DecodeCanId(Protocol, Frame.ID);
if IdBitFields.SubNode <> cSubNode_Self then Exit; // Ignore non controller/slave messages
if IdBitFields.AddressMode <> cCanId_AddrMode_CA then Exit;
MsgType := IdBitFields.MessageType;
if MsgType <> cMsg_CTL_CA_Broadcast_WarningAndFaultList then Exit;
if Frame.MsgLength < 5 then Exit;
GlobalNode := IdBitFields.GlobalNode;
LocalNode := IdBitFields.LocalNode;
SubNode := IdBitFields.SubNode;
// Silent exit if wrong node
if GlobalNode <> fNodeFilter.GlobalNode then Exit;
if LocalNode <> fNodeFilter.LocalNode then Exit;
if SubNode <> fNodeFilter.SubNode then Exit;
EntryType := Msg[1];
SubSystemType := Msg[2];
IsActive := (Msg[3] = 1);
SubSystemDeviceId := Msg[4];
IsAcked := (Msg[8] = 1);
DisableControls; // 2007-12-03/AJ Flytta inte scrollbars under uppdatering
OldRecNo := RecNo;
// OldIxName := IndexName; // Save current index
// IndexName := IndexDefs.Items[0].Name;
WasFiltered := Filtered; // Save filter status
Filtered := False;
try
try
if Findkey([GlobalNode, LocalNode, SubNode, EntryType, SubSystemType, SubSystemDeviceId]) then
begin // Update record
Edit;
FieldByName('fIsActive').AsBoolean := IsActive;
FieldByName('fIsAcked').AsBoolean := IsAcked;
FieldByName('fTimeout').AsDateTime := GetDatabaseTimeoutAt;
Post;
MainForm.AddToActivityLog('CtlCfg_DM_WF: DecodeRxFrame: Efter Edit. N=' + IntToStr(GlobalNode) + ' ' +
IntToStr(LocalNode) + ' ' +
IntToStr(SubNode) +
' RecCnt=' + IntToStr(RecordCount) + ' ET=' + IntToStr(EntryType) + ' SST=' + IntToStr(subSystemType) + ' SSD=' + IntToStr(SubSystemDeviceId), False);
end
else
begin // Create new record
Append;
MainForm.AddToActivityLog('CtlCfg_DM_WF: DecodeRxFrame: Efter Append. N=' + IntToStr(GlobalNode) + ' ' +
IntToStr(LocalNode) + ' ' +
IntToStr(SubNode) +
' RecCnt=' + IntToStr(RecordCount) + ' ET=' + IntToStr(EntryType) + ' SST=' + IntToStr(subSystemType) + ' SSD=' + IntToStr(SubSystemDeviceId), False);
try
FieldByName('fGlobalNode').AsInteger := GlobalNode;
FieldByName('fLocalNode').AsInteger := LocalNode;
FieldByName('fSubNode').AsInteger := SubNode;
FieldByName('fEntryType').AsInteger := EntryType;
FieldByName('fSubSystemType').AsInteger := SubSystemType;
FieldByName('fSubSystemDeviceId').AsInteger := SubSystemDeviceId;
FieldByName('fIsActive').AsBoolean := IsActive;
FieldByName('fIsAcked').AsBoolean := IsAcked;
FieldByName('fTimeout').AsDateTime := GetDatabaseTimeoutAt;
finally
try
Post; // VArför biter inte denna post så att det blir edit nästa gång
except
MainForm.AddToActivityLog('CtlCfg_DM_WF: DecodeRxFrame: Exception efter Post.', True);
end;
MainForm.AddToActivityLog('CtlCfg_DM_WF: DecodeRxFrame: Efter Post. N=' + IntToStr(GlobalNode) + ' ' +
IntToStr(LocalNode) + ' ' +
IntToStr(SubNode) +
' RecCnt=' + IntToStr(RecordCount) + ' ET=' + IntToStr(EntryType) + ' SST=' + IntToStr(subSystemType) + ' SSD=' + IntToStr(SubSystemDeviceId), False);
end;
end;
except
on E: Exception do
begin
MainForm.AddToActivityLog('Post exception message: [' + E.Message + ']', False);
MainForm.AddToActivityLog('Post exception class: [' + E.ClassName + ']', False);
MainForm.AddToActivityLog('Post exception Error code: [' + IntToStr(EDBCLIENT (E).ErrorCode) + ']', False);
MainForm.AddToActivityLog('Post exception ReadOnly is: [' + BoolToStr(ReadOnly) + ']', False);
MainForm.AddToActivityLog('Post exception CanModify is: [' + BoolToStr(CanModify) + ']', False);
MainForm.AddToActivityLog('DecodeRxFrame: Exception inside FindKey block', False);
Cancel;
end;
end;
finally
// IndexName := OldIxName; // Restore previous index
Filtered := WasFiltered; // Restore filter state
if (OldRecNo >= 1) and (OldRecNo <= RecordCount) then RecNo := OldRecNo;
EnableControls;
end;
end;
//MainForm.AddToActivityLog('DecodeRxFrame: Exit ur proceduren', False);
end;
The problem is when the record does not already exist,
and I need to Append a new record.
It often works fine, but many times it seems the POST does not work,
and the append is repeated a few or many times when new data comes in.
Suddenly the append works, and subbsequent updates are done using edit,
and as far as I can tell, after that it then works forever.
The issue is intermittent and the number of tries needed to succeed vary.
It feels like a timing issue, but I cannot figure it out.
Any ideas greatly appreciated.
Thanks,
Anders J
As mentioned in my comment a lot can be figured out about how the code flows using an extract of the logs. (Also as a side-note, sometimes you need to be careful of the reliability of your logging system. Your logging is at least partially home-brew, so I have no idea what it means when you arbitrarily pass True/False values to the AddToActivityLog method.)
However, I am still able to offer some guidance to help you identify your problem. I also have some general comments to help you improve your code.
You're not shy to use logging to narrow down your problem: this is a good thing!
However you technique could use a little improvement. You're trying to determine what's going wrong around the Post method. Given such a clear goal, your logging seems surprisingly haphazard.
You should do the following:
//Log the line before Post is called. It confirms when it is called.
//Log important state information to check you're ready to post "correctly"
//In this it's especially useful to know the Sate of the dataset (dsEdit/dsInsert).
Post;
//Log the line after Post to confirm it completed.
//Note that "completed" and "succeeded" aren't always the same, so...
//Again, you want to log important state information (in this case dsBrowse).
If you had this this logging, you might (for example) be able to tell us that:
Before calling Post dataset is in dsInsert state.
And (assuming no exceptions as you say): after calling Post the dataset is still in dsInsert state.
NOTE: If it were in dsBrowse but Post still considered "unsuccessful", you'd be told to log details of the record before and after Post.
So now: Post "completing" without the record being "posted" would give a few more things to look at:
What events are hooked to the data set? Especially consider events used for validation.
Since you're working with TClientDataSet there's an important event you'll want to take a look at OnPostError. DBClient uses this callback mechanism to notify the client of errors while posting.
If you log OnPostError I'm sure you'll get a better idea of the problem.
Finally I mentioned you have a lot of other problems with your code; especially the error handling.
Don't use with until you know how to use it correctly. When you know how to use it correctly, you'll also know there's never a good reason to use it. As it stands, your code is effectively 2 characters short of a subtle bug that could have been a nightmare to even realise it even existed; but would be a complete non-issue without with. (You declared and used a property called IsActive differing by only 2 characters from TDataSet's Active. I'm sure you didn't realise this; and their difference is but an accident. However, if they had been the same, with would very quietly use the wrong one.)
You need to write smaller methods - MUCH smaller! Blobs of code like you have are a nightmare to debug and are excellent at hiding bugs.
Your exception handling is fundamentally wrong:
Your comment about logging and exception handling suggests that you've been simply adding what you can out of desperation. I think it pays to understand what's going on to keep your logging useful and avoid the clutter. Let's take a close look at the most problematic portion.
/_ try
/_ FieldByName('fGlobalNode').AsInteger := GlobalNode;
/_E FieldByName('fLocalNode').AsInteger := LocalNode;
| FieldByName('fSubNode').AsInteger := SubNode;
| FieldByName('fEntryType').AsInteger := EntryType;
| FieldByName('fSubSystemType').AsInteger := SubSystemType;
| FieldByName('fSubSystemDeviceId').AsInteger := SubSystemDeviceId;
| FieldByName('fIsActive').AsBoolean := IsActive;
| FieldByName('fIsAcked').AsBoolean := IsAcked;
| FieldByName('fTimeout').AsDateTime := GetDatabaseTimeoutAt;
|_ finally
/_ try
/_ Post;
/ except
| MainForm.AddToActivityLog(..., True);
| end;
|_ MainForm.AddToActivityLog(..., False);
/ end;
|
...
So, in the above code:
If no exceptions happen, you'd simply step from one line to the next.
But as soon as an exception happens, you jump to the next finally/except block.
The first problem is: Why would you try to force a Post if you haven't finished setting your field values. It's a recipe for headaches when you end up with records that have only a fraction of the data they should - unless you're lucky and Post fails because critical data is missing.
When finally finishes during an exception, code immediately jumps to the next finally/except in the call-stack.
Except is slightly different, it only gets called if something did go wrong. (Whereas finally guarantees it will be called with/without an exception.
TIPS: (good for 99% of exception handling cases.
Only use try finally for cleanup that must happen in both success and error cases.
Nest your try finally blocks: The pattern is <Get Resource>try .... finally<Cleanup>end (The only place to do another resource protection is inside the .....)
Avoid except in most cases.
The main exception to the previous rule is when cleanup is needed only in the case of an error. In which case: do the cleanup and re-raise the exception.
Other than that: only implement an except block without re-raising if you can fully resolve an error condition. (Meaning the lines of code immediately after the exception swallower truly don't care about the previous exception.

Delphi multiple .ini files operation

Recently, I encountered a little issue, which causes an access violation when trying to read .ini files.
My question is, can I load multiple .ini files (e.g. settings.ini and data.ini) within one and the same procedure? For instance, I have two ini files, which I write in and read from.
Here is a shortened proc, which writes data:
//writing to file uninstall.ini
try
ini := TIniFile.Create(edPath.Text + '\Uninstall.ini');
ini.WriteString('Uninstall', 'qfProgramName', Label4.Caption);
ini.WriteString('Uninstall', 'qfUninstPath', edPath.Text);
finally
ini.Free;
end;
And then, there is this this code (in the same procedure)
configini := configini.Create(ExtractFilePath(Application.ExeName) + '\quickfix.ini');
sectionsCount := getMaxSectionIndex(ExtractFilePath(Application.ExeName) + '\quickfix.ini');
startmenuLoc := GetProperDir(_STARTMENU);
desktopLoc := GetProperDir(_DESKTOP);
for I := 1 to sectionsCount do begin
currentSection := 'qfShortcut_' + IntToStr(I);
shortcutFile := configini.ReadString(currentSection, 'qfShTarget', '');
shortcutDesc := configini.ReadString(currentSection, 'qfShDescription', '');
shortcutFullPath := installPath + '\' + shortcutFile;
shortcutDest := configini.ReadString(currentSection, 'qfShPath', '');
displayName := configini.ReadString(currentSection, 'qfDisplayName', '');
showmessage(startmenuLoc + '\' + displayName + '.lnk');
showmessage(shortcutFullPath);
if shortcutDest = 'spStartMenu' then CreateLink(shortcutFullPath, startmenuLoc + '\' + displayName + '.lnk', '', '');
if shortcutDest = 'spDesktop' then CreateLink(shortcutFullPath, desktopLoc + '\' + displayName + '.lnk', '', '');
The line with the second loaded file crashes with the access violation. It's not excluded, that the trouble comes from some other location, however, I would like you guys to take a look, maybe someone will see another problem.
This snippet is to create shortcuts on the desktop and in start menu, loading data from .ini files. File "quickfix.ini" is also loaded once at OnCreate, but freed then.
Try replacing
configini := configini.Create(ExtractFilePath(Application.ExeName) + '\quickfix.ini');
with:
configini := TIniFile.Create(ExtractFilePath(Application.ExeName) + '\quickfix.ini');
You are trying to call Create on an uninitialized variable rather than on a class.

7z in Delphi 2007

I'm trying to zip some files using Delphi 2007 using the JEDI JCL. The problem is I can't figure out why I keep getting this error "Sevenzip: Failed to load 7z.dll"
My code is :
var
archiveclass: TJclDecompressArchiveClass;
archive: TJclDecompressArchive;
item: TJclCompressionItem;
s: String;
i: Integer;
begin
archiveclass := GetArchiveFormats.FindDecompressFormat(dir);
if not Assigned(archiveclass) then
raise Exception.Create('Could not determine the Format of ' + dir);
archive := archiveclass.Create(dir);
try
if not (archive is TJclSevenZipDecompressArchive) then
raise Exception.Create('This format is not handled by 7z.dll');
archive.ListFiles;
s := Format('test.zip Item Count: %d'#13#10#13#10, [archive.ItemCount]);
for i := 0 to archive.ItemCount - 1 do
begin
item := archive.Items[i];
case item.Kind of
ikFile:
s := s + IntToStr(i+1) + ': ' + item.PackedName + #13#10;
ikDirectory:
s := s + IntToStr(i+1) + ': ' + item.PackedName + '\'#13#10;//'
end;
end;
if archive.ItemCount > 0 then
begin
// archive.Items[0].Selected := true;
// archive.ExtractSelected('F:\temp\test');
archive.ExtractAll('F:\temp\test');
end;
ShowMessage(s);
finally
archive.Free;
end;
I have the 7z.dll in the same folder as the Delphi project. What Am I doing wrong? Also is there any other simple way to 7z a folder? I'm not looking for some complex tasks, just to create a zip from a folder.
The JCLCompression unit only wraps the 7z API inside JCLCompression classes. The 7z API itself resides in the SevenZip.pas unit (in the windows folder of the JCL source). This is where the 7z.dll is loaded (by the Load7Zip routine, when required).
You appear to be compiling the project with dynamic linking to that DLL, resulting in the DLL only being loaded when needed, rather than being loaded and linked with your EXE. The fact that the loading is failing and the error message you are seeing in the exception indicates some problem with finding or loading that DLL at runtime.
Things to check:
Ensure that the 7z.dll is in the same folder as your EXE (not the DPR source file, but the EXE at runtime)
Ensure that the 7z.dll you are using is 32-bit. Delphi 2007 produces 32-bit executables only, so even on a 64-bit OS you will still need the 32-bit version of 7z.dll for your Delphi application.

I am currently writing a stock control system and getting a 105 error

I am writing a stock control system and when coding the last thing, I got a 105 error. I was just wondering if anyone had any ideas of what is wrong.
closefile(ContractOrdersfile);
closefile(ContractFile);
assignfile(ContractPriceListFile, 'Contract Price List.txt');
rewrite(ContractPriceListFile);
reset(ContractFile);
while not eof(ContractFile) do
begin
read(ContractFile, Contract);
if Contract.Flag = true then
begin
Writeln(Contract.Contract_Name + ' ' + inttostr(Contract.Contract_ID) + ' ' + CurrtoStr(Contract.Search_Price));
Contract.Search_Price := StrtoCurr('0.00');
Seek(ContractFile, (Filepos(ContractFile)-1));
write(ContractFile, Contract);
end;
end;
ShellExecute(Handle, 'open', PChar('notepad'), PChar('Contract Price List.txt'), nil, SW_SHOW);
closefile(ContractFile);
closefile(ContractPriceListFile);enter code here
I/O Error 105 is "File not open for output". It's usually caused by failing to set the appropriate FileMode after opening the file, or by using Write or WriteLn from a non-console application.
It's most likely caused by the WriteLn inside the if block that writes contract info. It appears to be going to the console (no file is specified as the first argument). You should probably provide a file as the first argument, or make sure you're running a console application.

Resources