Skip to main content
gamedev.net
🔒 Locked

Release Mode problems...

Started by barakus Mar 11, 2005 at 11:04 PM 4 replies 1.2k views
Original Post
barakus
barakus
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;
	
}

Drew_Benton
Drew_Benton
Here's a suggestion to try for getting rid of the memory leaks -
void RXMap::Cleanup(){	SAFE_RELEASE(vBuffer);	SAFE_RELEASE(iBuffer);	SAFE_RELEASE(mapTexture);	SAFE_DELETE_ARRAY(tiles);}


Call that explicity before you exit instead of letting the deconstructor run implicity. I think that *could* be causing the problems with the leaks. I do not know about the other code, not much of a DX person. However, most of the time when debug works and release doesn't, it means that some variables or pointer were not initialized properly. Debug mode sets all vars and pointers to 0 to being with, release mode does not. Look for something along those lines as well.

Also:
file.read((char *)(&tileIDSize),sizeof(char));
tileID = new char[tileIDSize];

file.read((char *)(tileID),sizeof(char)*(tileIDSize));
tileID[tileIDSize] = '\0';

You never free that memory at the end of the loop! That's probabally the leak right there. Add a delete [] tileID; at the end of the loop.

Same thing for mapName and sourceImage! Anywhere you use new, you need to have a delete with it as well. I think the program is just running out of memory from the allocations that are not freed.

You never check to make sure 'indices' is valid either. If it is not, you are accessing memory that is not valid and that could cause a crash.

short* indices = NULL;iBuffer->Lock(0,(vertTotal*3),(void**)&indices,0);if( !indices)   return E_FAIL;


Also it is impartive to make sure 'file' was opened. That way if it was not, then you know that there was a problem:
if( !file.is_open() )   return E_FAIL;


Phew! Ok that's enough from me now. Good luck!

- Drew
barakus
barakus
thanks for the quick reply.
The problem is, that when i try to delete tileID, it gives me an error, and regardless, i get no memory leak on the debug version and i just took out that piece of code and i still get both errors.
EDIT:
Actually the error is in that loop, but i still get the memory leak.

The memory leak isnt whats really worrying me, its the fact that it wont even start in release mode when run from the executable. How can it run from visual studio but not from the executable?
JohnBolton
JohnBolton
Quote:
Original post by barakus
... 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...


When you open a file, the location of the file depends on your "current working directory" (cwd) if the name doesn't specify the full path. Your cwd is different depending on how you start the program.

When starting from the debugger, the cwd is the folder containing the project file (or solution file, I don't remember). When starting from an icon, the cwd is the folder containing the executable. When starting from the command line, the cwd is the current folder for the current drive. When starting from a shortcut, the cwd defaults to the folder containing the executable, but can be changed in the shortcut.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.