PBv620 x86 Linux - Problems with Prototype and libusb-1

Linux specific forum
PeDe
Enthusiast
Enthusiast
Posts: 284
Joined: Sun Nov 26, 2017 3:13 pm

PBv620 x86 Linux - Problems with Prototype and libusb-1

Post by PeDe »

PB v6.20 x86, LMDE 6

I use the libusb-1 with Windows 32/64-bit, and with Linux 64-bit and Raspberry PI 64-bit without any problems. When compiling, I can switch between prototypes with OpenLibrary() and ImportC. The assembler and C backend work without errors.

Under Linux 32-bit my program does not work with the prototypes and OpenLibrary() with the assembler backend.
There is a memory error 'Invalid memory access' at the first call of a function from the libusb-1. The program works with the C backend. The ImportC version also works with both backends.

Here in the example code, the error occurs when the 'Init()' function is ended.

There is a code with the libusb-1 from infratec:
https://www.purebasic.fr/english/viewtopic.php?p=633916
With the file 'libusb.pbi' there is exactly the same error in the 'libusb_init(*ctx)' procedure.

At the moment I don't understand where the error are coming from.

Peter

Code: Select all

EnableExplicit

#Prototype = 1 ; 1 for Prototype, 0 = for ImportC

CompilerIf #Prototype
	Prototype.l ptLibusb_init(*Context)
	Global libusb_init.ptLibusb_init
CompilerElse
	ImportC "-lusb-1.0"
	libusb_init.l(*Context)
	EndImport
CompilerEndIf


CompilerIf #Prototype
	Procedure.i LoadLibrary()
		Protected fReturn.i, iLib.i
		iLib = OpenLibrary(#PB_Any, "libusb-1.0.so.0")
		If iLib
			libusb_init = GetFunction(iLib, "libusb_init")
			fReturn = #True
		EndIf
		ProcedureReturn fReturn
	EndProcedure
CompilerEndIf


Procedure.i Init()
  Protected iError.l
  iError = libusb_init(#Null)
  ProcedureReturn iError
EndProcedure


Define iError.i

CompilerIf #Prototype
	If LoadLibrary()
		iError = Init()
		Debug iError
	EndIf
CompilerElse
	iError = Init()
	Debug iError
CompilerEndIf
Last edited by PeDe on Tue Feb 18, 2025 9:13 pm, edited 3 times in total.
User avatar
mk-soft
Always Here
Always Here
Posts: 6245
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PBv620 x86 Linux - Problems with Prototype/ImportC and libusb-1

Post by mk-soft »

Code: Select all

CompilerIf #Prototype
	Prototype.l ptLibusb_init(*Context)
	Global libusb_init.ptLibusb_init
CompilerElse
	ImportC "-lusb-1.0"
	  libusb_init.l(*Context)
	EndImport ; <----------------------
CompilerEndIf
I've already stumbled upon it ...

Test
Prototype works but import "-lusb-1.0" not found ...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PeDe
Enthusiast
Enthusiast
Posts: 284
Joined: Sun Nov 26, 2017 3:13 pm

Re: PBv620 x86 Linux - Problems with Prototype/ImportC and libusb-1

Post by PeDe »

Thank you, I have corrected the code above. The error with the prototype version remains of course. If the library cannot be opened, there is no error, the code is not executed.

Peter
PeDe
Enthusiast
Enthusiast
Posts: 284
Joined: Sun Nov 26, 2017 3:13 pm

Re: PBv620 x86 Linux - Problems with Prototype and libusb-1

Post by PeDe »

I have tested with another PC with Debian 12. It gives the same error with 'Invalid memory access'. It looks like an error in the assembler backend, with the 32-bit version under Linux.
I have to add that the CPUs are very old. An Intel Pentium M (2004) and an AMD C60 (2011), maybe it has something to do with that.

Peter
PeDe
Enthusiast
Enthusiast
Posts: 284
Joined: Sun Nov 26, 2017 3:13 pm

Re: PBv620 x86 Linux - Problems with Prototype and libusb-1

Post by PeDe »

Could this be a problem with pointer addresses? Are the addresses too large? The PCs only have 2 GB of memory.

Assembler- & C-Backend PB v6.20 x86 / Windows 7 x64 ( 16 GB RAM)
Executable type: Windows - x86 (32bit, Unicode, Thread, Purifier)
[Debug] Test - Address *uDebug = $39F870

Assembler- & C-Backend PB v6.20 x86 / LMDE 6 x86 ( 2 GB RAM)
Executable type: Linux - x86 (32bit, Unicode)
[Debug] Test - Address *uDebug = $BFAA8150 -> 3,215,622,480

Code: Select all

EnableExplicit

Structure uDebug
	i.i
EndStructure

Procedure Test(*uDebug.uDebug)
	Debug #PB_Compiler_Procedure + " - Address *uDebug = $" + Hex(@*uDebug, #PB_Long)
EndProcedure

Procedure Main()
	Protected uDebug.uDebug
	Test(@uDebug)
EndProcedure

Main()


I also get error messages in my program that addresses are less than 100,000, which is unusual. Are the addresses perhaps used as negative values?

Peter

Code: Select all

[Debug] 111:Err: DebugView::Out: No valid value was passed with the pointer *uDebugViewData (< 100,000).
[Debug] 111:Err: DebugView::Out: - Call from: LibUsb1::LoadLibrary - eEvent: 6
[Debug] 112:Err: DebugView::Out: No valid value was passed with the pointer *uDebugViewData (< 100,000).
[Debug] 112:Err: DebugView::Out: - Call from: LibUsb1::Init - eEvent: 6
[ERROR] LibUsb1-v07.pbi (Line: 1577)
[ERROR] Procedure stack has been corrupted.
Post Reply