I had a program working good (without prompting errors) using references, but I decided to use a pointer-to-pointer array allocated on virtual memory, because I can use a variable as the size of the array.
The error prompts when I break the while (m_Window.isOpen()) loop, in other words when I close the game window and the game is finished. I have noticed that the program breaks when I try to erase the virtual memory in the Engine::cleanVirtualMemory() function. I have noticed that it is there because I have put two flags (cout << "running1" << endl; and cout << "running2" << endl) and I can show just the first flag.
Then it prompts a window with the following message:
HEAP CORRUPTION DETECTED after normal block.. CRT detected that the application wrote to memory after end of heap buffer
main.cpp
#include "Engine.h"
using namespace sf;
int main(){
Engine Motor;
Motor.run();
Motor.cleanVirtualMemory();
}
Engine.h
#pragma once
#include <SFML/Graphics.hpp>
#include "StructureBuilder.h"
using namespace sf;
using namespace std;
class Engine{
private:
// Lots of variables ....
Vector2i m_ArenaSize;
Vector2f * vectorStructureArray = new Vector2f[m_ArenaSize.y * m_ArenaSize.x * 4];
int** logicStructureArray = new int*[m_ArenaSize.y];
// Lots of variables ....
//Gameloop
void Input();
void Update(dtAsSeconds);
void Draw();
public:
Engine();
void run();
void cleanVirtualMemory();
};
Engine.cpp
#include "Engine.h"
#include
using namespace sf;
Engine::Engine() {
/// lots of variables and data ...
/// Making arena
m_ArenaSize = Vector2i(10, 10);
StructureBuilder(arenaStructures, vectorStructureArray, logicStructureArray, m_ArenaSize);
}
void Engine::run() {
//Timing
Clock clock;
while (m_Window.isOpen()) {
// Each time clock restarted, dt = time elapse (from 0 to now, then clock = 0)
Time dt = clock.restart();
// Convert time elapse to seconds
double dtAsSeconds = dt.asSeconds();
// Call each part of the game in turn
Input();
Update(dtAsSeconds);
Draw();
}
}
void Engine::cleanVirtualMemory() {
// Deallocate Virtual Memory
// first flag
cout << "running1" << endl;
for (int i = m_ArenaSize.x - 1; i > -1; i--) {
delete[] logicStructureArray[i];
}
delete[] logicStructureArray;
logicStructureArray = NULL;
delete[] vectorStructureArray;
vectorStructureArray = NULL;
// second flag
cout << "running2" << endl;
}
StructureBuilder.h
#pragma once
#include <SFML/Graphics.hpp>
#include
#include
using namespace sf;
using namespace std;
Vector2i StructureBuilder(VertexArray& rVA, Vector2f* rA, int** rLA, Vector2i ArenaSize);
In the following code, you can see where I use pointers, I have erased code just to simplify.
StructureBuilder.cpp
#include "StructureBuilder.h"
Vector2i StructureBuilder(VertexArray& rVA, Vector2f* rA, int** rLA, Vector2i ArenaSize) {
//Set map properties and VertexArrayType
double tileSize = 100;
double Height = ArenaSize.y * tileSize;
double angle = 30;
int offset = 0;
int Primitive = 4;
int currentVertex = 0;
rVA.setPrimitiveType(Quads);
rVA.resize(ArenaSize.x * ArenaSize.y * 4);
// First build graphically our map structures's using char strings
string stringArray[10];
stringArray[0] = "1000000000";
stringArray[1] = "0000000000";
stringArray[2] = "0000010000";
stringArray[3] = "0000000000";
stringArray[4] = "0000000000";
stringArray[5] = "0000000000";
stringArray[6] = "0000000000";
stringArray[7] = "0000000000";
stringArray[8] = "0000000000";
stringArray[9] = "0000000000";
// Convert stringArray to charArray, finally charArray to intArray
char** charArray = new char*[ArenaSize.y];
Vector2f Vector1;
Vector2f Vector2;
Vector2f Vector3;
Vector2f Vector4;
for (int i = 0; i < ArenaSize.x; i++) {
charArray[i] = new char[ArenaSize.x];
rLA[i] = new int[ArenaSize.x];
}
for (int i = 0; i < ArenaSize.x; i++) {
for (int j = 0; j < ArenaSize.y; j++) {
charArray[j][i] = stringArray[j][i];
rLA[j][i] = charArray[j][i] - 48;
// Check when we have a value greater or equal to 1, if yes build a structure.
if (rLA[j][i] == 1) {
Vector1 = Vector2f(Value..., Value...);// in order to understand I dont put the whole calculation here
Vector2 = Vector2f(Value..., Value ...); // is just trigonometry to find vertex
Vector3 = Vector2f(Value..., Value ...);
Vector4 = Vector2f(Value..., Value ...);
rVA[currentVertex + 0].position = Vector1;
rVA[currentVertex + 1].position = Vector2;
rVA[currentVertex + 2].position = Vector3;
rVA[currentVertex + 3].position = Vector4;
rVA[currentVertex + 0].texCoords = Vector2f(42, 0); // coords on my spritesheet
rVA[currentVertex + 1].texCoords = Vector2f(86, 24);
rVA[currentVertex + 2].texCoords = Vector2f(42, 49);
rVA[currentVertex + 3].texCoords = Vector2f(0, 24);
rA[currentVertex + 0] = Vector1; //Later I use this to tell the program where to construct restrictions (where the player can't move)
rA[currentVertex + 1] = Vector2;
rA[currentVertex + 2] = Vector3;
rA[currentVertex + 3] = Vector4;
}
currentVertex = currentVertex + Primitive;
}
}
// Deallocate Virtual Memory
for (int i = ArenaSize.x - 1; i > -1; i--) {
delete[] charArray[i];
}
delete[] charArray;
charArray = NULL;
return ArenaSize;
}
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)