Replaced use of heap with use of stack created ifstream to avoid potential memory leak

This commit is contained in:
Robert Osfield 2019-01-09 08:43:08 +00:00
parent bd83044c08
commit 8103da6ef1

View File

@ -46,7 +46,6 @@ VVDReader::~VVDReader()
bool VVDReader::readFile(const std::string & file) bool VVDReader::readFile(const std::string & file)
{ {
osgDB::ifstream * vvdFile;
VVDHeader header; VVDHeader header;
int vertIndex; int vertIndex;
int i, j; int i, j;
@ -54,7 +53,7 @@ bool VVDReader::readFile(const std::string & file)
// Remember the map name // Remember the map name
vvd_name = getStrippedName(file); vvd_name = getStrippedName(file);
vvdFile = new osgDB::ifstream(file.c_str(), std::ios::binary); osgDB::ifstream vvdFile(file.c_str(), std::ios::binary);
if (!vvdFile) if (!vvdFile)
{ {
OSG_NOTICE << "Vertex data file not found" << std::endl; OSG_NOTICE << "Vertex data file not found" << std::endl;
@ -63,7 +62,7 @@ bool VVDReader::readFile(const std::string & file)
// Read the header // Read the header
memset(&header, 0xcd, sizeof(VVDHeader)); memset(&header, 0xcd, sizeof(VVDHeader));
vvdFile->read((char *) &header, sizeof(VVDHeader)); vvdFile.read((char *) &header, sizeof(VVDHeader));
// Make sure the file is a valid Valve VVD file // Make sure the file is a valid Valve VVD file
if (header.magic_number != VVD_MAGIC_NUMBER) if (header.magic_number != VVD_MAGIC_NUMBER)
@ -77,9 +76,9 @@ bool VVDReader::readFile(const std::string & file)
// Read the fixup table // Read the fixup table
fixup_table = new VVDFixupEntry[header.num_fixups]; fixup_table = new VVDFixupEntry[header.num_fixups];
vvdFile->seekg(header.fixup_table_offset); vvdFile.seekg(header.fixup_table_offset);
for (i = 0; i < header.num_fixups; i++) for (i = 0; i < header.num_fixups; i++)
vvdFile->read((char *) &fixup_table[i], sizeof(VVDFixupEntry)); vvdFile.read((char *) &fixup_table[i], sizeof(VVDFixupEntry));
// Create the vertex buffers // Create the vertex buffers
for (i = 0; i < header.num_lods; i++) for (i = 0; i < header.num_lods; i++)
@ -100,12 +99,12 @@ bool VVDReader::readFile(const std::string & file)
if (fixup_table[j].lod_number >= i) if (fixup_table[j].lod_number >= i)
{ {
// Seek to the vertex indicated by the fixup table entry // Seek to the vertex indicated by the fixup table entry
vvdFile->seekg(header.vertex_data_offset + vvdFile.seekg(header.vertex_data_offset +
fixup_table[j].source_vertex_id * fixup_table[j].source_vertex_id *
sizeof(VVDVertex)); sizeof(VVDVertex));
// Read the number of vertices specified // Read the number of vertices specified
vvdFile->read((char *) &vertex_buffer[i][vertIndex], vvdFile.read((char *) &vertex_buffer[i][vertIndex],
fixup_table[j].num_vertices * fixup_table[j].num_vertices *
sizeof(VVDVertex)); sizeof(VVDVertex));
@ -117,10 +116,10 @@ bool VVDReader::readFile(const std::string & file)
else else
{ {
// Seek to the vertex data // Seek to the vertex data
vvdFile->seekg(header.vertex_data_offset); vvdFile.seekg(header.vertex_data_offset);
// Just read the vertices directly // Just read the vertices directly
vvdFile->read((char *) &vertex_buffer[i][0], vvdFile.read((char *) &vertex_buffer[i][0],
header.num_lod_verts[i] * sizeof(VVDVertex)); header.num_lod_verts[i] * sizeof(VVDVertex));
} }
@ -130,8 +129,7 @@ bool VVDReader::readFile(const std::string & file)
} }
// Close the file // Close the file
vvdFile->close(); vvdFile.close();
delete vvdFile;
return true; return true;
} }