Page 1 of 1

How to debug deeper at Invalid memory access?

Posted: Mon Sep 12, 2022 3:25 pm
by dige
Hi folks,

I use the freeimage.dll for faster loading images. It works very well and quickly so far.

However, with certain images in TIF format, it crashes with an invalid memory access.

I have now found out that this only happens within my program. If I load the TIF image only with the FreeImage Include (
CompilerIf #PB_Compiler_IsMainFile), there is no problem.

Now I would like to know why this memory access error occurs within my program and what else I can do to get more information about this error.

What would you do?

Re: How to debug deeper at Invalid memory access?

Posted: Mon Sep 12, 2022 3:33 pm
by mk-soft
Looks like a pointer error.
Do you also evaluate all returns for success.

You can also turn on the compiler option 'Enable Purifier' to find memory access errors.

Re: How to debug deeper at Invalid memory access?

Posted: Mon Sep 12, 2022 9:32 pm
by jacdelad
Where do I find said DLL, googling it sends me to...suspicious sites.

Re: How to debug deeper at Invalid memory access?

Posted: Mon Sep 12, 2022 9:50 pm
by mk-soft

Re: How to debug deeper at Invalid memory access?

Posted: Mon Sep 12, 2022 10:06 pm
by jacdelad
Ah great, thanks. Now, does anyone have an include or something? I'm not into C-Code at all...
Update: Found the documentation. That will be enough for me.

Re: How to debug deeper at Invalid memory access?

Posted: Tue Sep 13, 2022 11:49 am
by SPH
@dige

Already, on my "Karate anim" code, you had this memory problem.
Can you try this code and tell me if it works? :

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Code : Lecteur de polygones (datas)   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; PB6.0 - SPH(2022) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;EnableExplicit

;-CONSTANTS
Enumeration
  #MainWindow
  #OpenGLGadget
EndEnumeration

;These two GL constants are used for texture creation. Don't change their values.
#GL_BGR = $80E0
#GL_BGRA = $80E1

ExamineDesktops()
ddw=DesktopWidth(0)
ddh=DesktopHeight(0)

;********************* DPI *********************
echelle_xf.f=(1920/ddw)*DesktopResolutionX()
echelle_yf.f=(1080/ddh)*DesktopResolutionY()
;**********************************************

;-STRUCTURES
Structure Integer2
  X.i
  Y.i
EndStructure
Global.Integer2 WindowDim
WindowDim\X = ddw
WindowDim\Y = ddh


;-DEFINES
Define.i Event
Define.i WindowFlags = #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget


;-DECLARES
Declare Render()
Declare Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
Declare SetupOpenGL()
Declare SetupGLTexture(ImageHandle.i)


;-MAIN WINDOW
win=OpenWindow(#MainWindow, 0, 0,ddw,ddh, "Picasso_SPH",#PB_Window_Maximize|#PB_Window_BorderLess)
If win=0
;   Beep_(500,250) : Delay(150) : Beep_(500,250)
  MessageRequester("Erreur","OpenWindow() impossible")
  End
EndIf

screenGL=OpenGLGadget(#OpenGLGadget,0,0,ddw,ddh)
If screenGL=0
;   Beep_(500,250) : Delay(150) : Beep_(500,250)
  MessageRequester("Erreur","OpenGLGadget() impossible")
  End
EndIf


SetupOpenGL()

AddKeyboardShortcut(0,  #PB_Shortcut_Escape, 666) ; quitter

glOrtho_(0,ddw,ddh,0,-1,1)
glMatrixMode_(#GL_MODELVIEW)
glLoadIdentity_()
glClear_(0)

;*********************************************************************************************************************************

;;;;;;;;;;;
  SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
  
  timer=ElapsedMilliseconds()

glClearColor_(0,0,0, 1.0)
; ShowCursor_(0)
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
Repeat
  
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  
  
  ;*****
  Repeat
    Event = WindowEvent()
    
    Select Event
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            Resx = GetGadgetAttribute(1, #PB_OpenGL_MouseX)
            Resy = GetGadgetAttribute(1, #PB_OpenGL_MouseY)
            ;;;;;;;         
            Select EventType()
                ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;             
            EndSelect
        EndSelect
      Case #PB_Event_Menu
        Select EventMenu()
          Case 666
            timer=ElapsedMilliseconds()-timer
            ;MessageRequester("Timer", Str(timer)+" ms - "+Str(scene)+" frames",2)
            ;           Debug scene
            ;           Debug timer
            ; ShowCursor_(1)
            End
        EndSelect
    EndSelect
    
  Until Event = 0
  
  
  ;##############################################
  ;##############################################
  ;##############################################
  ;##############################################
  
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Restore sph_data

Read.w sph_nombre_poly
Read.w sph_cmb
; Debug sph_nombre
; Debug sph_cmb
sph_cmb_mem=sph_cmb

Dim sph_xx.w(sph_cmb)
Dim sph_yy.w(sph_cmb)
sph_la=0

Repeat
                      glBegin_(#GL_POLYGON);
                      glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
If sph_la<>0
;   Debug "==="
  sph_cmb_mem+sph_cmb
;  Debug sph_cmb_mem
  ReDim sph_xx.w(sph_cmb_mem)
  ReDim sph_yy.w(sph_cmb_mem)
EndIf

sph_xx(la)=sph_nombre
sph_yy(la)=sph_cmb

For i=1 To 4;sph_cmb
Read.w sph_xx(sph_la+i)  
Read.w sph_yy(sph_la+i)  
; Debug sph_xx(sph_la+i)
; Debug sph_yy(sph_la+i)
Next
                      glColor4f_(sph_xx(sph_la+1)/255,sph_xx(sph_la+2)/255,sph_xx(sph_la+3)/255,sph_xx(sph_la+4)/255)

For i=5 To sph_cmb-1
Read.w sph_xx(sph_la+i)  
Read.w sph_yy(sph_la+i)  
                      glVertex2f_(sph_xx(sph_la+i)/echelle_xf,sph_yy(sph_la+i)/echelle_yf);
Next
                      glEnd_()                      ; 

Read.w sph_xx(sph_la+i)  
Read.w sph_yy(sph_la+i)  
; Debug "=="
sph_cmb=sph_yy(sph_la+i)
; Debug "sph_cmb = "+Str(sph_cmb)
sph_la+i
Until sph_yy(sph_la)=0
;;;;;;;;;;;;;;;

 
  SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
  
timer=ElapsedMilliseconds()-timer
;Debug timer
timer=ElapsedMilliseconds()
 
ForEver

End


Procedure Render()
  
  ;Clearing buffers and resetting clear color to remove old graphics from the last frame.
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  ;  glClearColor_(0.2, 0.2, 0.2, 1.0)
  glClearColor_(0,0,0,1)
  
  ;## DRAWING TEXTURES/IMAGES
  ;First enable the Texture system.
  glEnable_(#GL_TEXTURE_2D)
  
  ;This procedure will create a quad and apply a texture to it.
  ;The Texture variable contains the texture created earlier using SetupGLTexture().
  Render2DQuad(Texture1, 0, 0, ImageWidth(Image1), ImageHeight(Image1), -2)
  ; Render2DQuad(Texture2, 0, 0, ImageWidth(Image2), ImageHeight(Image2), -1)
  
  ;After all the textures have been displayed disable the texture system.
  ;Otherwise it will conflict with the non texture graphics.
  glDisable_(#GL_TEXTURE_2D)
  
EndProcedure

Procedure Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
  
  ;The texture is first bound which tells OpenGL to use this texture for any future rendering.
  glBindTexture_(#GL_TEXTURE_2D, OGLTexture)
  glBegin_(#GL_QUADS)
  glColor4f_   (1,1,1,1)
  glNormal3f_  (0,0,1.0)
  glTexCoord2f_(1.0,1.0)
  glVertex3f_  (StartX+Width,StartY,Z)
  glTexCoord2f_(0.0,1.0)
  glVertex3f_  (StartX,StartY,Z)
  glTexCoord2f_(0.0,0.0)
  glVertex3f_  (StartX,StartY+Height,Z)
  glTexCoord2f_(1.0,0.0)
  glVertex3f_  (StartX+Width,StartY+Height,Z)
  glEnd_()
  
EndProcedure

Procedure SetupOpenGL()
  
  glMatrixMode_(#GL_PROJECTION)
  
  glOrtho_(0.0, WindowDim\X, WindowDim\Y, 0.0, -1000.0, 1000.0)
  
  glMatrixMode_(#GL_MODELVIEW)
  
  ; glEnable_(#GL_DEPTH_TEST)
  
  glEnable_(#GL_BLEND)
  glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)
  
EndProcedure


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;- DataSection
DataSection
sph_data:
Data.w 25,9,51,0,71,0,115,0,255,0,0,0,0,1079,1919,1079,1919,0
Data.w 0,22,66,0,89,0,153,0,255,0,235,0,156,551,165,601,296,814,430,849,484,900,726,902,711,883,1184,858,1236,829,1418,847,1463,847,1575,814,1689,599,1695,545,1650,320,1625,0
Data.w 0,17,64,0,110,0,239,0,255,0,373,0,264,244,236,326,261,498,291,554,427,730,679,844,1185,769,1516,516,1509,350,1401,203,1343,0
Data.w 0,17,0,0,138,0,255,0,255,0,389,0,304,233,291,355,344,522,506,612,653,722,714,740,1176,744,1517,513,1508,344,1394,188,1345,0
Data.w 0,13,36,0,39,0,59,0,255,0,67,960,213,887,274,892,333,967,1520,964,1644,1079,0,1079,0,966
Data.w 0,11,36,0,39,0,59,0,255,0,1919,816,1828,813,1775,834,1719,897,1607,1079,1919,1079
Data.w 0,10,36,0,39,0,59,0,255,0,1721,895,1525,951,1513,988,1621,1079,1712,1079
Data.w 0,9,51,0,71,0,115,0,255,0,341,834,347,649,377,650,403,889
Data.w 0,9,0,0,0,0,4,0,255,0,0,1008,0,1079,1919,1079,1919,1010
Data.w 0,23,0,0,0,0,4,0,255,0,402,610,416,705,530,960,685,981,638,691,599,610,631,588,1174,43,1033,10,828,10,716,41,646,163,555,233,517,348,440,411,422,497,389,517,379,554
Data.w 0,16,0,0,0,0,4,0,255,0,911,951,935,886,1213,134,1171,42,623,580,745,895,728,953,512,1079,914,1079,924,1007,927,986
Data.w 0,9,0,0,0,0,4,0,255,0,529,956,438,1018,738,1037,677,969
Data.w 0,21,0,0,0,0,4,0,255,0,949,945,930,864,1212,125,1223,155,1307,239,1358,339,1420,396,1253,584,1153,743,1121,892,1122,947,1171,964,1200,1079,914,1079,932,1008,931,965
Data.w 0,19,0,0,0,0,4,0,255,0,1278,621,1231,553,1420,397,1452,480,1483,489,1495,534,1460,619,1458,639,1442,771,1368,979,1367,1079,1205,1079,1220,974,1231,762
Data.w 0,11,0,0,0,0,4,0,255,0,1224,974,1169,1003,1156,1079,1409,1079,1404,1001,1360,970
Data.w 0,9,172,0,135,0,115,0,255,0,828,689,846,688,841,641,835,641
Data.w 0,9,172,0,135,0,115,0,255,0,912,705,901,706,907,665,913,665
Data.w 0,9,172,0,135,0,115,0,255,0,942,514,937,578,943,579,959,514
Data.w 0,9,172,0,135,0,115,0,255,0,817,502,828,535,833,536,833,504
Data.w 0,9,111,0,26,0,0,0,255,0,789,438,794,454,811,454,804,438
Data.w 0,9,111,0,26,0,0,0,255,0,967,425,984,425,978,441,963,440
Data.w 0,9,89,0,0,0,0,0,255,0,0,0,0,1079,12,1079,12,0
Data.w 0,9,89,0,0,0,0,0,255,0,0,0,1919,0,1919,15,0,15
Data.w 0,9,89,0,0,0,0,0,255,0,1919,0,1919,1079,1907,1079,1906,0
Data.w 0,9,89,0,0,0,0,0,255,0,1919,1079,0,1079,0,1067,1919,1065
Data.w 0,0
EndDataSection

Re: How to debug deeper at Invalid memory access?

Posted: Wed Sep 14, 2022 7:01 am
by dige
@SPH: Line 144 : Invalid memory access

Code: Select all

glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
Tested with PB6 x86/x64

Re: How to debug deeper at Invalid memory access?

Posted: Wed Sep 14, 2022 7:08 am
by dige
Now I've found out, it happens only with Freeimage v3.18 - with v3.17. it works everything.

Also tried a new wrapper for x64 use. There also no problems with v3.18. But I need the x86 one.
Next step, I'll convert the x64 wrapper to x86. May be this wrapper is better implementated..

@mk-soft: I use Purifier with the single step setting (PurifierGranularity(1, 1, 1, 1). But no problem is detected.
mk-soft wrote: Mon Sep 12, 2022 3:33 pm Looks like a pointer error.
Do you also evaluate all returns for success.

You can also turn on the compiler option 'Enable Purifier' to find memory access errors.

Re: How to debug deeper at Invalid memory access?

Posted: Wed Sep 14, 2022 4:37 pm
by SPH
What is freeimage and how does it relate to purebasic?

Re: How to debug deeper at Invalid memory access?

Posted: Wed Sep 14, 2022 9:35 pm
by jacdelad
SPH wrote: Wed Sep 14, 2022 4:37 pm What is freeimage and how does it relate to purebasic?
Like mk-soft wrote (viewtopic.php?p=589145#p589145): an external DLL for loading and processing various images.
Just as a sidenode: I downloaded the package but can't even load the DLL. I must be doing something terribly wrong...

Code: Select all

Debug OpenLibrary(0,"O:\FreeImage.dll")
always returns "0"...

Re: How to debug deeper at Invalid memory access?

Posted: Fri Sep 16, 2022 7:55 am
by dige
SPH wrote: Wed Sep 14, 2022 4:37 pm What is freeimage and how does it relate to purebasic?
Especially with gigapixel images like panoramas or 360VR images, loading of JPG formats is incredibly fast.

Re: How to debug deeper at Invalid memory access?

Posted: Fri Sep 16, 2022 9:15 pm
by SPH
What I don't understand is the moment when freeimage intervenes in my anim!?!!!