Forum > Windows
Access Violation on Procedure Exit
therealhades:
I allocated the memory before the procedure call, sorry for not showing this.
Here is the complete procedure which calls the ReadString procedure:
--- 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.ReadButtonConfig;var i, j: integer; AdsError: LongInt; tmp: string;begin AdsError:= AdsPortOpen(); //ShowMessage('AdsPort: ' + AdsError.ToString); if AMS.port <> 0 then begin for i:= 0 to 7 do begin j:= i + 1; btn[i].VarName:= 'Config.Btn_Grp' + j.ToString; btn[i].Handle:= 0; end; end; ReadStringVar(btn[0]); ShowMessage('after procedure: ' + btn[0].Data); for i:= 1 to 7 do begin ReadStringVar(btn[i]); end; btnGrp1.Caption:= btn[0].Data; btnGrp2.Caption:= btn[1].Data; btnGrp3.Caption:= btn[2].Data; btnGrp4.Caption:= btn[3].Data; btnGrp5.Caption:= btn[4].Data; btnGrp6.Caption:= btn[5].Data; btnGrp7.Caption:= btn[6].Data; btnGrp8.Caption:= btn[7].Data;end;
I think Line 19 is executed fine, because I got the MessageBox from the end of the procedure, but before Line 20 is executed I got the AccessViolation (only in WinCE, Win32 works fine).
The decalaration of btn:
--- 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";}};} ---var frmMain: TfrmMain; btn: array[0..7] of TStringVar;
Thaddy:
That record is wrong anyway. I would define it as:
--- 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[255]; // to avoid it is a pointer type Handle: LongWord; Data: String;end;This assumes the string is never longer than 255 bytes, because it is a ShortString.
It has no indirections, no pointers. All data is part of the record.
But misusing strings to catch data from lines is not a smart way anyway.
You should always catch that in binary format. Which means use a stream or a large enough buffer.
therealhades:
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";}};} --- TStringVar = record VarName: String[255]; Handle: LongWord; Data: String; end;
Same effect. I changed the calling procedure to the following for a test:
--- 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.ReadButtonConfig;var i, j: integer; AdsError: LongInt; tmp: string;begin AdsError:= AdsPortOpen(); //ShowMessage('AdsPort: ' + AdsError.ToString); if AMS.port <> 0 then begin for i:= 0 to 7 do begin j:= i + 1; btn[i].VarName:= 'Config.Btn_Grp' + j.ToString; btn[i].Handle:= 0; end; end; ReadStringVar(btn[0]); ReadStringVar(btn[0]);end;
The effect is that the first procedure call is executed and i get the MessageBox, then i get the AccessViolation and not the second MessageBox.
Thaddy:
Really?
--- 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";}};} ---var tmp: array of char;Why not:
--- 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";}};} ---var tmp: string[255] // and start your index at 1Without seeing more code I can't reply with any other suggestions, because I don't have your PLC. But it transmits binary data, so your code should handle the data in the binary domain. NOT as string.
Zvoni:
Single-step through your code, and report the exact line you get the AV
Navigation
[0] Message Index
[#] Next page
[*] Previous page