Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird Access Violation in program 2

Status
Not open for further replies.

amateursRus

Technical User
Oct 10, 2005
15
GB
Hi All,

Hope someone can take pity on me and figure out what the heck is happening here...

I'm using D6 Ent. Edition on NT4 (don't laugh - someone has to!) and have a working program which writes a list of usersids' to a text file.

The original code fragment follows;

for i:= 0 to (UserList.Count-1) do begin
writeln(t, s1+ '<option value=' + QuotedStr(UserList) + '>');
writeln(t, s2+ '<xsl:if test="$selected_userid=' + QuotedStr(UserList) + '">');
writeln(t, s3+ '<xsl:attribute name="selected">Selected</xsl:attribute>');
writeln(t, s2+ '</xsl:if>' + (UserList) + '</option>');
end;

Basically, it's creating part of an XSL file (s1..s3 are spacers to make it easier for me to read) for use in an HTML data island and has worked faultlessly for over 18 months.

HOWEVER!!
The users have requested some improvements to the end result and I've therefore made certain changes to the file.
These are shown below in the new code fragment;

for i:= 0 to (UserList.Count-1) do begin
writeln(t, s1+ '<option>' + (UserList) + '</option>');
//if (i>35) then ShowMessage('UserList ' + IntToStr(i) + ' = ' + tempUser);
end;

This is simply used to create a list of users for an HTML 'select' drop-down.
Please note the 'REMmed' out line is there purely for debugging...:)

Nothing else has been changed.

Now, the weird thing is that when the showmessage is enabled the code runs without errors but when it's disabled the following error invariably appears;

Access violation at address 77F34EB5 in module "kernel32.dll". Read of address 00000025

The REALLY weird thing is, the program still produces the goods even after the error and the final report for the users is fine....
Any ideas on how to fix this?

Regards

Steve
 
Where is the new code fragment in relation to the old/original code fragment?

Is there any code in between?

Which line does the debugger complain about?

Has then entry in UserList been initialized?

Regards,

Django
 
Hi DjangMan,

Thanks for the swift response!

In answer to your questions -

The new code fragment replaces the old, in exactly the same place.

No, there is no code between.

The error occurs immediately after the code fragment is exited.

UserList has been initialized (ShowMessage successfully displays a User name - a previous version of the message displayed the UserList.Count).

Hope this helps...:)

Regards

Steve
 
Bugs which "appear" and "disappear" changing unrelated code
are a symptome of:

a) Bad pointers, uninitialized or destroyed objects, bad mannaged ansistring or dynarray, etc, etc.

b) Threads out of synchronization (in multithreaded apps).

What you said about uncommenting the "if" is pretty significative, BTW.

This kind of bug can sleep for months and wake up with any change. I think a broad code check is needed. The app is multithreaded? Are you accessing the VCL main thread without propper synchro?

buho (A).
 
Hi buho and Django,

Again, many thanks for your interest and help.

buho, the app. doesn't use pointers or dynamic arrays and is single threaded.

Django, the following comes immediately after the code fragment -

Flush(t);

FileMode := 0; {Set file access to read only }
Reset(f);
while not Eof(f) do begin
Readln(f, r);
writeln(t, r);
end;
Flush(t);
CloseFile(f);

CloseFile(t);
UserList.Free;

MainForm.WinCopyFile(C_file, G_file);

end;


The variables 'f' and 't' are textfiles while 'r' is a string.
"WinCopyFile" is the following -

function TMainForm.WinCopyFile(Source, Dest: string): Boolean;
var
Struct : TSHFileOpStruct;
Resultval: integer;
begin
ResultVal := 1;
try
Source := Source + #0#0;
Dest := Dest + #0#0;
Struct.wnd := 0;
Struct.wFunc := FO_COPY;
Struct.pFrom := PChar(Source);
Struct.pTo := PChar(Dest);
Struct.fFlags:= FOF_SIMPLEPROGRESS or FOF_NOERRORUI or FOF_NOCONFIRMATION;
Struct.fAnyOperationsAborted := False;
Struct.hNameMappings := nil;
Resultval := ShFileOperation(Struct);
finally
Result := (Resultval = 0);
end;
end;

Hasten to say, I got this from a forum (would that I were that clever!!).:)

Regards

Steve

ps; the last couple of attemps to run the app the error hasn't appeared!!!
 
Mmmm.... from the SDK:

<<
typedef struct _SHFILEOPSTRUCT {
HWND hwnd;
UINT wFunc;
LPCTSTR pFrom;
LPCTSTR pTo;
FILEOP_FLAGS fFlags;
BOOL fAnyOperationsAborted;
LPVOID hNameMappings;
LPCTSTR lpszProgressTitle;
} SHFILEOPSTRUCT, *LPSHFILEOPSTRUCT;

lpszProgressTitle
Address of a string to use as the title of a progress dialog box. This member is used only if fFlags includes the FOF_SIMPLEPROGRESS flag.
>>

You are using FOF_SIMPLEPROGRESS but not assigning the lpszProgressTitle field. The record is a stack variable, the field will have random values... and it is a pointer :).

Use somethimg like Struct.lpszProgressTitle = '' or Struct.lpszProgressTitle = 'This is my title';

>>
the app. doesn't use pointers
<<

You are using pointer everywhere, everytime, likeing it or not, knowing it or not :).

buho (A).

 
Hi buho,

Thanks for the heads-up on the SDK code!
I've changed it as you suggest and so far no further occurrence of the error (fingers crossed!).

Re. pointers - please excuse my embarrassed blushes...:)

Regards

Steve
 
Evil rounds every corner" - Minsc in Baldur's Gate I. :)

Fingers crossed here too. Carefully check your code, may be some other bug is waiting in the shadows to backstab you.

buho (A).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top