Author Topic: access violation calling FindNextFileA  (Read 2567 times)

0 Members and 1 Guest are viewing this topic.

Offline blametroi

  • Newbie
  • Posts: 26
    • View Profile
access violation calling FindNextFileA
« on: December 28, 2018, 09:32:21 pm »
I've been working on my API testing program and with the prior advice I've made pretty good progress, but now I get an access violation after some number of calls to FindNextFileA. It appears to be deterministic, if I change the file spec I am searching for, I can get a different number of looping calls to FindNextFileA before the failure.

I'm not a C++ programmer so I'm not sure how to approach this on my own.

Event log information and code attached. Failure occurs on the call:

    Result% = FindNextFile&(hFind&, ffd)

I've confirmed that the ffd structure is the correct length so there's no overwrite of memory that could be happening there. I've thrown in some debug prints and the find handle doesn't appear to get walked on either.

Ideas/advice?

Quote
Log Name:      Application
Source:        Application Error
Date:          12/28/2018 9:17:10 PM
Event ID:      1000
Task Category: (100)
Level:         Error
Keywords:      Classic
User:          N/A
Computer:      yirgacheffe.blametroi.com
Description:
Faulting application name: winapitesting.exe, version: 0.0.0.0, time stamp: 0x5c26d8e5
Faulting module name: msvcrt.dll, version: 7.0.17134.1, time stamp: 0xc5dd3631
Exception code: 0xc0000005
Fault offset: 0x00088a7a
Faulting process id: 0x61f8
Faulting application start time: 0x01d49f1c9a214fb7
Faulting application path: E:\programming\qb\winapitesting.exe
Faulting module path: C:\WINDOWS\System32\msvcrt.dll
Report Id: d579a6ac-77a9-4b93-afce-1287162c2c56
Faulting package full name:
Faulting package-relative application ID:
Code: QB64: [Select]
  1. ' windows api testing, build a set of
  2. ' useful routines
  3. ' starting code is lifted from the qb64.org wiki to
  4. ' bootstrap the effort, then i'll add my own stuff.
  5. '
  6. ' txb 27 December 2018
  7.  
  8.  
  9. ' use a cmd console window instead of a screen
  10.  
  11. ' *************************************
  12. ' Windows API constants and types
  13.  
  14. CONST MAX_PATH = 260
  15.  
  16. ' FINDEX_INFO_LEVELS
  17. CONST FindExInfoStandard = 0
  18. CONST FindExInfoBasic = 1
  19.  
  20. ' FINDEX_SEARCH_OPS
  21. CONST FindExSearchNameMatch = 0
  22. CONST FindExSearchLimitToDirectories = 1
  23. CONST FindExSearchLimitToDevices = 2
  24.  
  25. ' Error codes from Windows
  26. CONST ERROR_FILE_NOT_FOUND = 2
  27. CONST ERROR_NO_MORE_FILES = 18
  28.  
  29. ' WIN32_FIND_DATA
  30. TYPE FindFileData
  31.     FileAttributes AS LONG
  32.     CreationTime AS STRING * 8 ' actually these are FileTimes, 64 bit nanoseconds from 1601
  33.     LastAccessTime AS STRING * 8
  34.     LastWriteTime AS STRING * 8
  35.     FileSizeHigh AS LONG
  36.     FileSizeLow AS LONG
  37.     reserved0 AS LONG
  38.     reserved1 AS LONG
  39.     FileName AS STRING * MAX_PATH
  40.     AlternateFileName AS STRING * 14 ' DOS 8.3 format name
  41.     FileType AS LONG
  42.     CreatorType AS LONG
  43.     FinderFlags AS INTEGER
  44.  
  45. ' *************************************
  46. ' Windows API functions, some originally from
  47. ' qb64.org/wiki examples, others created for this
  48. ' effort.
  49.  
  50. DECLARE LIBRARY 'Directory Information using KERNEL32 provided by Dav
  51.     FUNCTION GetWindowsDirectory ALIAS GetWindowsDirectoryA (lpBuffer AS STRING, BYVAL nSize AS LONG)
  52.     FUNCTION GetSystemDirectory ALIAS GetSystemDirectoryA (lpBuffer AS STRING, BYVAL nSize AS LONG)
  53.     FUNCTION GetCurrentDirectory ALIAS GetCurrentDirectoryA (BYVAL nBufferLen AS LONG, lpBuffer AS STRING)
  54.     FUNCTION GetTempPath ALIAS GetTempPathA (BYVAL nBufferLen AS LONG, lpBuffer AS STRING)
  55.     FUNCTION GetModuleFileName ALIAS GetModuleFileNameA (BYVAL hModule AS LONG, lpFileName AS STRING, BYVAL nSize AS LONG)
  56.     FUNCTION FindClose (hFindFile AS LONG)
  57.     FUNCTION GetLastError& ()
  58.  
  59.     ' Per FillippeHeitor at qb64.org, moved from 'declare library'
  60.     ' to 'declare customtype library' to get around a C++
  61.     ' compile error. Interestingly, the various functions in the
  62.     ' above 'declare library' fail if put under 'customtype'.
  63.     FUNCTION FindFirstFileEx& ALIAS FindFirstFileExA (lpFileName AS STRING, BYVAL fInfoLevelId AS LONG, lpFindFileData AS FindFileData, BYVAL fSearchOp AS LONG, BYVAL aNull AS LONG, BYVAL AdditionalFlags AS LONG)
  64.     FUNCTION FindNextFile& ALIAS FindNextFileA (BYVAL handle AS LONG, lpFindFileData AS FindFileData)
  65.  
  66. ' *************************************
  67. ' Variables and more variables
  68.  
  69. DIM ResultLength&
  70. DIM CurrentDirectory$, ModuleFileName$
  71. DIM ffd AS FindFileData
  72. DIM searchString AS STRING * MAX_PATH
  73. DIM hFind&
  74. DIM LastError&
  75. DIM Result%
  76.  
  77. ' *************************************
  78. ' Some process related information
  79.  
  80. ' SHOW CURRENT DIRECTORY
  81. CurrentDirectory$ = SPACE$(MAX_PATH)
  82. ResultLength& = GetCurrentDirectory(MAX_PATH, CurrentDirectory$)
  83. IF ResultLength& THEN
  84.     PRINT "CURRENT DIRECTORY: "; LEFT$(CurrentDirectory$, ResultLength&)
  85.  
  86. ' SHOW CURRENT PROGRAM
  87. ModuleFileName$ = SPACE$(MAX_PATH)
  88. ResultLength& = GetModuleFileName(0, ModuleFileName$, MAX_PATH)
  89. IF ResultLength& THEN
  90.     PRINT "CURRENTLY EXECUTING PROGRAM: "; LEFT$(ModuleFileName$, ResultLength&)
  91.  
  92. ' *************************************
  93. ' look for a likely non-existing file to demonstrate
  94. ' error handling
  95. '***to be provided***
  96.  
  97. ' *************************************
  98. ' list all the *.bas files in the directory
  99. searchString = "*.*" + CHR$(0)
  100. PRINT "seeking files matching "; searchString
  101. hFind& = FindFirstFileEx&(searchString, FindExInfoStandard&, ffd, 0&, FindExSearchNameMatch, 0&)
  102. IF hFind& < 0 THEN
  103.     LastError& = GetLastError&
  104.     PRINT "find first failed for "; searchString
  105.     PRINT LastError&
  106.     DIM a$
  107.     INPUT a$
  108.     SYSTEM 0
  109. PRINT "found some files:"
  110. PRINT LEN(ffd)
  111.     PRINT LEFT$(ffd.FileName$, Strlen(ffd.FileName$, MAX_PATH))
  112.     'PRINT "found ", ffd.FileName$
  113.     'PRINT Strlen(ffd.FileName$, MAX_PATH%)
  114.     PRINT hFind&
  115.     Result% = FindNextFile&(hFind&, ffd)
  116.     PRINT Result%
  117. LOOP WHILE Result%
  118. DIM george$
  119. INPUT george$
  120. Result% = FindClose(hFind&)
  121. PRINT Result%
  122.  
  123. DIM fred$
  124. INPUT fred$
  125. ' and done
  126. SYSTEM 0 ' instead of END
  127.  
  128. ' *************************************
  129. ' Return length of a null terminated (asciiz) string
  130. '
  131. ' this is bugged, it isn't going to handle zero
  132. ' length strings correctly
  133. ' nor max length strings
  134. FUNCTION Strlen% (s AS STRING * 1, maxlen AS INTEGER)
  135.     DIM i AS INTEGER
  136.     i = 1
  137.     WHILE i < maxlen AND ASC(MID$(s, i)) <> 0
  138.         i = i + 1
  139.     WEND
  140.     Strlen% = i - 1
  141.  

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: access violation calling FindNextFileA
« Reply #1 on: December 29, 2018, 09:47:47 am »
Hi
running in windows 10 64bit on intel i3  4GigaRam 500HDD and compiling with QB64 32bit version 1.2 Dev e490b1a

I got as results 5 printed informations and then an "out of memory" critical error with only OK button to accept the crashing of instance runned.
In the folder in which the application runs there are 13 files.

Programming isn't difficult, only it's  consuming time and coffee

Offline blametroi

  • Newbie
  • Posts: 26
    • View Profile
Re: access violation calling FindNextFileA
« Reply #2 on: December 29, 2018, 11:01:56 am »
More ram here but also Windows 10 (pro) 64-bit. I've got the latest dev build downloaded, git 6fe5c48. For me the app just disappears and the event log is updated. I'm trying with the release build to see if I can get any different information.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: access violation calling FindNextFileA
« Reply #3 on: December 29, 2018, 08:13:48 pm »
Changing your code, putting out your function Strlen%,
Code: QB64: [Select]
  1.  
  2. PRINT LEFT$(ffd.FileName$, Strlen(ffd.FileName$, MAX_PATH))

to

Code: QB64: [Select]
  1. PRINT LEFT$(ffd.FileName$, INSTR(ffd.FileName$, CHR(0)) -1)
  2.  
  3.  
  I got only an application  that  crashes suddenly.
Programming isn't difficult, only it's  consuming time and coffee