delphi - Setting IHTMLDocument2 to nil (or FreeAndNil) causes Access violation -
i have system using ihtmldocument2 work html received idhttp, far good, problem depending on version of windows + version of internet explorer + other detail have not figured out, because mistakes not understand why happens. example, create:
var idoc : ihtmldocument2; begin idoc := createcomobject(class_htmldocument) ihtmldocument2; //work html... end;
the problem @ end, see forms , errors, if this:
idoc := nil;
get error
access violation @ address 000007fee4cff3e1 in module 'jscript9.dll'. read of address ffffffffffffff..
this error not occur cancel idoc, moments later (do not know when, not view debug)
if this:
freeandnil(idoc);
get error
access violation @ address 0000000000000000 in module 'program.exe'. execution of address 0000000000000000
this error occurs @ point.
and if nothing, or: = nil or freeandnil (). have error of dll 'jscript9.dll'
the problem in machines work in way , others way, have 2 windows same configuration , same version of internet explorer, in machine have use "idoc: = nil;" , other "freeandnil (idoc)" function without errors, wanted know why happens, because wanted follow pattern work on version of windows , internet explorer.
test unit:
unit f_prin; interface uses winapi.windows, winapi.messages, system.sysutils, system.variants, system.classes, vcl.graphics, vcl.controls, vcl.forms, vcl.dialogs, comobj, vcl.stdctrls, mshtml, winapi.activex, vcl.grids; type tform1 = class(tform) button1: tbutton; memo1: tmemo; radiobutton1: tradiobutton; radiobutton2: tradiobutton; radiobutton3: tradiobutton; stringgrid1: tstringgrid; button2: tbutton; button3: tbutton; procedure button1click(sender: tobject); procedure button2click(sender: tobject); procedure button3click(sender: tobject); private { private declarations } function pegaconteudofuncaojs(str, nomefunc : string) : string; function urlencodemj(s : string) : string; public { public declarations } end; var form1: tform1; implementation {$r *.dfm} function urlencode(const asrc: string): string; var i: integer; const unsafechars = ['*', '#', '%', '<', '>', ' ','[',']']; {do not localize} begin result := ''; {do not localize} := 1 length(asrc) begin if (asrc[i] in unsafechars) or (not (ord(asrc[i])in [33..128])) begin {do not localize} result := result + '%' + inttohex(ord(asrc[i]), 2); {do not localize} end else begin result := result + asrc[i]; end; end; end; function tform1.urlencodemj(s : string) : string; begin result := urlencode(s); result := stringreplace(result, '&', '&', [rfreplaceall]); end; procedure tform1.button2click(sender: tobject); begin stringgrid1.colcount := 1; stringgrid1.rowcount := 1; end; procedure tform1.button3click(sender: tobject); const ieversion : integer = 9; //get ie version var ovtable, v : olevariant; i, j, q, ncol, nrow : integer; mat : array of array of string; stemp : string; btemreg, bsai : boolean; //wbt : twebbrowser; idoc2 : ihtmldocument2; begin ncol := 0; nrow := 0; btemreg := false; := 0; j := 0; q := 0; bsai := false; try idoc2 := createcomobject(class_htmldocument) ihtmldocument2; idoc2.designmode := 'on'; if ieversion < 9 while idoc2.readystate <> 'complete' application.processmessages; v := vararraycreate([0, 0], varvariant); v[0] := memo1.text; idoc2.write(psafearray(system.tvardata(v).varray)); idoc2.designmode := 'off'; if ieversion < 9 while idoc2.readystate <> 'complete' application.processmessages; q := 0 (idoc2.all.tags('table') ihtmlelementcollection).length -1 begin ovtable := (idoc2.all.tags('table') ihtmlelementcollection).item(q, 0); := 0 (ovtable.rows.length - 1) begin j := 0 (ovtable.rows.item(i).cells.length - 1) begin stemp := trimright(trimleft(ovtable.rows.item(i).cells.item(j).innertext)); if (stemp = 'prova') = true begin bsai := true; break; end; end; if bsai = true break; end; if bsai = true break; end; if bsai = false //não achou... begin if radiobutton2.checked = true freeandnil(idoc2); if radiobutton1.checked = true idoc2 := nil; exit; end; if radiobutton2.checked = true freeandnil(idoc2); if radiobutton1.checked = true idoc2 := nil; nrow := ovtable.rows.length; stringgrid1.rowcount := nrow; setlength(mat, nrow); ncol := 7; stringgrid1.colcount := ncol+1; := 0 nrow - 1 setlength(mat[i], ncol+1); := 0 (nrow - 1) begin if = 0 //linha 0 = nome dos campos begin stemp := 'lances'; mat[i, 0] := stemp; stemp := ovtable.rows.item(i).cells.item(1).innertext; mat[i, 1] := stemp; stemp := ovtable.rows.item(i).cells.item(2).innertext; mat[i, 2] := stemp; stemp := ovtable.rows.item(i).cells.item(3).innertext; mat[i, 3] := stemp; stemp := ovtable.rows.item(i).cells.item(4).innertext; mat[i, 4] := stemp; stemp := ovtable.rows.item(i).cells.item(5).innertext; mat[i, 5] := stemp; stemp := ovtable.rows.item(i).cells.item(6).innertext; mat[i, 6] := stemp; stemp := 'link person'; mat[i, 7] := stemp; end else begin stemp := ovtable.rows.item(i).cells.item(0).innertext; mat[i, 0] := stemp; if ovtable.rows.item(i).cells.length > 1 //tem registros begin btemreg := true; stemp := ovtable.rows.item(i).cells.item(1).innertext; mat[i, 1] := stemp; stemp := ovtable.rows.item(i).cells.item(2).innertext; mat[i, 2] := stemp; stemp := ovtable.rows.item(i).cells.item(3).innertext; mat[i, 3] := stemp; stemp := ovtable.rows.item(i).cells.item(4).innertext; mat[i, 4] := stemp; stemp := ovtable.rows.item(i).cells.item(5).innertext; mat[i, 5] := stemp; stemp := ovtable.rows.item(i).cells.item(6).innertext; mat[i, 6] := stemp; stemp := ovtable.rows.item(i).cells.item(0).innerhtml; stemp := pegaconteudofuncaojs(stemp, 'window.open'); stemp := copy(stemp, 0, pos(',', stemp)-1); stemp := stemp.replace('''', '', [rfreplaceall]); stemp := urlencodemj(stemp); mat[i, 7] := stemp; end; end;//end if linha = 0 end;//end (geral) := 0 (nrow - 1) begin j := 0 (length(mat[i]) - 1) begin stringgrid1.cells[j, i] := mat[i, j]; end; end; application.processmessages; except on e: exception begin if radiobutton2.checked = true freeandnil(idoc2); if radiobutton1.checked = true idoc2 := nil; end; end; end; function tform1.pegaconteudofuncaojs(str, nomefunc : string) : string; var stemp : string; begin stemp := copy(str, pos(nomefunc+'(', str) + (length(nomefunc)+1), length(str)); stemp := copy(stemp, 0, pos(')', stemp) - 1); result := stemp; end; procedure tform1.button1click(sender: tobject); const ieversion : integer = 9; //get ie version var ovtable, v : olevariant; i, j, q, ncol, nrow : integer; mat : array of array of string; stemp : string; btemreg, bsai : boolean; //wbt : twebbrowser; idoc : ihtmldocument2; begin ncol := 0; nrow := 0; btemreg := false; := 0; j := 0; q := 0; bsai := false; try idoc := createcomobject(class_htmldocument) ihtmldocument2; idoc.designmode := 'on'; if ieversion < 9 while idoc.readystate <> 'complete' application.processmessages; v := vararraycreate([0, 0], varvariant); v[0] := memo1.text; idoc.write(psafearray(system.tvardata(v).varray)); idoc.designmode := 'off'; if ieversion < 9 while idoc.readystate <> 'complete' application.processmessages; q := 0 (idoc.all.tags('table') ihtmlelementcollection).length -1 begin ovtable := (idoc.all.tags('table') ihtmlelementcollection).item(q, 0); := 0 (ovtable.rows.length - 1) begin j := 0 (ovtable.rows.item(i).cells.length - 1) begin stemp := trimright(trimleft(ovtable.rows.item(i).cells.item(j).innertext)); if (stemp = 'prova') = true begin bsai := true; break; end; end; if bsai = true break; end; if bsai = true break; end; if bsai = false //não achou... begin if radiobutton2.checked = true freeandnil(idoc); if radiobutton1.checked = true idoc := nil; exit; end; if radiobutton2.checked = true freeandnil(idoc); if radiobutton1.checked = true idoc := nil; nrow := ovtable.rows.length; stringgrid1.rowcount := nrow; setlength(mat, nrow); ncol := 7; stringgrid1.colcount := ncol+1; := 0 nrow - 1 setlength(mat[i], ncol+1); := 0 (nrow - 1) begin if = 0 //linha 0 = nome dos campos begin stemp := 'lances'; mat[i, 0] := stemp; stemp := ovtable.rows.item(i).cells.item(1).innertext; mat[i, 1] := stemp; stemp := ovtable.rows.item(i).cells.item(2).innertext; mat[i, 2] := stemp; stemp := ovtable.rows.item(i).cells.item(3).innertext; mat[i, 3] := stemp; stemp := ovtable.rows.item(i).cells.item(4).innertext; mat[i, 4] := stemp; stemp := ovtable.rows.item(i).cells.item(5).innertext; mat[i, 5] := stemp; stemp := ovtable.rows.item(i).cells.item(6).innertext; mat[i, 6] := stemp; stemp := 'link person'; mat[i, 7] := stemp; end else begin stemp := ovtable.rows.item(i).cells.item(0).innertext; mat[i, 0] := stemp; if ovtable.rows.item(i).cells.length > 1 //tem registros begin btemreg := true; stemp := ovtable.rows.item(i).cells.item(1).innertext; mat[i, 1] := stemp; stemp := ovtable.rows.item(i).cells.item(2).innertext; mat[i, 2] := stemp; stemp := ovtable.rows.item(i).cells.item(3).innertext; mat[i, 3] := stemp; stemp := ovtable.rows.item(i).cells.item(4).innertext; mat[i, 4] := stemp; stemp := ovtable.rows.item(i).cells.item(5).innertext; mat[i, 5] := stemp; stemp := ovtable.rows.item(i).cells.item(6).innertext; mat[i, 6] := stemp; stemp := ovtable.rows.item(i).cells.item(0).innerhtml; stemp := pegaconteudofuncaojs(stemp, 'window.open'); stemp := copy(stemp, 0, pos(',', stemp)-1); stemp := stemp.replace('''', '', [rfreplaceall]); stemp := urlencodemj(stemp); mat[i, 7] := stemp; end; end;//end if linha = 0 end;//end (geral) := 0 (nrow - 1) begin j := 0 (length(mat[i]) - 1) begin stringgrid1.cells[j, i] := mat[i, j]; end; end; application.processmessages; except on e: exception begin if radiobutton2.checked = true freeandnil(idoc); if radiobutton1.checked = true idoc := nil; end; end; end; end.
dfm:
object form1: tform1 left = 0 top = 0 caption = 'form1' clientheight = 583 clientwidth = 635 color = clbtnface font.charset = default_charset font.color = clwindowtext font.height = -11 font.name = 'tahoma' font.style = [] oldcreateorder = false pixelsperinch = 96 textheight = 13 object button1: tbutton left = 278 top = 256 width = 75 height = 25 caption = 'test 1' taborder = 0 onclick = button1click end object memo1: tmemo left = 0 top = 0 width = 635 height = 225 align = altop lines.strings = ( '<html>' '<head>' '<meta http-equiv="expires" content="0">' '<meta http-equiv="cache-control" content="private">' '<meta http-equiv="pragma" content="no-cache">' '<meta http-equiv="refresh" content="600; url=lsd.asp">' '<meta http-equiv="content-type" content="text/html; charset=iso-' + '8859-1">' '<link rel="stylesheet" href="../../imagens/style1.css" type="tex' + 't/css">' '<script language="javascript">' '<!--' 'function mm_reloadpage(init) { //updated pvii. reloads w' + 'indow if nav4 resized' ' if (init==true) (navigator) {if ((appname=="netscape")&&(' + 'parseint(appversion)==4)) {' ' document.mm_pgw=innerwidth; document.mm_pgh=innerheight; onr' + 'esize=mm_reloadpage; }}' ' else if (innerwidth!=document.mm_pgw || innerheight!=document.' + 'mm_pgh) location.reload();' '}' 'mm_reloadpage(true);' '//-->' '</script>' '</head>' '<body onload="setmousecursortohand('#39'type'#39','#39'button'#39');" bgcolor="#' + 'ffffff" text="#000000" leftmargin="0" topmargin="0" ' 'marginwidth="0" marginheight="0" ' 'class="body_1">' '<script language="javascript" type="text/javascript" src="../../' + 'imagens/action.js"></script>' '<table width="100%" border="0" cellspacing="0" cellpadding="0">' #9'<tr> ' #9#9'<td><img src="/imagens/x2_dot.gif" width="2" height="2"></td>' ' </tr>' ' <tr> ' #9#9'<td valign="top"> ' ' <table width="100%" border="0" cellspacing="1" cel' + 'lpadding="2">' ' <td width="5"><img src="/imagens/x2_dot.gif" w' + 'idth="2" height="2" /></td>' ' <td> ' ' <table width="100%" border="0" cellspacing="' + '0" cellpadding="0">' ' <tr> ' ' <td colspan="2" class="tex3a"><img src="' + '../../imagens/es.gif" width="199" height="23" ' 'align="absbottom" /> </td>' ' <td class="tex3a" width="23%"> ' ' <!--div para logotipos-->' ' <div id="layer4" style="position:absol' + 'ute; width:183px; height:115px; z-index:4; top: 32px; visibility' + ': ' 'visible"></div>' ' <!--div para logotipos- fim-->' ' </td>' ' </tr>' ' <tr> ' ' <td width="1%"><img src="../../imagens/r' + 'isco.gif" width="5" /></td>' ' <th colspan="3" nowrap><img src="../../i' + 'magens/risco2.gif" width="100%" height="3" /></th>' ' </tr>' ' </table>' ' </td>' ' <td width="5"> <img src="/imagens/x2_dot.gif" ' + 'width="2" height="2" /></td>' ' </tr>' ' </table>' ' <table border="0" cellspacing="1" cellpadding="2" ' + 'width="100%">' ' <tr> ' ' <td valign="top" align="right" width="23"><i' + 'mg src="../../imagens/tm_bullet4.gif" width="6" height="14" /></' + 'td>' ' <td class="tex3">clique para ver detalhes</t' + 'd>' ' </tr>' ' <tr> ' ' <td valign="top" align="right" width="23"><i' + 'mg src="../../imagens/tm_bullet4.gif" width="6" height="14" /></' + 'td>' ' <td class="tex3">aten'#195#167#195#163'o!<br /></td>' ' </tr>' ' <tr> ' ' <td valign="top" align="right" width="23">&n' + 'bsp;</td>' ' <td class="tex3b">' ' <table border="0" width="100%" class="td' + '" cellpadding="2" cellspacing="1">' '<tr>' '<td></td>' '<td class="tex3b" align="center">prova</td>' '<td align="center" class="tex3b" nowrap="nowrap">cod.</td>' '<td align="center" class="tex3b" nowrap="nowrap">empresa</td>' '<td align="center" class="tex3b">data abertura</td>' '<td align="center" class="tex3b">jjo</td>' '<td align="center" class="tex3b">plo</td>' '<td></td>' '</tr>' '<tr>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '<a href="#" onclick="javascript:window.open('#39'gerencia_sdf.asp?pr' + 'gcod=545686&numprp=492015&indsrp=n'#195 #163'o&indicms=n'#195 #163'o&cns=true'#39',' '' #9' ' #39#39','#39'toolbar=no,location=no,directories=no,status=no,menubar=no,s' + 'crollbars=yes,resizable=yes,copyhistory=no,top=0,left=0, ' 'width=750, ' 'height=550'#39');return ' ' false;">item</a>' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '<a href="#" onclick="javascript:window.open('#39'detalhe_prop.asp?pr' + 'gcod=545686'#39', ' ' '#39'detalhe_proposta'#39','#39'maximized=yes, screenx=250, screeny=' + '0, ' 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrol' + 'lbars=yes,resizable=no,copyhistory=no,width=550,height' '=400'#39');return ' ' false;">492015</a>' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '925217' '</td>' '<td bgcolor="#ffffff" class="tex3" align="center">' 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '15/05/2015 11:00' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">não</t' + 'd>' '<td bgcolor="#ffffff" class="tex3a" align="center">não</t' + 'd>' '<td bgcolor="#ffffff" class="tex3" align="center" />' '</tr>' '' '<tr>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '<a href="#" onclick="javascript:window.open('#39'gerencia_sdf.asp?pr' + 'gcod=545686&numprp=492015&indsrp=n'#195 #163'o&indicms=n'#195 #163'o&cns=true'#39',' #9' ' #39#39','#39'toolbar=no,location=no,directories=no,status=no,menubar=no,s' + 'crollbars=yes,resizable=yes,copyhistory=no,top=0,left=0, ' 'width=750, ' 'height=550'#39');return ' ' false;">item</a>' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '<a href="#" onclick="javascript:window.open('#39'detalhe_prop.asp?pr' + 'gcod=545686'#39', ' ' '#39'detalhe_proposta'#39','#39'maximized=yes, screenx=250, screeny=' + '0, ' 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrol' + 'lbars=yes,resizable=no,copyhistory=no,width=550,height' '=400'#39');return ' ' false;">445665</a>' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '845645' '</td>' '<td bgcolor="#ffffff" class="tex3" align="center"> ooooooooooooo' + 'ooooooooooo' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '15/05/2015 11:00' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">não</t' + 'd>' '<td bgcolor="#ffffff" class="tex3a" align="center">não</t' + 'd>' '<td bgcolor="#ffffff" class="tex3" align="center" />' '</tr>' '' '' '<tr>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '<a href="#" onclick="javascript:window.open('#39'gerencia_fsdf.asp?p' + 'rgcod=545686&numprp=492015&indsrp=n'#195 #163'o&indicms=n'#195 #163'o&cns=true'#39',' #9' ' #39#39','#39'toolbar=no,location=no,directories=no,status=no,menubar=no,s' + 'crollbars=yes,resizable=yes,copyhistory=no,top=0,left=0, ' 'width=750, ' 'height=550'#39');return ' ' false;">item</a>' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '<a href="#" onclick="javascript:window.open('#39'detalhe_prop.asp?pr' + 'gcod=545686'#39', ' ' '#39'detalhe_proposta'#39','#39'maximized=yes, screenx=250, screeny=' + '0, ' 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrol' + 'lbars=yes,resizable=no,copyhistory=no,width=550,height' '=400'#39');return ' ' false;">412345</a>' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '785455' '</td>' '<td bgcolor="#ffffff" class="tex3" align="center"> kkkkkkkkkkkkk' + 'kkkkkkkkkkkkkkkkk' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '15/05/2015 11:00' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">não</t' + 'd>' '<td bgcolor="#ffffff" class="tex3a" align="center">não</t' + 'd>' '<td bgcolor="#ffffff" class="tex3" align="center" />' '</tr>' '' '' '<tr>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '<a href="#" onclick="javascript:window.open('#39'gerencia_fsdf.asp?p' + 'rgcod=545686&numprp=492015&indsrp=n'#195 #163'o&indicms=n'#195 #163'o&cns=true'#39',' #9' ' #39#39','#39'toolbar=no,location=no,directories=no,status=no,menubar=no,s' + 'crollbars=yes,resizable=yes,copyhistory=no,top=0,left=0, ' 'width=750, ' 'height=550'#39');return ' ' false;">item</a>' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '<a href="#" onclick="javascript:window.open('#39'detalhe_prop.asp?pr' + 'gcod=545686'#39', ' ' '#39'detalhe_proposta'#39','#39'maximized=yes, screenx=250, screeny=' + '0, ' 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrol' + 'lbars=yes,resizable=no,copyhistory=no,width=550,height' '=400'#39');return ' ' false;">498745</a>' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '312456' '</td>' '<td bgcolor="#ffffff" class="tex3" align="center"> ppppppppppppp' + 'pppppppppppppppp' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">' '15/05/2015 11:00' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center"> não' '</td>' '<td bgcolor="#ffffff" class="tex3a" align="center">não</t' + 'd>' '<td bgcolor="#ffffff" class="tex3" align="center" />' '</tr>' '' '</table>' '' #9#9' </td>' ' <tr> ' ' <td></td> ' ' <td><br />' ' <input type="button" name="voltar" value' + '="voltar" class="texfield2" ' 'onclick="location.href='#39'pregao1.asp'#39';return false;" />' ' </td>' ' </tr>' ' </table>'#9#9#9 ' </td>' ' </tr>' '</table>' '' '</body>' '</html>') taborder = 1 end object radiobutton1: tradiobutton left = 14 top = 261 width = 51 height = 17 caption = ':= nil' checked = true taborder = 2 tabstop = true end object radiobutton2: tradiobutton left = 70 top = 261 width = 88 height = 17 caption = 'freeandnil()' taborder = 3 end object radiobutton3: tradiobutton left = 166 top = 261 width = 57 height = 17 caption = 'nothing' taborder = 4 end object stringgrid1: tstringgrid left = 0 top = 312 width = 635 height = 271 align = albottom taborder = 5 end object button2: tbutton left = 552 top = 256 width = 75 height = 25 caption = 'clear' taborder = 6 onclick = button2click end object button3: tbutton left = 360 top = 256 width = 75 height = 25 caption = 'test 2' taborder = 7 onclick = button3click end end
Comments
Post a Comment