Silverfrost Forums

Welcome to our forums

Memory corruption when casting in .NET integration

21 Jun 2019 4:47 #23822

Hello,

I posted about an issue with memory corruption when working with value types in collection previously: https://forums.silverfrost.com/Forum/Topic/2716

But still to this day my work around is not accessing booleans in a collection directly... as after posting an example showing the representation of booleans read from a .NET array were changing randomly because of how Silverfrost was reading the memory I never received a reply... That example project is no longer available as I did not maintain the dropbox account hosting it.

However I have grown increasingly frustrated with memory corruption issues in the .NET integration and have now noticed simply casting to value types actually pointed to by a collection/array is enough to run into this...

You can clone the solution containing the demonstration code below from: https://github.com/alexhopeoconnor/ftn_generic_collections_example.git

But I have defined the following type in the solution including my FTN project:

public class DataType
    {
        public List<string> DataList
        {
            get;
            set;
        }

        public List<int> IntDataList
        {
            get;
            set;
        }

        public List<double> DoubleDataList
        {
            get;
            set;
        }

        public DataType()
        {
            DataList = new List<string>() { 'hello', 'this', 'is', 'a', 'test' };
            IntDataList = new List<int>() { 1, 2, 3, 4, 5, 640000 };
            DoubleDataList = new List<double>() { 1.1, 2.2, 3.3, 4.4, 5.5 };
        }
    }

In FTN if we were able to access non-wrapped generic types directly the following would compile:

! Initialize the dataType
    dataType = NEW@('Domain.DataType')

    ! Simple integration syntax won't work for generic types...... Won't even compile, need to wrap it as a non-generic type for it to work directly 
    ! even though it implements non-generic interfaces (IList) with the same name as the member I am trying to access...
    !c = dataType%DataList%Count

So I have tried to use a wrapper written using reflection in .NET and calling it in FTN, but discovered when trying to cast from System.Object to a value type like System.Int32 memory is corrupted, when trying to do System.Object to System.Double it simply crashes after trying access protected memory, the following posts also address this issue: https://forums.silverfrost.com/Forum/Topic/1687 http://forums.silverfrost.com/viewtopic.php?t=1963

So really I think there must be an issue with the compiler converting .NET types to 'intrinsic' (am I using that right?) FTN types, possibly something to do with the number of bytes it is reading from memory at the address in some situations because of the way these types are represented in different circumstances on the .NET side.

Please let me know if you need any further examples of these problems with the .NET integration and accessing generics or type conversion resulting in memory corruption.

21 Jun 2019 8:40 #23833

I'll take a look at this problem.

Please login to reply.