digitalmars.D.learn - Is there anything fundamentally wrong with this code?
- WhatMeWorry (38/38) Feb 03 2017 The code below compiles fine but I always get a run time abort
- Johan Engelen (4/34) Feb 03 2017 The error is in this line. Instead of assigning to the `postProc`
- WhatMeWorry (4/10) Feb 03 2017 Thank you so much. This is where I deserve a big Duh. I guess
- =?UTF-8?Q?Ali_=c3=87ehreli?= (22/33) Feb 03 2017 No matter how experienced, these happen to most programmers. :-/
- aberba (3/18) Feb 04 2017 Most D devs ignore the "this" in their code and that has
- Adam D. Ruppe (3/5) Feb 04 2017 I actually usually use `this` now exactly to be explicit against
- Era Scarecrow (3/6) Feb 04 2017 Wasn't the compiler suppose to warn you when you are shadowing
- Jonathan M Davis via Digitalmars-d-learn (5/11) Feb 05 2017 Local variables cannot shadow other local variables. All other levels of
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
Feb 03 2017
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); ... } ... }
Feb 03 2017
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: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.[...]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.[...]
Feb 03 2017
On 02/03/2017 11:43 AM, WhatMeWorry wrote:On Friday, 3 February 2017 at 18:37:15 UTC, Johan Engelen wrote: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 } } AliOn Friday, 3 February 2017 at 17:20:43 UTC, WhatMeWorry wrote: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.[...]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.[...]
Feb 03 2017
On Friday, 3 February 2017 at 22:34:31 UTC, Ali Çehreli wrote:On 02/03/2017 11:43 AM, WhatMeWorry 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?On Friday, 3 February 2017 at 18:37:15 UTC, Johan Engelen 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 } } AliOn Friday, 3 February 2017 at 17:20:43 UTC, WhatMeWorry wrote:[...]
Feb 04 2017
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.
Feb 04 2017
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?
Feb 04 2017
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:Local variables cannot shadow other local variables. All other levels of shadowing are allowed. - Jonathan M DavisThe 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?
Feb 05 2017