Memory Corruption Issue??

Daniel Kozak via Digitalmars-d digitalmars-d at puremagic.com
Wed Jan 20 06:11:40 PST 2016


V Wed, 20 Jan 2016 13:58:46 +0000
Bottled Gin via Digitalmars-d <digitalmars-d at puremagic.com> napsáno:

> >> Another workaround is to use GC.addRoot for dynamic allocated 
> >> data in Dynamic.proc
> >>
> >> void proc () {
> >>     import core.memory: GC;
> >>     dash.length = 32;
> >>     GC.addRoot(cast(void*)dash.ptr);
> >>     dash[] = '-';
> >> }  
> >
> > And another one is hold pointer to data:
> >
> > class Dynamic {
> >   static char[] space;
> >   static char[] dash;
> >   char* dash_ptr;
> >   void rehash () {
> >     static Hash hash ;
> >     hash = new Hash;
> >     hash.clear();
> >   }
> >   void proc () {
> >     import core.memory: GC;
> >     dash.length = 32;
> >     dash_ptr = dash.ptr;
> >     dash[] = '-';
> >   }
> > }  
> 
> Daniel, thanks for confirming the bug and for providing 
> workaround. The second workaround (saving the pointer) will not 
> work on my real project though. I have multiple threads and the 
> TLS variable will have a different pointer on each thread.
> 
> Also, can you please tell me how to addRoot an assoc array to GC. 
> It seems there is no ptr property available for assoc arrays.
> 
> Regards
> - Puneet
> 
> 
>

You can use cast(void *)aa;

something like this:

class Dynamic {
  static char[] space;
  static char[int] dash;
  void rehash () {
    static Hash hash ;
    hash = new Hash;
    hash.clear();
  }
  void proc () {
    import core.memory: GC;
    //GC.addRoot(cast(void *)dash); // not here
    {
        dash[i] = '-';
    }
    GC.addRoot(cast(void *)dash); // must be after allocation
  }
}


Be careful because you mast use addPtr after aa is initialized (you put
something to it)



More information about the Digitalmars-d mailing list