Forum > Windows
Access Violation on Procedure Exit
therealhades:
Hello everyone,
i have the following problem and hope that somebody has an idea what i'm doing wrong.
I defined a record:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TStringVar = record VarName: String; Handle: LongWord; Data: String;end;
and a procedure to read a String Value from a PLC:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TfrmMain.ReadStringVar(var aVar: TStringVar);var tmp: array of char; ads: longint;begin if aVar.Handle = 0 then ads:= AdsSyncReadWriteReq(@AMS, ADSIGRP_SYM_HNDBYNAME, $0000, sizeof(aVar.Handle), @aVar.Handle, Length(aVar.VarName) + 1, @aVar.VarName[1]) else ads:= 0; SetLength(tmp, 255); if ads = 0 then ads:= AdsSyncReadReq(@AMS, ADSIGRP_SYM_VALBYHND, aVar.Handle, Length(tmp), @tmp[1]); if ads = 0 then aVar.Data:= PlcStringToString(tmp); ShowMessage('in Procedure: ' + aVar.Data);end;
I call this procedure in another procedure (btn[0] is of type TStringVar):
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---ReadStringVar(btn[0]);ShowMessage('after procedure: ' + btn[0].Data);
Now i have the following Situation:
If i compile this code for Win32 or Win64 everything works fine. But if I compile this code for WinCE, only the ShowMessage inside the ReadStringVar procedure is called (with the correct Value from PLC) and then I got an Access Violation and the ShowMessage from the original procedure is not called.
Can anybody tell me what I'm doing wrong? As I said, this only happens with WinCE, on Win32 everything works like a charm.
Best regards
Bjoern
PS: Lazarus Version is 3.2, fpc is 3.2.2
Zvoni:
Line 11, last Parameter looks fishy to me.
shouldn't that be @tmp[0]?
tmp is dynamic array of char, and first char should be in Member 0
therealhades:
You're right, it was [1] because i tried with Ansistring first.
I changed it to
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---ads:= AdsSyncReadReq(@AMS, ADSIGRP_SYM_VALBYHND, aVar.Handle, Length(tmp), @tmp[0]); and got the same behavior (Access Vioalation after Procedure Exit)
cdbc:
Hi
Line 7: you're using 'Length(aVar.VarName) + 1' & 'aVar.VarName[1]', when you haven't allocated *any* memory to 'aVar.VarName' %)
Try to insert this line just before Line 7:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---SetLength(aVar.VarName,255); ...and maybe skip the +1 part in 'Length(aVar.VarName) + 1', I dunno...
Regards Benny
Zvoni:
--- Quote from: cdbc on May 29, 2024, 09:58:24 am ---Hi
Line 7: you're using 'Length(aVar.VarName) + 1' & 'aVar.VarName[1]', when you haven't allocated *any* memory to 'aVar.VarName' %)
Try to insert this line just before Line 7:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---SetLength(aVar.VarName,255); ...and maybe skip the +1 part in 'Length(aVar.VarName) + 1', I dunno...
Regards Benny
--- End quote ---
VarName is a string, so self-managed.
But you might be right.
Accessing external hardware is usually done with Pointer to chars, which need to be allocated
Navigation
[0] Message Index
[#] Next page