Page 1 of 1

[solved] Is it bug or just me

Posted: Thu Dec 26, 2024 2:53 am
by Jal
Hi All, happy seasons greetings.

I've been coding in PB for some time, mostly my own code, but recent version update broke lots of my code in ways I can not find, and have had to go backwards and compile with old version in same cases.. because I could not find whwre the problem was..

Then a few days back I tried a simple experiment and it failed too, so here is that code which breaks on multiple versions in diffirent ways.

Code: Select all

EnableDebugger
; Test Code

; ERRORS when using non-hardwired parameters in call to ExamineDirectory()
; code based on code from PB-HELP, only save result to variable is different.

; Breaks on call to NextDirectoryEntry() in compiled or in debug mode
; only happens when using NON hardwired numbers like ExamineDirectory(0)


; Set #TEST_CODE constant below to either 1, 2, 3 or 4, then compile run with Debugger

;#TEST_CODE = 1          ; Works as expected, as per help file
#TEST_CODE = 2          ; Using #PB_Any, produces ERROR
;#TEST_CODE = 3          ; use a constant, produces ERROR
;#TEST_CODE = 4          ; use a variable


;; RESULTS
;;
;;; >>> [ERROR] The specified #Directory is not initialised.
;;

; Tried this in different version of PureBasic

;; PureBasic 6.04 LTS (Windows - x64)
;;	WORKS for #TEST_CODE = 1 & 2
;;	Will Break for #TEST_CODE = 3 & 4

;; PureBasic 6.12 LTS (Windows - x64)
;;	WORKS for #TEST_CODE = 1 ONLY
;;	Breaks for every other case #TEST_CODE = 2, 3 & 4


Directory$ = GetHomeDirectory() ; Lists files in the home directory

CompilerIf #TEST_CODE = 1
  ; from help file, only save to var lDir is change, else all same.
  ; ### THIS WORKS ####
  
  lDir.l = ExamineDirectory(0, Directory$, "*.test")
  If lDir
    While NextDirectoryEntry(0)
      If DirectoryEntryType(0) = #PB_DirectoryEntry_File
        Type$ = "[File] "
        Size$ = " (Size: " + DirectoryEntrySize(0) + ")"
      Else
        Type$ = "[Directory] "
        Size$ = "" ; A directory doesn't have a size
      EndIf
  
      Debug Type$ + DirectoryEntryName(0) + Size$
    Wend
    FinishDirectory(0)
  EndIf
CompilerEndIf


CompilerIf #TEST_CODE = 2
  ; >>>> NOTE using #PB_Any
  lDir.l = ExamineDirectory(#PB_Any, Directory$, "*.test")
  If lDir
    While NextDirectoryEntry(lDir)
      If DirectoryEntryType(lDir) = #PB_DirectoryEntry_File
        Type$ = "[File] "
        Size$ = " (Size: " + DirectoryEntrySize(lDir) + ")"
      Else
        Type$ = "[Directory] "
        Size$ = "" ; A directory doesn't have a size
      EndIf

      Debug Type$ + DirectoryEntryName(lDir) + Size$
    Wend
    FinishDirectory(lDir)
  EndIf
CompilerEndIf


CompilerIf #TEST_CODE = 3
  
  ; declare a constant instead of #PB_Any
  ; NOTE: It could be any vaild variable here, too it breaks
  Enumeration
    #TEST_DIR_SEARCH
  EndEnumeration

  ; >>>> NOTE using constant
  lDir.l = ExamineDirectory(#TEST_DIR_SEARCH, Directory$, "*.test")
  If lDir
    While NextDirectoryEntry(lDir)
      If DirectoryEntryType(lDir) = #PB_DirectoryEntry_File
        Type$ = "[File] "
        Size$ = " (Size: " + DirectoryEntrySize(lDir) + ")"
      Else
        Type$ = "[Directory] "
        Size$ = "" ; A directory doesn't have a size
      EndIf

      Debug Type$ + DirectoryEntryName(lDir) + Size$
    Wend
    FinishDirectory(lDir)
  EndIf
CompilerEndIf

CompilerIf #TEST_CODE = 4
  
  ; declare a variable instead of hard coded
  ;Define iExDir.i = 0
  iExDir.i = 0

  ; >>>> NOTE using Variable "iExDir"
  lDir.l = ExamineDirectory(iExDir, Directory$, "*.test")
  If lDir
    While NextDirectoryEntry(lDir)
      If DirectoryEntryType(lDir) = #PB_DirectoryEntry_File
        Type$ = "[File] "
        Size$ = " (Size: " + DirectoryEntrySize(lDir) + ")"
      Else
        Type$ = "[Directory] "
        Size$ = "" ; A directory doesn't have a size
      EndIf

      Debug Type$ + DirectoryEntryName(lDir) + Size$
    Wend
    FinishDirectory(lDir)
  EndIf
CompilerEndIf

Please let me know what you find and what I'm doing wrong here.
Thanks
JAL

Re: Is it bug or just me

Posted: Thu Dec 26, 2024 4:35 am
by BarryG
Change all "lDir.l" to just "lDir" so they're not Long type. The manual doesn't say to use a Long for ExamineDirectory().

Re: Is it bug or just me

Posted: Thu Dec 26, 2024 10:56 am
by SMaag
Change all "lDir.l" to just "lDir" so they're not Long type. The manual doesn't say to use a Long for ExamineDirectory().
In other words: you force 32Bit in system functions in your code and try to compile it in 64Bit.
If you want to code for x32/x64 always use Integer .i or without specification what is Integer for PB!

As said above: lDir or lDir.i whats same!

Re: Is it bug or just me

Posted: Thu Dec 26, 2024 11:04 am
by infratec
In general: if not explicit needed ... use always .i

Why you used .l :?:
Is this written in the help/ docs?

Re: Is it bug or just me

Posted: Fri Dec 27, 2024 9:32 am
by STARGĂ…TE
infratec wrote: Thu Dec 26, 2024 11:04 am In general: if not explicit needed ... use always .i

Why you used .l :?:
Is this written in the help/ docs?
Many code here in the forum and in the world wide web are very old.
They were written before PB 4.30, in a time when there was no 64 bit compiler and no integer (.i) type, just long (.l).
It was common practice at that time to define variables with ".l".

Re: Is it bug or just me

Posted: Fri Dec 27, 2024 1:12 pm
by Jal
Thank you all for your responses.

The variable type is the fix for my code samples.

As pointed out; 64bit support means having to assign correct variables types, in my case, this means going through old code and ensuring I've not defined long when its should be interger or something else.

The thought of leaving it to the compiler to decide, by simply not assigning has appeal, but it feel not right..

This probably explains the random failure within my older code, which fails in weird places, with a runtime "Invalid Memory Access" on the newer compiler.

I suspect that this code has lots of "var.l" defined as was good pratice to define varibles correctly.

On a side note: no error is issued at compile time for this type of code, but it fails to run, should a warning not be issued if we know that a result is going to a variable of the wrong type or is this not possible ?

Thank you again for all the help, much appreciated.
JAL