Is there anything fundamentally wrong with this code?
The code below compiles fine but I always get a run time abort with the error down below. Isn't the postProc at module scope so shouldn't the class instance always be around (ie not deallocated?) If it helps, this was translated from C++ code. Thanks. -file post_processor.d -- module post_processor; class PostProcessor { ... GLuint FBO; } -file game.d --- module game; PostProcessor postProc; // just a declaration (no memory allocation) class Game { ... void initGame(){ ... PostProcessor postProc = new PostProcessor(...); ... } ... void update(GLfloat deltaTime){ ... writeln("Right Before"); writeln("postProcesor.FBO = ", postProc.FBO); writeln("Right After); ... } ... } Right Before Program exited with code -1073741819 myapp exited with code 2
Re: Is there anything fundamentally wrong with this code?
On Friday, 3 February 2017 at 17:20:43 UTC, WhatMeWorry wrote: -file post_processor.d -- module post_processor; class PostProcessor { ... GLuint FBO; } -file game.d --- module game; PostProcessor postProc; // just a declaration (no memory allocation) class Game { ... void initGame(){ ... PostProcessor postProc = new PostProcessor(...); The error is in this line. Instead of assigning to the `postProc` at module scope, you are defining a new local variable and assigning to it. ... } ... void update(GLfloat deltaTime){ ... writeln("Right Before"); writeln("postProcesor.FBO = ", postProc.FBO); writeln("Right After); ... } ... }
Re: Is there anything fundamentally wrong with this code?
On Friday, 3 February 2017 at 18:37:15 UTC, Johan Engelen wrote: On Friday, 3 February 2017 at 17:20:43 UTC, WhatMeWorry wrote: [...] The error is in this line. Instead of assigning to the `postProc` at module scope, you are defining a new local variable and assigning to it. [...] Thank you so much. This is where I deserve a big Duh. I guess there is no way to to make this idiot proof. I'll print it out and hang it over my desk.
Re: Is there anything fundamentally wrong with this code?
On 02/03/2017 11:43 AM, WhatMeWorry wrote: On Friday, 3 February 2017 at 18:37:15 UTC, Johan Engelen wrote: On Friday, 3 February 2017 at 17:20:43 UTC, WhatMeWorry wrote: [...] The error is in this line. Instead of assigning to the `postProc` at module scope, you are defining a new local variable and assigning to it. [...] Thank you so much. This is where I deserve a big Duh. I guess there is no way to to make this idiot proof. I'll print it out and hang it over my desk. No matter how experienced, these happen to most programmers. :-/ Somebody else had a similar problem just the other day on this forum. The same problem somewhat commonly happens when one copy+pastes members into the constructor and assigns to them forgetting to remove the types: struct S { int i; int j; this(int a) { // Declarations pasted from the members int i = 42 + a;// meant 'i = 42 + a' (or this.i = ...) int j = 43 + a; } } Another related one is assigning to a parameter usually in the constructor: struct S { int i; this(int i) { i = i;// meant this.i = i } } Ali
Re: Is there anything fundamentally wrong with this code?
On Friday, 3 February 2017 at 22:34:31 UTC, Ali Çehreli wrote: On 02/03/2017 11:43 AM, WhatMeWorry wrote: On Friday, 3 February 2017 at 18:37:15 UTC, Johan Engelen wrote: On Friday, 3 February 2017 at 17:20:43 UTC, WhatMeWorry wrote: [...] ... Another related one is assigning to a parameter usually in the constructor: struct S { int i; this(int i) { i = i;// meant this.i = i } } Ali Most D devs ignore the "this" in their code and that has influenced me to do that often. Is it no prone to bugs?
Re: Is there anything fundamentally wrong with this code?
On Saturday, 4 February 2017 at 14:37:45 UTC, aberba wrote: Most D devs ignore the "this" in their code and that has influenced me to do that often. Is it no prone to bugs? I actually usually use `this` now exactly to be explicit against this kind of thing.
Re: Is there anything fundamentally wrong with this code?
On Friday, 3 February 2017 at 18:37:15 UTC, Johan Engelen wrote: The error is in this line. Instead of assigning to the `postProc` at module scope, you are defining a new local variable and assigning to it. Wasn't the compiler suppose to warn you when you are shadowing another variable? Or is that only with two local ones?
Re: Is there anything fundamentally wrong with this code?
On Sunday, February 05, 2017 02:19:35 Era Scarecrow via Digitalmars-d-learn wrote: > On Friday, 3 February 2017 at 18:37:15 UTC, Johan Engelen wrote: > > The error is in this line. Instead of assigning to the > > `postProc` at module scope, you are defining a new local > > variable and assigning to it. > > Wasn't the compiler suppose to warn you when you are shadowing > another variable? Or is that only with two local ones? Local variables cannot shadow other local variables. All other levels of shadowing are allowed. - Jonathan M Davis