Separate dynamic object arrays for multiple instances of overlying object

Jonathan daemon512 at gmail.com
Thu Mar 14 10:21:41 UTC 2024


The following seems the be the minimum required for this problem 
to pop up.  I've changed the names to what I'm using in my code 
to make it more clear for me.

```
import std;
class SpectrumList
{
     int count = 2;
     Spectrum[] spectra;
}

class Spectrum
{
     PrecursorList precursorList = new PrecursorList();
}

class PrecursorList
{
  	int count = 3;
     Precursor[] precursors;
}

class Precursor
{
  	string a = "placeholder";
     string b = "loren ipsum";
}

void main()
{
    SpectrumList mySpectrumList = new SpectrumList;
    for(int j=0; j<mySpectrumList.count; ++j)
    {
    	    Spectrum mySpectrum = new Spectrum;
         writeln(mySpectrum.precursorList.precursors.length); // 
should always be 0
         assert(mySpectrum.precursorList.precursors.length == 0);
         for(int k=0; k<mySpectrum.precursorList.count; ++k)
         {
             Precursor myPrecursor = new Precursor;
             mySpectrum.precursorList.precursors ~= myPrecursor;
         }
         mySpectrumList.spectra ~= mySpectrum;
    }
}
```

Output from run.dlang.io:

```
0
3
core.exception.AssertError at onlineapp.d(32): Assertion failure
----------------
??:? [0x55676698b006]
??:? [0x55676698ac82]
??:? [0x5567669b11d6]
??:? [0x5567669927ef]
??:? [0x556766989d92]
onlineapp.d:32 [0x556766959f4c]
??:? [0x5567669924bc]
??:? [0x5567669923b6]
??:? [0x55676699220d]
/dlang/ldc-1.35.0/bin/../import/core/internal/entrypoint.d:42 
[0x55676695a1d1]
??:? [0x7efc3bed5d8f]
??:? __libc_start_main [0x7efc3bed5e3f]
??:? [0x556766959c54]
Error: /tmp/onlineapp-ae55ed failed with status: 1
```


More information about the Digitalmars-d mailing list