Arrays are resetted after calling SendSync with URLConnection

Hello!

I need some help as I’m truly unable to understand what’s going on here… I have a Thread sub class that saves images stored in an array and uploads them to a CDN giving them a proper name.

In a window I have a property, Self.tUploadImages that represents the Thread (cImageProcessor), once instantiated, I simply pass the array of the images with the corresponding names (let’s say 3 images and 3 names), to two properties defined inside the Thread sub class and then run it.

Self.tUploadImages = New cImageProcessor
Self.tUploadImages.images = imagesArray
Self.tUploadImages.names = namesArray
Self.tUploadImages.Start

Here’s the code in the Run event of the Thread, problem is that after the line “Call u.SendSync etc…” both images and names arrays inside the thread are reset to -1. Their values simply disappear! So first run of the For loop is ok, the next I get an OutOfBound Exeception.

I don’t really know why this is happening, can someone help?

macOS 14.6.1 and Xojo 2024R2.1

Var lastindex As Integer = images.LastIndex
Var u As URLConnection
Var f As FolderItem

For i As Integer = 0 To lastindex
  
  // ###########################
  // ### SAVE THE IMAGE TO A FILE
  // ###########################
  
  f = SpecialFolder.Temporary.Child(names(i))
  images(i).Save(f, Picture.Formats.JPEG, 90)
  
  // ################################
  // ### UPLOAD THE IMAGE ON THE CDN
  // ################################
  
  u = New URLConnection
  u.RequestHeader("Content-Type") = "image/jpeg"
  u.RequestHeader("Content-Length") = f.Length.ToString
  u.SetRequestContent(f.ImageToString, "image/jpeg")
  
  Call u.SendSync("PUT", "https://cdn.myserver.com/"+f.Name, 100)
  
  // ######################
  // ### DELETE IMAGE FILE
  // ######################
  
  f.Remove

Next

ImageToString is a simple function that extends a FolderItem

Public Function ImageToString(Extends f As FolderItem) As String
  Var t As TextInputStream = TextInputStream.Open(f)
  Var s As String = t.ReadAll()
  t.Close
  
  Return s
End Function

Thanks!

Something outside of your thread is resetting the arrays back to empty. Arrays are considered objects and as such are assigned by reference, not by value, so if you call imagesArray.ResizeTo(-1) you are resetting the array that the thread refers to.

More than likely you want to make a copy of the array before assignment. Rather than Self.tUploadImages.images = imagesArray do something like:

Self.mtUploadImages.images.ResizeTo(imagesArray.LastIndex)
For Idx As Integer = 0 To imagesArray.LastIndex
  Self.mtUploadImages.images(Idx) = imagesArray(Idx)
Next

Repeat with namesArray. The images inside the array will still be references, but that’s likely not an issue. Now you can resize imagesArray without affecting Self.mtUploadImages.images since they are different objects/arrays.

Thanks a lot Thom, yes I indeed do reset the array in the window after running the thread, didn’t knew the variables inside the thread may be affected by that. Learnt something new today, thanks!