Original Post
Ive been trying to find a solution for this for quite a while and im still no closer to fixing it. In debug mode, everything runs fine. If i run it in release mode from visual studio, it also runs, but i get a memory leak error when i close the program for some reason. However, if i run the release build from its executable, the program wont even start. Ive traced the problem to be in my map loading class, which is tagged below. Any ideas?
#include "DXGLSTD.h"
#include "RXSprite.h"
#include "RXTile.h"
#include "RXMap.h"
#include <fstream>
RXMap::RXMap(LPDIRECT3DDEVICE9 device, char* path)
{
mapPosX = 0;
mapPosY = 0;
mapPosZ = 0;
nLayers = 0;
tiles = NULL;
mapName = NULL;
mapWidth = 0;
mapHeight = 0;
nTiles = 0;
numBatchVerts = 0;
batchVerts = NULL;
iBuffer = NULL;
mapTexture = NULL;
vBuffer = NULL;
LoadMap(device,path);
}
RXMap::~RXMap()
{
SAFE_RELEASE(vBuffer);
SAFE_RELEASE(iBuffer);
SAFE_RELEASE(mapTexture);
SAFE_DELETE_ARRAY(tiles);
}
HRESULT RXMap::LoadMap(LPDIRECT3DDEVICE9 device, char* path)
{
ifstream file;
file.open(path,ios::in |ios::binary);
char mapNameSize = NULL;
int nDiffTiles = 0;
INT64 imageSize = NULL;
file.seekg(0,ios::beg);
file.read((char *)(&mapNameSize),sizeof(char));
mapName = new char[mapNameSize];
file.read((char *)(mapName),sizeof(char)*(mapNameSize));
mapName[mapNameSize] = '\0';
long tempPos = file.tellg();
file.read((char *)(&mapWidth),sizeof(mapWidth));
file.read((char *)(&mapHeight),sizeof(mapHeight));
file.read((char *)(&nTiles),sizeof(nTiles));
file.read((char *)(&nDiffTiles),sizeof(nDiffTiles));
file.read((char *)(&imageSize),sizeof(imageSize));
char* sourceImage = new char[imageSize];
file.read((char *)(sourceImage),sizeof(char)*imageSize);
int vertTotal = nTiles*4;
if(FAILED(D3DXCreateTextureFromFileInMemory(device,sourceImage,imageSize,&mapTexture)))
{
MessageBox(NULL,L"Could not create texture",L"ERROR",NULL);
return E_FAIL;
}
if(FAILED( device->CreateVertexBuffer(vertTotal*sizeof(RX_TEXTUREDVERTEX),
D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_TEXTUREDVERTEX, D3DPOOL_DEFAULT ,
&vBuffer,NULL)))
return E_FAIL;
if(FAILED( device->CreateIndexBuffer((vertTotal*3),
0,D3DFMT_INDEX16 ,D3DPOOL_MANAGED,&iBuffer,NULL)))
return E_FAIL;
int index = 0;
short* indices = NULL;
iBuffer->Lock(0,(vertTotal*3),(void**)&indices,0);
for(int vertex =0; vertex < vertTotal; vertex +=4)
{
indices[index] = vertex;
indices[index + 1] = vertex + 2;
indices[index + 2] = vertex + 3;
indices[index + 3] = vertex;
indices[index + 4] = vertex + 1;
indices[index + 5] = vertex + 2;
index += 6;
}
iBuffer->Unlock();
tiles = new RXTile[nTiles];
vBuffer->Lock(0,vertTotal*sizeof(RX_TEXTUREDVERTEX),(void **)&batchVerts,0);
for(int i = 0; i < nTiles; i++)
{
char* tileID = "";
char tileIDSize = NULL;
int tempPosX = 0;
int tempPosY = 0;
double tempTexX = 0;
double tempTexY = 0;
int tempLayer = 0;
int tempFrameWidth = 0;
int tempFrameHeight = 0;
int tempNFrames = 1;
file.read((char *)(&tileIDSize),sizeof(char));
tileID = new char[tileIDSize];
file.read((char *)(tileID),sizeof(char)*(tileIDSize));
tileID[tileIDSize] = '\0';
file.read((char *)(&tempPosX),sizeof(tempPosX));
file.read((char *)(&tempPosY),sizeof(tempPosY));
file.read((char *)(&tempTexX),sizeof(tempTexX));
file.read((char *)(&tempTexY),sizeof(tempTexY));
file.read((char *)(&tempLayer),sizeof(tempLayer));
file.read((char *)(&tempFrameWidth),sizeof(tempFrameWidth));
file.read((char *)(&tempFrameHeight),sizeof(tempFrameHeight));
tiles.ResetTile(device,mapTexture,tempTexX,tempTexY,tempPosX,tempPosY,
0,tempNFrames,tempFrameWidth,tempFrameHeight,10,true,tileID);
batchVerts[numBatchVerts] = tiles.vertices[0];
batchVerts[numBatchVerts+1] = tiles.vertices[1];
batchVerts[numBatchVerts+2] = tiles.vertices[2];
batchVerts[numBatchVerts+3] = tiles.vertices[3];
numBatchVerts +=4;
}
file.close();
vBuffer->Unlock();
return S_OK;
}