Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Debug Assertion Failure

This thread is locked; no one can reply to it. rss feed Print
 1   2 
Debug Assertion Failure
Gatleos
Member #12,534
January 2011

You're checking the wrong bounds again.

Those bounds are consistent with the allocation, I just changed it earlier.

Anyway, I narrowed it down even more. The values in Game.tile[x][y] are all initialized in the constructor, and the debugger confirms this. However, after I pass the window variable to the Draw_Char function all three pointers immediately go bad.

Edgar Reynaldo said:

You're passing window by value, and not by reference. That means whatever you do to modify window will be done to a local copy and not to the Window you're passing to the function. It works in this case because the local copy of window has the same Tile** tile that the one passed to the function does.

Like you said, I'm passing by value and not reference. The only reason I do it that way is because there may be multiple Window variables to be passed to that function at different times. Could I still do that by passing by reference? That may be just what I need to do to fix this.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Gatleos said:

The only reason I do it that way is because there may be multiple Window variables to be passed to that function at different times. Could I still do that by passing by reference?

Each call to the function will have it's own local memory on the stack. No need to worry about cross contamination. Passing by reference& allows you to modify the object directly (which in this case, is what I believe you want to do). If you're going to have multiple threads calling Draw_Char on the same window then you could have problems.

I just figured out why you're having problems. When the local copy of window goes out of scope at the end of Draw_Char, it's destructor gets called. The first time it is passed to the function it's Tile** tile is still valid so that's why it doesn't fail the first time. When you pass in window again, the Tile** it has has already been destroyed, so that's why you get a memory access error. Using a Window& window will solve this problem.

Gatleos
Member #12,534
January 2011

Even closer. NOW, I get a compiler error about ambiguous functions. My function that draws strings of characters calls Draw_Char at the end of a loop, and according to the compiler more than one overloaded function matches its argument list.

Overloaded Draw_Char functions:

void Draw_Char(int code,int x,int y);
void Draw_Char(int code,int x,int y,const char *colorset,const char *backcolorset);
void Draw_Char(int code,Window& window,int x,int y);
void Draw_Char(int code,Window& windowcolor,int x,int y,const char *colorset,const char *backcolorset);

As you can see, none of them have the same arguments. This has to have something to do with changing the Window to a Window& in all the functions. Or, does it have to do with the fact that I'm passing the reference to a function from inside a function?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

That doesn't make sense.

Post the entire compiler error(s). And the line(s) it says are ambiguous.

Do your function declarations match your function defintions?

Gatleos said:

Or, does it have to do with the fact that I'm passing the reference to a function from inside a function?

No, there's nothing wrong with that. Just make sure you're only passing your Window by reference.

Gatleos
Member #12,534
January 2011

Never mind, I figured it out. Phew. Thank you very much for all the help. I never would have gotten this far without all that help. :)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

X-G
Member #856
December 2000
avatar

Please do note well: If your class allocates and frees memory in its constructor and destructor respectively, you must provide your own copy constructors and assignment operators, or you will have mysterious memory errors and crashes. The cause of your problem is, like I mentioned earlier, that you are making copies of the Window object and freeing the memory in its destructor while still trying to use it.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

 1   2 


Go to: