InvalidMemoryOperationError when calling functions from destructors

Volfram lonewolf325 at gmail.com
Thu Apr 25 08:50:26 PDT 2013


I've run into a problem which I'd like to hope is a bug, but 
let's see if we can figure out if I'm doing something stupid 
first, eh?

When a destructor calls a function from another module, I get an 
InvalidMemoryOperationError.  When a destructor calls a function 
from another class in the same module, and that function calls 
the function I was trying to call initially, everything seems to 
go fine.  When a destructor calls a function in the same class, 
which calls a function in a different module, I get the 
InvalidMemoryOperationError again.

This may have to do with garbage collector subtleties I'm not 
familiar with.

Example:

File alpha.d
module alpha;

import beta;

class AlphaClass
{
     BetaClass bc;
     Alpha2Class a2c;

     void cleanup()
     {
         bc.cleanup();//if this is called from the destructor, 
expect an error.
     }

     public:
     this()
     {
         bc = new BetaClass();
         a2c = new Alpha2Class(bc);
     }
     ~this
     {
         bc.cleanup();//this will cause an error.
         a2c.cleanup();//this works fine
         cleanup();//this will cause an error.
     }
}

class Alpha2Class
{
     BetaClass bc;

     void cleanup()
     {
         bc.cleanup();
     }

     public:
     this(BetaClass initbc)
     {
         bc = initbc;
     }
}

File beta.d

module beta;

class BetaClass
{
     public:
     this()
     {
         //do something
     }
     void cleanup()
     {
         //clean up after the bosses
     }
}

Further info can be provided if necessary.


More information about the Digitalmars-d mailing list