NullReferenceException on push_back - c++

I am getting a nullReferenceException when using vector.push_back on a new object.
In the code segment you see I have the vector object as a pointer but I originally had it as a non-pointer, I changed it in my troubleshooting out of desperation.
I stepped through the instantiation of BasicSolver completely to make sure the were no issues in it and also separated the instantiation from the push_back to help show that push_back is where the problem is occurring.
InformedSolver1&2 are children of BasicSolver. vector is included in BasicSolver and therefore in puzzleSolver also.
#include "stdafx.h"
#include "puzzleSolver.h"
PuzzleSolver::PuzzleSolver()
{
solvers = new vector<BasicSolver*>();
}
void PuzzleSolver::createPuzzle(string input)
{ //hitting step-over
BasicSolver* temp = new BasicSolver(input);// no errors
solvers->push_back(temp); // nullReferenceException
solvers->push_back(new InformedSolver1(input));
solvers->push_back(new InformedSolver2(input));
}
That should be all the relevant information.
Let me know if you have any ideas as to what is causing this/ how to fix it!
Thanks.
Edit:
Added BasicSolver constuctor and dependency methods by comment request
also a little background: this is a Sudoku solver for an AI class
BasicSolver::BasicSolver(string input)
{
peers = new vector<Peer*>();
squares = new vector<Square*>();
unsolved = new vector<Square*>();
solved = new vector<Square*>();
createStructure(input);
original = input;
mistakes = 0;
}
void BasicSolver::createStructure(string input)
{
try
{
createEmptyStructure(peers, squares, unsolved);
//Parse the puzzle and assign input to squares
int numCharsToRead = MAX_SQUARES; //makes sure vector isn't outside its range
if (input.length() < MAX_SQUARES) //makes sure string isn't outside its range
numCharsToRead = input.length();
for (int i = 0; i < numCharsToRead; i++)
{
if(input[i] != '.')
insertValue(input[i], (*squares)[i], unsolved);
}
}
catch(exception e)
{
throw e;
}
}
void BasicSolver::createEmptyStructure(vector<Peer*> *workingPeers, vector<Square*> *workingSquares, vector<Square*> *workingUnsolved)
{
for (int i = 0; i < MAX_PEERS; i++)
{
workingPeers->push_back(new Peer());
}
for (int i = 0; i < 81; i++)
{
try
{
workingSquares->push_back(new Square('.'));
//Adding the square to its corresponding peers
(*workingPeers)[i / MAX_ROWS]->addSquare((*workingSquares)[i]); //adds the square into its appropriate row of peers
(*workingPeers)[(i % MAX_ROWS) + COL_OFFSET]->addSquare((*workingSquares)[i]); //adds the square into its appropriate column of peers
int tempBoxCol = (i % MAX_ROWS) / MAX_BOX_COLS; //returns the box column (0,1,2)
if ((i / MAX_ROWS) < BOX_ROW_WIDTH) //if its box is in the first row
{
(*workingPeers)[tempBoxCol + BOX_OFFSET]->addSquare((*workingSquares)[i]);
}
else if ((i / MAX_ROWS) < (2 * BOX_ROW_WIDTH)) //if its box is in the second row
{
(*workingPeers)[BOX_ROW_WIDTH + tempBoxCol + BOX_OFFSET]->addSquare((*workingSquares)[i]);
}
else //if the box is in the third row
{
(*workingPeers)[2 * BOX_ROW_WIDTH + tempBoxCol + BOX_OFFSET]->addSquare((*workingSquares)[i]);
}
}
catch(exception e)
{
throw e;
}
}
*workingUnsolved = *workingSquares;
}
Edit2:
Personal tests
add these lines:
vector<BasicSolver*>* test = new vector<BasicSolver*>();
test->push_back(temp);
before
solvers->push_back(temp);
and they execute fine, I also notice that during runtime solvers is listed as outside of scope even though it is a protected variable of BasicSolver.

The problem was that the calling source did not have PuzzleSolver properly instantiated.
Thanks to #TheDark for suggesting I check that!

Related

Linker causing seemingly random crashes?

EDIT: After some more trying and testing, it seems to set down to changing stack size everytime I change code and I want the program to run. If I don't change the stack size, the program seems to crash everytime after code change.
EDIT 2: Same seems to apply to both /HEAP and /STACK
A bit of and odd question, but as far as I can tell, based on some checking and testing.
On multiple projects (at some point) I've come across this same problem:
I change a bit of code, the program crashes.
I change stack size, the program doesn't crash(with the changed code), doesn't seem to matter if incrementing or decrementing stack size.
Seemingly as if there's a hidden randomized stack which, if below a certain value causing the program to crash (no error). I can cause the crash by testing different stack sizes.
On a recent project, the crash seems to "pinpoint" to the bit of code:
typedef unsigned int DINT;
template <typename LIST_ITEM>
struct LIST {
LIST() {
this->length = 0;
this->total = 0;
for (DINT i = 0; i < ENGINE_DATABASE_LIST_LENGTH_MAX; i++) {
//this->item[i] = { 0 };
this->existance[i] = 0;
}
};
~LIST() {
for (DINT i = 0; i < ENGINE_DATABASE_LIST_LENGTH_MAX; i++) {
//this->item[i] = { 0 };
this->existance[i] = 0;
}
this->length = 0;
this->total = 0;
};
DINT length, total;
LIST_ITEM item[ENGINE_DATABASE_LIST_LENGTH_MAX];
DINT existance[ENGINE_DATABASE_LIST_LENGTH_MAX];
DINT _set(LIST_ITEM item) {
for (DINT d = 0; d < ENGINE_DATABASE_LIST_LENGTH_MAX; d++) {
if (this->existance[d] == 0) {
this->item[d] = item;
this->existance[d] = 1;
this->length++;
this->total++;
return d;
}
}
return 0;
}
void _remove(DINT position = 0) {
this->item[position] = {};
this->existance[position] = 0;
LIST <LIST_ITEM> list = {};
DINT length = 0;
do {
if (this->existance[length] == 1) {
list._set(this->_get(length));
this->existance[length] = 0;
//this->item[l] = {};
}
length++;
} while (length < ENGINE_DATABASE_LIST_LENGTH_MAX);
this->_carry(list);
}
LIST_ITEM _get(DINT position = 0) {
return this->item[position];
}
void _carry(LIST <LIST_ITEM> list = {}) {
for (DINT d = 0; d < list.length; d++) {
if (list.existance[d] == 1) this->_set(list.item[d]);
}
}
void _deconstruct() {
/*
for (DINT i = 0; i < ENGINE_DATABASE_LIST_LENGTH_MAX; i++) {
if (this->existance[i] == 1) {
this->existance[i] = 0;
this->item[i] = { };
}
}
this->length = 0;
this->total = 0;
*/
this->~LIST();
}
};
Possible solution/fix:
void _remove(DINT position = 0) {
this->existance[position] = 0;
this->length--;
LIST <LIST_ITEM> *list = new LIST;
for (DINT a = 0; a < ENGINE_DATABASE_LIST_LENGTH_MAX; a++) {
if (this->existance[a] == 1) {
list->_set(this->item[a]);
if (list->length == this->length) break;
}
}
list->total = this->total;
*this = *list;
delete list;
}
If the max list length is set to 64, the program seems to run fine, no matter of stack, but at 128 the crashes start to happen, and if I do the above (change stack size by even just 1, the program runs fine again, until next change of code, then I change stack by 1 again and the program runs fine again).
There might be a "< 0" somewhere (which could also cause crashing), which I just can't seem to spot.
Please do point out.
Seem confusing? Please ask.

How to limit a decrement?

There is a initial game difficulty which is
game_difficulty=5 //Initial
Every 3 times if you get it right, your difficulty goes up to infinity but every 3 times you get it wrong, your difficulty goes down but not below 5. So, in this code for ex:
if(user_words==words) win_count+=1;
else() incorrect_count+=1;
if(win_count%3==0) /*increase diff*/;
if(incorrect_count%3==0) /*decrease difficulty*/;
How should I go about doing this?
Simple answer:
if(incorrect_count%3==0) difficulty = max(difficulty-1, 5);
But personally I would wrap it up in a small class then you can contain all the logic and expand it as you go along, something such as:
class Difficulty
{
public:
Difficulty() {};
void AddWin()
{
m_IncorrectCount = 0; // reset because we got one right?
if (++m_WinCount % 3)
{
m_WinCount = 0;
++m_CurrentDifficulty;
}
}
void AddIncorrect()
{
m_WinCount = 0; // reset because we got one wrong?
if (++m_IncorrectCount >= 3 && m_CurrentDifficulty > 5)
{
m_IncorrectCount = 0;
--m_CurrentDifficulty;
}
}
int GetDifficulty()
{
return m_CurrentDifficulty;
}
private:
int m_CurrentDifficulty = 5;
int m_WinCount = 0;
int m_IncorrectCount = 0;
};
You could just add this as a condition:
if (user words==words) {
win_count += 1;
if (win_count %3 == 0) {
++diff;
}
} else {
incorrect_count += 1;
if (incorrect_count % 3 == 0 && diff > 5) {
--diff
}
}
For example:
if(win_count%3==0) difficulty++;
if(incorrect_count%3==0 && difficulty > 5) difficulty--;
This can be turned into a motivating example for custom data types.
Create a class which wraps the difficulty int as a private member variable, and in the public member functions make sure that the so-called contract is met. You will end up with a value which is always guaranteed to meet your specifications. Here is an example:
class Difficulty
{
public:
// initial values for a new Difficulty object:
Difficulty() :
right_answer_count(0),
wrong_answer_count(0),
value(5)
{}
// called when a right answer should be taken into account:
void GotItRight()
{
++right_answer_count;
if (right_answer_count == 3)
{
right_answer_count = 0;
++value;
}
}
// called when a wrong answer should be taken into account:
void GotItWrong()
{
++wrong_answer_count;
if (wrong_answer_count == 3)
{
wrong_answer_count = 0;
--value;
if (value < 5)
{
value = 5;
}
}
}
// returns the value itself
int Value() const
{
return value;
}
private:
int right_answer_count;
int wrong_answer_count;
int value;
};
And here is how you would use the class:
Difficulty game_difficulty;
// six right answers:
for (int count = 0; count < 6; ++count)
{
game_difficulty.GotItRight();
}
// check wrapped value:
std::cout << game_difficulty.Value() << "\n";
// three wrong answers:
for (int count = 0; count < 3; ++count)
{
game_difficulty.GotItWrong();
}
// check wrapped value:
std::cout << game_difficulty.Value() << "\n";
// one hundred wrong answers:
for (int count = 0; count < 100; ++count)
{
game_difficulty.GotItWrong();
}
// check wrapped value:
std::cout << game_difficulty.Value() << "\n";
Output:
7
6
5
Once you have a firm grasp on how such types are created and used, you can start to look into operator overloading so that the type can be used more like a real int, i.e. with +, - and so on.
How should I go about doing this?
You have marked this question as C++. IMHO the c++ way is to create a class encapsulating all your issues.
Perhaps something like:
class GameDifficulty
{
public:
GameDifficulty () :
game_difficulty (5), win_count(0), incorrect_count(0)
{}
~GameDifficulty () {}
void update(const T& words)
{
if(user words==words) win_count+=1;
else incorrect_count+=1;
// modify game_difficulty as you desire
if(win_count%3 == 0)
game_difficulty += 1 ; // increase diff no upper limit
if((incorrect_count%3 == 0) && (game_difficulty > 5))
game_difficulty -= 1; //decrease diff;
}
inline int gameDifficulty() { return (game_difficulty); }
// and any other access per needs of your game
private:
int game_difficulty;
int win_count;
int incorrect_count;
}
// note - not compiled or tested
usage would be:
// instantiate
GameDiffculty gameDifficulty;
// ...
// use update()
gameDifficulty.update(word);
// ...
// use access
gameDifficulty.gameDifficulty();
Advantage: encapsulation
This code is in one place, not polluting elsewhere in your code.
You can change these policies in this one place, with no impact to the rest of your code.

Arrays giving me errors:: String subscript out of range; but everything seems to be in order

I've run into some problems with arrays, one while I was coding in Winsock and one in DirectX 11. In DirectX 11 its actually a texture array that I'm trying to release.
Here's the Winsock problem:
int retval;
retval = recv(hclientSocket, tempBuffer, sizeof(tempBuffer), 0);
if (retval == 0)
{
break; // Connection has been closed
}
else if (retval == SOCKET_ERROR)
{
throw ErrorHandler("Failed to receive due to socket");
}
else
{
Encyrption enc;
string done = enc.Cipher(tempBuffer, retval);
retval = retval * 3;
cout << retval; // it prints out 3
for (int i = 0; i < retval; i++) {
tempBuffer[i] = done[i]; //the error is being pointed here on the 6th time it runs through this, even though its only suppose to go through this 3 times
}
if (send(hclientSocket, tempBuffer, retval, 0) == SOCKET_ERROR)
throw ErrorHandler("Failed to send due to socket");
}
okay most of this code I got from a Winsock tutorial place, but I wanted to try a different encryption method.
Here's the call function, because originally intended to pass and return a string but this time I'm passing a char* and returning a string, which is converted in the above code.
The encryption takes in one character and turns it into a string of 3 for example a would become bca and c would become cba or something that's why I'm multiplying retval by 3. It prints out everything I want it to print out, but its giving an error after its done.
string pass = (string)message;
pass.resize(size);
for (int i = 0; i < size; i++) {
if (!isalnum(pass[i])) {
return "\n";
}
else {
return Cipher(pass);
}
}
Okay so here's the Directx11 problem
I recently learned how to use multitextures utilizing a texture array, and Im having trouble releasing it.
#define TEXTURE_ELEMENTS_COUNT 2
ID3D11ShaderResourceView* m_textures[TEXTURE_ELEMENTS_COUNT];
for (int i = 0; i <= TEXTURE_ELEMENTS_COUNT; i++) {
m_textures[i] = 0;
}
//some code here
for (int i = 1; i <= (TEXTURE_ELEMENTS_COUNT - 1); i++) {
m_textures[i]->Release(); //it throws an exception right here, but I can't figure out why, I tried change `i` to zero, but it still throws it.
m_textures[i] = 0;
}
Thanks for taking the time to look through my code, I have no idea what I'm doing wrong and arrays sometimes throw me off, because its suppose to start at zero and sometimes its hard for me to visualize. Anyway thanks for any input in advance.
Your element count is 2. Therefore you have an element at position 0 and at position 1.
So you have to start your loop at "i = 0" and end your loop after "i = 1". So start from 0 and run to "i < maxCount".
for (int i = 0; i < TEXTURE_ELEMENTS_COUNT; i++) { //FROM 0 TO i<MAXCOUNT
m_textures[i] = 0; // HERE YOU CREATE A NULLPTR TO EVERY SRV
}
//some code here
for (int i = 0; i < (TEXTURE_ELEMENTS_COUNT); i++) { //USE THE SAME LOOP
m_textures[i]->Release(); //IF THERE IS ALREADY A NULLPTR YOU HAVE AN INVALID ACCESS
m_textures[i] = 0;
}
Try to use the Safe_Release function if you use the SDK. Otherwise define it for yourself.
SAFE_RELEASE(m_textures[i])
There is an included test for nullptr:
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(x)
if(x != NULL)
{
x->Release();
x = NULL;
}
#endif
Good Luck

Trouble finding memory leak (deleting double pointer)

So I'm writing a program which uses a double pointer in some somewhat deep recursion, and I've got a huge memory leak which I can't find the source of. The way the program works is (and I'll post some sample code below) that there is a board object with a double pointer called "board" which is the real board (it's a connect 4 game) and another double pointer called "newBoard" which is what is used by a function called minimax() (outside of the Board class).
minimax() is recursive, stepping through each possible route the game could take, and since I don't want to edit the actual board at all, and I figured copying the entire "board" object thousands and thousands of times would be unncecessary, I thought that creating this "newBoard" pointer to copy the actual board would be the best option.
I have a function to delete newBoard, but it's not working like I'd like it to. Here are the relevant pieces of code:
minimax():
void Brain::minimax(Board board, int who, int currentCheck, int iter)
{
board.createNewBoard();
if (iter <= MAX_ITER)
{
for (int i = 0; i < w_; i++)
{
if (board.playMove(i, currentCheck))
{
if (winDetect(board, board.getDisc('c')))
{
if (iter == 0)
{
score[i] += 1000;
}
else
score[i] += (MAX_ITER - iter);
}
else if (winDetect(board, board.getDisc('p')))
{
if (iter == 1)
{
score[i] += 500;
}
else
score[i] -= (MAX_ITER - iter);
}
else if (!winDetect(board, board.getDisc('c')) && !winDetect(board, board.getDisc('p')))
{
if (currentCheck == board.getDisc('p'))
currentCheck = board.getDisc('c');
else if (currentCheck == board.getDisc('c'))
currentCheck = board.getDisc('p');
minimax(board, who, currentCheck, iter + 1);
}
}
}
}
}
createNewBoard():
void Board::createNewBoard()
{
newBoard = new int*[h_];
for (int i = 0; i < h_; i++)
newBoard[i] = new int[w_];
}
NB_delete():
void Board::NB_delete()
{
for (int i = 0; i < w_; i++)
delete[] newBoard[i];
delete[] newBoard;
}
I think that's all the relevant code, but if you think there might be more, let me know and I'll include it! Thanks in advance for any help.
EDIT: SOLVED
I had simply forgotten to call my NB_delete() function. Calling it fixed the program!
As far as I can see, newBoard is a member of class Board. Now minimax is called recursively and so is CreateNewBoard. That means: In CreateNewBoard you are overwriting newBoard in each successive recursive call! That's probably your memory leak.
If CreateNewBoard is to be called recursively, you need a separate instance of newBoard for each level of recursion. Or is newBoard a 2D array for that very reason and the h_ index denotes recursion depth? In that case you should not create a new instance of newBoard in iteration 2 ... h_.

Error: not all control paths return a value

I am writing two functions in a program to check if a string has an assigned numeric code to its structure array or if the given numeric code has an assigned string in the same structure array. Basically, if I only know one of the two, I can get the other. I wrote the following:
int PrimaryIndex::check_title_pos(std::string title) {
bool findPos = true;
if (findPos) {
for (int s = 1; s <= 25; s++) {
if (my_list[s].title == title) {
return s;
}
}
} else {
return -1;
}
}
std::string PrimaryIndex::check_title_at_pos(int pos) {
bool findTitle = true;
if (findTitle) {
for (int p = 1; p <= 25; p++) {
if (my_list[p].tag == pos) {
return my_list[p].title;
}
}
} else {
return "No title retrievable from " + pos;
}
}
However, it says not all control paths have a return value. I thought the else {} statement would handle that but it's not. Likewise, I added default "return -1;" and "return "";" to the appropriate functions handling int and string, respectively. That just caused it to error out.
Any idea on how I can keep this code, as I'd like to think it works but cant test it, while giving my compiler happiness? I realize through other searches that it sees conditions that could otherwise end in no returning values but theoretically, if I am right, it should work fine. :|
Thanks
In the below snippet, if s iterates to 26 without the inner if ever evaluating to true then a return statement is never reached.
if (findPos) {
for (int s = 1; s <= 25; s++) {
if (my_list[s].title == title) {
return s;
}
}
}