CloseWindow crash

Everything else that doesn't fall into one of the other PB categories.
Josepho
User
User
Posts: 65
Joined: Thu Oct 22, 2020 7:01 am

CloseWindow crash

Post by Josepho »

Im having a code that crash randomly at closewindow

I open the window using an if (openwindow) etc etc and the window appears, i have a button that makes some hard process and then closes the window and some times im getting a "invalid memory access"

If IsWindow(#editMapWindow)
CloseWindow(#editMapWindow)
EndIf

why if the first isWindow is true the program crashes at closewindow?

Im sorry cause i cant share the source as i would need to share all the program to be able to fix the issue. Also i wasnt able to reproduce it using a small code

Any body can think on any reason on why a code like that would crash? im using a mac x64 if that is helpful some how

Also i have seen that closing the window using the close button always works good, so maybe is there any way to close the window using something like a self function? maybe i will avoid the error
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: CloseWindow crash

Post by STARGÅTE »

Image

An "invalid memory access" not always relates to the function where it occurs.
For example, when CloseWindow() is called, also all the gadgets, menus, times, toolbars, bound events etc. are freed.
So it is likely that the error has another reason, long before the window is closed.

Try to use the Purifier, to determine memory overflows.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
kernadec
Enthusiast
Enthusiast
Posts: 146
Joined: Tue Jan 05, 2010 10:35 am

Re: CloseWindow crash

Post by kernadec »

hi, :wink:

Code: Select all

#editMapWindow = 0

If IsWindow(#editMapWindow)
CloseWindow(#editMapWindow)
EndIf
bye
Josepho
User
User
Posts: 65
Joined: Thu Oct 22, 2020 7:01 am

Re: CloseWindow crash

Post by Josepho »

STARGÅTE wrote: Thu Nov 10, 2022 6:26 am Image

An "invalid memory access" not always relates to the function where it occurs.
For example, when CloseWindow() is called, also all the gadgets, menus, times, toolbars, bound events etc. are freed.
So it is likely that the error has another reason, long before the window is closed.

Try to use the Purifier, to determine memory overflows.
Its probably something related to this, the problem is that is totally random, i dont know exactly what is causeing the issue. I enabled purefier but amybe im not able to understand how to work with it or i dont have any memory overflow as its not reporting anything.

Im going to paste the total code where the close window is called maybe someone is able to see anything im not seeing, but i dont know how usefull it would be without the rest of the code

Code: Select all

Procedure updateMap()
  
  Shared folderSelectedID
  Shared mapFiles()
  Shared MapTileIDSelctedArr()
  Shared mapLowerIMGSBack()
  Shared mapUpperIMGSBack()
  
  SelectElement(globalListMaps(),folderSelectedID)
  SelectElement(mapFiles(),globalListMaps()\fileData)
   
  globalListMaps()\file = GetGadgetText(#nameEditMap)
  
  actualX.i = mapFiles()\sizeX
  actualY.i = mapFiles()\sizeY
  
  newX.i = Val(GetGadgetText(#sizeXEditMap))
  newY.i = Val(GetGadgetText(#sizeYEditMap))
  
  
  chipsetAct.i = mapFiles()\chipset
  chipsetNew.i = GetGadgetState(#chipsetListEditMap)
  
  ;Debug "act : " + Str(chipsetAct) + " new: "+Str(chipsetnew)
  
  Debug "Updating map"

  
  If Not actualX = newX Or Not actualY = newY
    mapFiles()\sizeX = newX
    mapFiles()\sizeY = newY
    
;     NewList mapUpperIMGSaux.mapTile()
;     NewList mapLowerIMGSaux.mapTile()
    
    NewList layerGroupsAux.layerGroup()
    
    counter = 0
    ForEach globalListLayerG()
      If counter = 0
        AddElement(layerGroupsAux())
        createMapEx(layerGroupsAux()\layer(),0,newX,newY) 
      Else
        AddElement(layerGroupsAux())
        createMapEx(layerGroupsAux()\layer(),1,newX,newY)
      EndIf 
      counter = counter + 1
    Next
    
;     For i = 1 To 4
;       AddElement(layerGroupsAux())
;       createMapEx(layerGroupsAux()\layer(),1,newX,newY)
;     Next
    
    Debug "Maps created Ex"
    ;createMap(mapLowerIMGSaux(),mapUpperIMGSaux(),newX,newY)
    
    sizeSelX = Min(actualX, newX)
    sizeSelY = Min(actualY, newY)
    
    NewList layerCopy.sArray2()
    
    layercount = 0
    
    ForEach mapFiles()\layerGroups()
      
      Debug "creating layer: " + Str(layercount)
      layercount = layercount + 1
      
      AddElement(layerCopy())
      Dim layerCopy()\arr(sizeSelX-1,sizeSelY-1)
      
      For i = 0 To ArraySize(layerCopy()\arr(),2)
        For z = 0 To ArraySize(layerCopy()\arr(),1)
          
          idsel = (mapSelectYini+i) * actualX
          idsel = idsel + mapSelectXini+z
          
          SelectElement(mapFiles()\layerGroups()\layer(),idsel)
          layerCopy()\arr(z,i) = mapFiles()\layerGroups()\layer()\intelliID
          
        Next z
      Next i 
    Next

    SelectElement(layerGroupsAux(),0)
    
    Debug "drawing tiles"

    
    Debug "finished tileS"
    
    counter = 0
    ForEach layerCopy()
      For i = 0 To ArraySize(layerCopy()\arr(),2)
        For z = 0 To ArraySize(layerCopy()\arr(),1)
          
          SelectElement(layerGroupsAux(),counter)
          drawMapTileFinal(z,i,1,layerCopy()\arr(z,i),layerGroupsAux()\layer())
          ;drawMapTileFinal(z,i,1,MapTileIDSelctedArrUpper(z,i),mapUpperIMGSaux())
        Next z
      Next i
      
      counter = counter +1
    Next
    
    Debug "layer copy created"
    
    counter = 0
    ForEach layerGroupsAux()
      SelectElement(mapFiles()\layerGroups(),counter)
      CopyList(layerGroupsAux()\layer(),mapFiles()\layerGroups()\layer())
      counter = counter +1
    Next
    
    Debug "LAyer copy copied"

    refreshMap()
;     
  EndIf
  
  If Not chipsetAct = chipsetNew And chipsetNew >= 0
    Debug "updating chipsets"
    mapFiles()\chipset = chipsetNew
    loadChipset(mapFiles()\chipset)
    refreshMap()
    selectingMapZoom(globalZoomMap)
    Debug "chipsets updated"
  EndIf
  
  folderRefresh()
  Debug "folder refreshed"
  
  DisableWindow(#mainWindow,#False)
  
  Debug "enabling window"
  
  Delay(1000)
  
  Debug "after delay"
  
  If IsWindow(#editMapWindow) 
    CloseWindow(#editMapWindow) 
  EndIf
  
  
EndProcedure
All the debug messages are triggered without any issue and still crash in the closewindow

This is the code that creates the window

Code: Select all

Procedure editMapWindow(mode.i = 0)
  
  Shared folderSelectedID
  Shared mapFiles()
  Shared mapNextID
  
  SelectElement(globalListMaps(),folderSelectedID)
  SelectElement(mapFiles(),globalListMaps()\fileData)
  
  DisableWindow(#mainWindow,#True)
  
  editMap.s = "Edit Map"
  applyChanges.s = "Apply Changes"
  nameTxt.s = globalListMaps()\file
  
  If mode = 1
    editMap = "Create Map"
    nameTxt = "map_" + Str(mapNextID+1)
  EndIf    
  
  If OpenWindow(#editMapWindow,0,0,400,400,editMap,#PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
    
    StickyWindow(#editMapWindow,#True)
    
    displacementY.i = 10
    displacementX.i = 50
    
    TextGadget(#PB_Any, 10, displacementY, displacementX, 24, "Name")
    StringGadget(#nameEditMap, displacementX+10, displacementY, 200, 24, nameTxt)
    displacementY = displacementY + 26
    
    TextGadget(#PB_Any, 10, displacementY, displacementX, 24, "Size X")
    StringGadget(#sizeXEditMap, displacementX+10, displacementY, 200, 24, Str(mapFiles()\sizeX))
    displacementY = displacementY + 26
    
    TextGadget(#PB_Any, 10, displacementY, displacementX, 24, "Size Y")
    StringGadget(#sizeYEditMap, displacementX+10, displacementY, 200, 24, Str(mapFiles()\sizeY))
    displacementY = displacementY + 36
    
    TreeGadget(#chipsetListEditMap, 10, displacementY, 120, 200)
    
    Define RowHeight.CGFloat
    CocoaMessage(@RowHeight, GadgetID(#chipsetListEditMap), "rowHeight")
    RowHeight = 17.0
    CocoaMessage(0, GadgetID(#chipsetListEditMap), "setRowHeight:@", @RowHeight)
    
    ForEach globalChipsetAssets()
      AddGadgetItem(#chipsetListEditMap,-1,globalChipsetAssets()\name)
    Next
    
    SetGadgetState(#chipsetListEditMap,mapFiles()\chipset)
    
    CanvasGadget(#chipsetCanvasEditMap, 140, displacementY, 250, 200)
    SelectElement(globalChipsetAssets(),mapFiles()\chipset)
    editMapRefresh(globalChipsetAssets()\img)
    
    displacementY = displacementY + 200 + 15
    
    Select mode
      Case 0:
        ButtonGadget(#applyButtonEditMap, 10, displacementY, 160, 34, "Apply Changes")
        
      Case 1
        ButtonGadget(#createNewmapEditMap, 10, displacementY, 160, 34, "Accept")
        
    EndSelect
    
    
  EndIf
  
  
EndProcedure
Josepho
User
User
Posts: 65
Joined: Thu Oct 22, 2020 7:01 am

Re: CloseWindow crash

Post by Josepho »

Looks like @STARGÅTE was right and the issue has something to do in the time the closewindows was removing one of the gadgets, more specificaly it was about the canvasgadget, by adding a

freegadget(canvas)

before the closewindow it appears the crash is gone, now im wondering if this is a bug or not, cause its supposed the closewindow function should do this right? should I free any canvas of any windows before closing it?
Post Reply