www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there anything fundamentally wrong with this code?

reply WhatMeWorry <kheaser gmail.com> writes:
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
parent reply Johan Engelen <j j.nl> writes:
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
next sibling parent reply WhatMeWorry <kheaser gmail.com> writes:
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.
Feb 03
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Feb 03
parent reply aberba <karabutaworld gmail.com> writes:
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?
Feb 04
parent Adam D. Ruppe <destructionator gmail.com> writes:
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
prev sibling parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
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
parent Jonathan M Davis via Digitalmars-d-learn writes:
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
Feb 05