www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Inexplicable invalid memory operation when program terminates

reply pineapple <meapineapple gmail.com> writes:
I have a program which I have stripped down to a single offending 
line which, when present in my program, causes an invalid memory 
operation to occur after main has evaluated:

     import mach.sdl.window;
     void main(){
         auto win = new Window(300, 300);
     }

The program is a bit larger than that, but I do not get the error 
when I comment out that instantiation and the things dependent on 
it.

I commented out the body of the constructor that is being called, 
and I still get the error.

Am I missing something or is this an obnoxious bug with the GC?
Sep 05 2016
next sibling parent Lodovico Giaretta <lodovico giaretart.net> writes:
On Monday, 5 September 2016 at 17:33:17 UTC, pineapple wrote:
 I have a program which I have stripped down to a single 
 offending line which, when present in my program, causes an 
 invalid memory operation to occur after main has evaluated:

     import mach.sdl.window;
     void main(){
         auto win = new Window(300, 300);
     }

 The program is a bit larger than that, but I do not get the 
 error when I comment out that instantiation and the things 
 dependent on it.

 I commented out the body of the constructor that is being 
 called, and I still get the error.

 Am I missing something or is this an obnoxious bug with the GC?
InvalidMemoryOperationError is raised when you try to allocate memory inside a destructor called by the GC. I see from your repository that the destructor of Window uses a function called log defined by yourself. That function uses writeln internally. writeln is not nogc, so it may be that that call to log in the destructor is actually allocating memory.
Sep 05 2016
prev sibling next sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Mon, Sep 05, 2016 at 05:33:17PM +0000, pineapple via Digitalmars-d-learn
wrote:
 I have a program which I have stripped down to a single offending line
 which, when present in my program, causes an invalid memory operation
 to occur after main has evaluated:
 
     import mach.sdl.window;
     void main(){
         auto win = new Window(300, 300);
     }
 
 The program is a bit larger than that, but I do not get the error when
 I comment out that instantiation and the things dependent on it.
 
 I commented out the body of the constructor that is being called, and
 I still get the error.
 
 Am I missing something or is this an obnoxious bug with the GC?
[...] What does the destructor do? T -- "The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts." -- Bertrand Russell. "How come he didn't put 'I think' at the end of it?" -- Anonymous
Sep 05 2016
prev sibling parent reply pineapple <meapineapple gmail.com> writes:
On Monday, 5 September 2016 at 17:33:17 UTC, pineapple wrote:
 Am I missing something or is this an obnoxious bug with the GC?
Oh, I've been trying to figure this out on and off for days and of course five minutes after I post I fix the problem. I'm not really sure why, but it did fix it. In the example the window class destructor was being called. I found that if I did `delete win;` at the end there it worked fine, but otherwise the GC was tripping up? I removed a console output statement and rewrote a thing using a foreach loop instead of a filter range and it stopped complaining. I'm realizing now, too, that the reason I was getting the same error in commits long before I added that particular thing the GC disliked was an entirely different one. That was not fun to debug. On Monday, 5 September 2016 at 17:42:18 UTC, Lodovico Giaretta wrote:
 InvalidMemoryOperationError is raised when you try to allocate 
 memory inside a destructor called by the GC. I see from your 
 repository that the destructor of Window uses a function called 
 log defined by yourself. That function uses writeln internally. 
 writeln is not  nogc, so it may be that that call to log in the 
 destructor is actually allocating memory.
That was in fact part of the problem
Sep 05 2016
parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Mon, Sep 05, 2016 at 05:48:11PM +0000, pineapple via Digitalmars-d-learn
wrote:
[...]
 In the example the window class destructor was being called. I found
 that if I did `delete win;` at the end there it worked fine, but
 otherwise the GC was tripping up? I removed a console output statement
 and rewrote a thing using a foreach loop instead of a filter range and
 it stopped complaining.
[...] The problem is caused by trying to allocate memory inside the dtor when the dtor is invoked by the GC. The delete operator is being deprecated, but IIRC it doesn't involve a GC collection run, so the problem is hidden. Basically, class dtors should never do anything involving the GC, and should not depend on any reference member variables still being valid. T -- Never wrestle a pig. You both get covered in mud, and the pig likes it.
Sep 05 2016