Advertisement

assertions!?

Started by July 12, 2000 12:35 AM
8 comments, last by skitzo_smurf 24 years, 5 months ago
What is an assertion failure? I am trying to debug a class im writing, and I am getting that WEIRD error. I am using a win32 console app to debug. Heres main() int main() { Stage stage; //just testin the default constructor showInfo(stage); } I only have 2 statements in showInfo so far(I add them as I go!) void showInfo(Stage stage) { cout << endl; cout << "Rows = " << stage.getRows() << endl; cout << "Cols = " << stage.getCols() << endl; } Here are the only parts of the class getting used class Stage { public: Stage() : rows(0), cols(0) { grid = 0; } int getRows(){ return rows; } int getCols(){ return cols; } private: int **grid; //dynamically resize the grid int rows; //num rows in grid int cols; //num cols in grid } ok, this is why I know something is weird. If I call showInfo() in main, I get an assertion failure. If I take the SAME code thats in the function and paste it into main...it works fine. WTF??? what does this mean? skitzo_smurf
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf
gawd im stupid! Please dont reply to this post now. I was calling my copy constructor when I made the function call to showInfo(). I read my post and it then became obvious to me.

sorry
skitzo_smurf
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf
Advertisement
An assertion statement specifies a condition at some particular point in your program. Assertion statements are useful for catching logic errors. If you set an assertion on a condition that must be true according to the logic of your program, the assertion has no effect unless a logic error occurs. For example, suppose you are writing a simulation of gas molecules in a container, and the variable numMols represents the total number of molecules. Obviously, this number cannot be less than zero, so you might include an assertion statement like this:

ASSERT(numMols >= 0);

This statement does nothing if your program is operating correctly. If a logic error has caused numMols to be less than zero, however, the statement halts the execution of your program and displays the Assertion Failed dialog box.

In your code, try delcaring your showInfo() function like this instead:

void showInfo(Stage& stage)
{
// Code here..
}

// CHRIS
// CHRIS [win32mfc]
yeah it does work passing by reference, but the mere fact that I cant pass by value means that something is wrong with my copy constructor. Thanks for the explanation because I have wondered about that for a while. I used to get that error when I used Andre''s bitmap loading function from Tricks of the Windows Game Programming Gurus. I still to this day dont know what was causing the error. Just out of curiosity....did anyone else get an assertion error using his bitmap loading function?

skitzo
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf
ok I just remembered something. I know I was getting an assertion failure with andre''s code when he attempted to deallocate memory to the old bitmap data. This line of code was causing the assertion failurePage 350 TOTWGPG)

if(bitmap->buffer)
free(bitmap->buffer);

Now my copy constructor isnt working for much of the same reason. Why doesnt the following work?

int main()
{
// - see above post for Stage class

Stage stage; //Stage() : rows(0), cols(0) { grid = 0; }

somePassByValueFunction(stage); //invokes copy constructor

}

//this line from the copy constructor causes the assertion
//failure

if(grid) //this always tests to true..why
//code to delete grid
Why does this evaluate to true even if grid is assigned the value 0?





"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."

words of,
skitzo_smurf
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf


Post your code for your copy ctor and maybe there will be a problem that is evident there.
Advertisement
    Stage::Stage(Stage &s){   //get own block of memory for the grid   newGrid(s.rows,s.cols);  //now copy the original grid into the new one  for(int i = 0; i < rows; i++)    for(int j = 0; j < cols; j++)      grid[ i ][ j ] = s.grid[ i ][ j ];}the call to newGrid causes the problem because the first thing it does is call deleteGrid() to ensure we dont leak memory. here's the code for deleteGrid()void Stage::deleteGrid(){   if(grid) //THIS IS THE PROBLEM...always tests to true    {        //even if I initialize grid to 0      for(int i = 0; i < rows; i++)         delete[] grid<i>; //delete each row      //now delete the pointer to the rows      delete[] grid;   }}    



"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."

words of,
skitzo_smurf



Edited by - skitzo_smurf on July 12, 2000 6:28:28 PM
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf
Trying to delete something that isn''t there will most definitely cause an assertion error. To avoid this, initialize all pointers to zero. Even if nothing is there it won''t cause a failure if you do this.

-BacksideSnap-
Thats what I dont understand backside. All I am doing is calling the default constructor which in turn initializes the only pointer, grid, to zero. The very chopped down class is listed in the first post. in my main function I'm doing this:

int main()
{
Stage someObject; //calls default constructor which
//initializes class member grid to 0

//then I invoke the copy constructor when I pass by value
//to this function
showStageInfo(someObject);

return(0);
}

So, I dont see how class member grid could equal anything BUT 0. If you look at the source code two posts up, I even test to see if grid has been initialized in the function deleteGrid(). Am I just missing something here?

thanks in advance,
skitzo_smurf

Edited by - skitzo_smurf on July 13, 2000 1:53:56 AM
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf
well, you were right backSideSnap! That pointer WAS missing initialization. Problem fixed.

thanks everyone!



"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."

words of,
skitzo_smurf
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf

This topic is closed to new replies.

Advertisement