C++ for iphone crash
Im developing a game engine for iphone in C++ but im getting a crash with my Matrix class. I hope you can see whats wrong because i stare my self blind on the crash.
/*
* Copyright (c) 2010 Johnny Mast
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "Matrix.h"
Matrix::Matrix() {
/*
** if this constuctor has been used you should call
** setWidth and 开发者_StackOverflow社区setHeight before using this class.
** use of this constructor is not reconmended and it
** will producse rended code.
*/
matrixwidth = 0;
matrixheight = 0;
}
Matrix::Matrix(int width, int height) {
matrixwidth = width;
matrixheight = height;
setupMatrix();
}
void Matrix::setWidth(int width) {
matrixwidth = width;
setupMatrix();
}
void Matrix::setHeight(int height) {
matrixheight = height;
setupMatrix();
}
void Matrix::mark(int x, int y, int value) {
NSLog(@"Marking");
//matrix[x][y] = value;
NSLog(@"Marked");
}
int Matrix::get(int x, int y) {
return matrix[x][y];
}
#pragma mark -
#pragma mark Private functions from here.
void Matrix::setupMatrix() {
if (matrixwidth > 0 && matrixheight > 0) {
for (int w = 0, h = 0; w < matrixwidth, h < matrixheight; w++, h++) {
matrix[w][h] = 0;
}
}
}
The crash is in setupMatrix().
The problem is your loop:
for (int w = 0, h = 0; w < matrixwidth, h < matrixheight; w++, h++)
{
matrix[w][h] = 0;
}
The comma operator is not doing what you expect. This loops over the diagonal of the matrix and stops only when h < matrixheight
. You need to write something like:
for (int i=0; i<width; ++i)
for (int j=0; j<height; ++j)
matrix[i][j] = 0;
You have one visible problem and one invisible problem. First the invisible one.
The fact that you're crashing in setupMatrix
is almost certainly because you haven't allocated enough (or any) space for the matrix
array. We'll need to see where that's done to be certain but it's a fairly safe bet.
One possibility, if you're using a dynamic array, is to create it in the constructors thus (after setting up width and height members):
matrix = 0; // for default constructor.
setupMatrix();
Then, in, setupMatrix
, create the actual matrix, something like (untested):
if (matrix != 0) delete[] matrix; // to delete current.
if (matrixwidth * matrixheight == 0)
matrix = 0; // none desired.
else
matrix = new int[matrixwidth][matrixheight]; // make new.
// Initialise matrix (see corrected code below)
keeping in mind that you may want to copy the old data in some cases.
The other problem is with your for
loop in that function. It only sets up the diagonals to 0, matrix[0][0]
, matrix[1][1]
and so on. It should be:
for (int w = 0; w < matrixwidth; w++) {
for (int h = 0; h < matrixheight; h++) {
matrix[w][h] = 0;
}
}
The other problem with the loop you had will be if you have non-square matrices. Since the value of the expression a,b
is b
, your loop:
for (int w = 0, h = 0; w < matrixwidth, h < matrixheight; w++, h++) {
will only exit when h >= matrixheight
. If your matrix is higher than it is wide, you may well be running off the end of your array, even if it is allocated correctly.
精彩评论