Dictionary Corruption

We are getting a very weird error when using a dictionary. Sometimes, the values become corrupted.
Here is an except from debugging the problem.

dim overalls(2) as string

dim d as new dictionary
dim pd as new mydictionary
dim d2 as new dictionary
dim d3 as new xojo.core.dictionary
me.gradedata = new mydictionary

dim overall as string = overalls(0)
dim cmyk as string = overalls(1)
dim spot as string = overalls(2)
me.gradedata.value("overall") = overall
d.value("overall") = overall
pd.value("overall") = overall
d2.Value("overall")="3"
d3.Value("overall")="3"

me.gradedata is a mydictionary class and is a subclass of dictionary. The others are just local variables for testing to compare.

overalls(2) is a string array containing (ā€œ3ā€,ā€œā€,ā€œ3ā€) and can be confirmed by looking in the IDE.

But, when I look at the dictionary, the values for the keys show as empty and the typeinfo of the value contains gibberish ā€˜UHļæ½ļæ½AVSIļæ½ļæ½Hļæ½ļæ½ļæ½!fHļæ½ļæ½Lļæ½ļæ½ļæ½fHļæ½Tļæ½Hļæ½8ā€™

We normally write the dictionary as a string to save it and have a loop to generate a string data for many of the types

dim attributestrings() as string
dim thiskey as variant
dim thisvalue as variant
for each thisentry as DictionaryEntry in d
  thiskey=thisentry.Key
  thisvalue=thisentry.Value
  
  Var ti As Introspection.Typeinfo = Introspection.GetType(thisvalue)
  
  if ti<>nil then
select case ti.name
case "string"
....
....
case "Folderitem"
  valuestring = encodehex(folderitem(thisvalue).SaveInfo(nil))
else
  if ti.IsPrimitive then
    //basic type
    attributestrings.Append(thiskey+chr(9)+EncodingToURLMBS(thisvalue))
  else
    break
    app.debugmessage("dictionary_string error mydictionary string for a type "+ti.fullname)
    valuestring="BADDATA"
  end if
end select

We get a breakpoint with it.fullname containing the above gibberish type when it goes wrong. Processing the same data again does not cause each time.

Back to the code aboveā€¦
d, pd, d2 and d3 are all local variables.
Inspecting them in the IDE shows that dictionary d and d2 are both corrupt. The value of ā€œoverallā€ is empty and has the gibberish ti.fullname. How this can happen to a local variable, I really canā€™t understand. d2 has ā€œ3ā€ directly allocated to it. d3, of type Xojo.Core.Dictionary is ok and contains a single entry with a key of ā€œoverallā€ and a value of ā€œ3ā€. Unfortunately, I canā€™t make mydictionary a subclass of Xojo.Core.Dictionary because there are many internal functions and plugins which require a ā€˜Dictionary classā€™, not ā€˜Xojo.Core.Dictionaryā€™

Has anyone seen anything like this before?

Iā€™m using Xojo2024r1.1 and Xojo2024r1. MBS 2023r4 and now 2024pr2 beta as a test

Regards,

Lee

Iā€™ve not seen this and canā€™t reproduce your results. Here is how I tested your code:

dim overalls() as string = array("3", "", "3")

dim d as new dictionary
'dim pd as new mydictionary
dim d2 as new dictionary
dim d3 as new xojo.core.dictionary
'me.gradedata = new mydictionary

dim overall as string = overalls(0)
dim cmyk as string = overalls(1)
dim spot as string = overalls(2)
'me.gradedata.value("overall") = overall
d.value("overall") = overall
'pd.value("overall") = overall
d2.Value("overall")="3"
d3.Value("overall")="3"

This is on a Mac (ARM).

The entries are fine in the debugger, so it sounds like something else is going on.

Create a sample, a complete one, showing the issue. Share it.

Thereā€™s no consistency in it failing for me. It always had worked on the first run. When the code calculates from the same job data for the second or third time it may go wrong. The local variable of type dictionary is corrupt, the local variable in Xojo.Core.Dictionary is ok. When it goes wrong, Itā€™s always the same error with the corrupted variable type.

Iā€™ve changed just that me.gradedata class to be Xojo.Core.Dictionary and so far itā€™s all working even after multiple runs. Thereā€™s still loads of other dictionaries in use elsewhere. Iā€™ve had to add a couple of Extensions to Xojo.Core.Dictionary for the loading/saving of dictionary values.

Regards,

Lee

Iā€™m testing on a Mac M2 with an intel desktop build. Due to other plugins, I canā€™t create a universal binary yet.

Why do you subclass the dictionary and not write extends or use a wrapper? I use dictionaries all over my app and have never seen such a corruption.

1 Like

I did have extensions, but I was adding so many I decided to subclass it. In the example it made no difference whether I used mydictionary or dictionary. It was only with Xojo.Core.Dictionary did I not get any corruption.

Is the corruption consistent? IOW, is it the same gibberish each time? If not, it sounds like something is releasing memory it shouldnā€™t be releasing.

Yes, the corruption is consistent. Typeinfo ti.fullname is always the same thing, as far as I can tell. Iā€™ve not looked at the binary, but the displayed string is the same.

I keep coming back to the local variables being wrong too, not just the one belonging to the overall class.

I can step line by line and see that the strings are ok, but directly after the d.value(ā€œoverallā€)=ā€œ3ā€ line the dictionary is bad when stepping through the code. Using ā€˜Step Inā€™ shows that there are no context switching and each line in the debug is run.

Regards,

Lee

This part is being run in the Main thread. There is a separate thread with access to the class (and the gradedata dictionary), but the thread is only a reader and at the time of corruption, itā€™s asleep. When inspected from the thread, the dictionary is similarly corrupt.

Regards,

Lee

Can you make a project to show it and maybe report as bug?

Iā€™ve read it as ā€œI canā€™tā€

Pretty much. Iā€™m not sure how I can re-create the problem in a small project. The post here was really if anyone else had come across a similar problem. Iā€™ve spent almost two weeks trying to diagnose what is going wrong and re-factoring code. Now Iā€™ve got it working (with Xojo.Core.Dictionary) Iā€™ll need to move onto something else, but Iā€™ll see what I can do.

In your Dictionary subclass, are you using a Constructor? Does your code call

Super.Constructor

Failing to do that can introduce subtle bugsā€¦

Hmm. interesting.

I have 2 constructors, one had super.constructor in it, the other one did not.

Iā€™ll add it and see.

It still doesnā€™t explain why the local dictionary(both dictionary and subclass) are wrong.

Regards,

Lee

Although Xojo.Core.Dictionary was working, I would need to go through all the code and replace any dictionary.lookup code as when trying to compare with a string Iā€™m getting a type mismatch for text and string.

I added the missing Super.Constructor to one of the constructors but it made no difference.

Regards,

Lee

1 Like

Did you check the encodings of the keys if they are string ?

Too bad, that would have been an easy explanation and fix.

Do you see the corruption only in the debugger or does it happen in a built app too? For example, what if you debug the info using system.debuglog() only? I could imagine this is a debugger bug only.

The keys look ok, itā€™s the values which are bad

1 Like

In the built app, we just get an immediate crash. The App closes with no message or crash log.