CosmosOS / Cosmos

Cosmos is an operating system "construction kit". Build your own OS using managed languages such as C#, VB.NET, and more!
https://www.goCosmos.org
BSD 3-Clause "New" or "Revised" License
2.93k stars 553 forks source link

GC collects objects that it should not #2693

Closed GoldenretriverYT closed 1 year ago

GoldenretriverYT commented 1 year ago

Area of Cosmos - What area of Cosmos are we dealing with?

Garbage Collector

Expected Behaviour - What do you think that should happen?

Buffer should not be collected

Actual Behaviour - What unexpectedly happens?

Buffer gets collected unless the TextField.VisibleIncludingParents is true the whole time. I assume this turning to false somehow makes the GC think that references to ScrollableTextFrame.Buffer have been removed, despite this not being the case.

Reproduction - How did you get this error to appear?

  1. Clone this repo: https://github.com/GoldenretriverYT/nxtlvlOS
  2. Start the kernel - click any keyboard layout
  3. The input fields will look corrupted, this is because the Buffer of ScrollableTextFrame (a child of TextField) gets collected by the GC
  4. This can be proven by commenting out the 3 lines here: https://github.com/GoldenretriverYT/nxtlvlOS/blob/31ee4b1b7346d3006a9c660a9cad9f5ad064916c/nxtlvlOS/Kernel.cs#L76
  5. If you have commented them out, the input fields will look perfectly fine.

Version - Were you using the User Kit or Dev Kit? And what User Kit version or Dev Kit commit (Cosmos, IL2CPU, X#)?

Devkit latest

(sorry for the bad reproduction steps but this seems like a very specific edge case)

terminal-cs commented 1 year ago

If you are using pointers with heap allocation, you need to increment the reference count manually

GoldenretriverYT commented 1 year ago

I am not using pointers - everything is managed

But manually incremeting sounds like something that could be a workaround for now

quajak commented 1 year ago

Might be fixed by https://github.com/CosmosOS/Cosmos/pull/2604 . There was an issue with large enough objects not being marked as in use

GoldenretriverYT commented 1 year ago

Yeah seems like that fixed it

GoldenretriverYT commented 1 year ago

and my os no longer leaks memory definitely one of the top 3 cosmos prs ever amazing job quajak