From: Stephen Howe on
On Wed, 16 Dec 2009 17:47:13 +0800, "Jack" <jl(a)knight.com> wrote:

>PBYTE binfile = new BYTE[size.QuadPart+0x1e0-4];
>delete binfile;

This is incorrect C++.

If you do new [], it should be matched with delete [], not delete

new[] and delete [] go together (for multiple items)
new and delete go together (for single item)

Cheers

Stephen Howe
From: Jack on
Hi Stephen,
Actually, I have tried both delete and delete[], but doesn't work either.
Thanks for your help!!
Jack



From: Jochen Kalmbach [MVP] on
Hi Jack!

> Actually, I have tried both delete and delete[], but doesn't work either.

In this special case (POD) it makes no difference...

You need to show the code between the "new" and "delete"!

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
From: Jack on
There u go, :)

HRESULT CMesh::SetMeshData(char *szfilename)
{
LARGE_INTEGER size;
HANDLE hFileMapping;
int cchFileName;
char szPath[256];
char *szTemp;
DWORD cchPath;
CAllocateHierarchy Alloc;

// unload these functions to a dll
::GetModuleFileNameA(NULL, szPath, sizeof(szPath));

strcat (szPath, szfilename);


//// Open .dat mesh
HANDLE h = CreateFileA(szPath, GENERIC_READ, NULL, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE)
{
MessageBoxA(NULL, "Couldn't open file with CreateFile()", "Error",
MB_OK);
return E_FAIL;
}

GetFileSizeEx(h, &size);

std::vector< BYTE > v( size.QuadPart+0x1e0-4 );

BYTE * binfile = &v[0];


//PBYTE binfile = new BYTE[size.QuadPart+0x1e0-4]; // maybe get the file
size 1st
memset (binfile, 0, size.QuadPart+0x1e4-4);
//binfile.clear();
memcpy (binfile, template_bin, 0x1e0);
//binfile.push_back(template_bin);//, 0x1e0);
binfile += 0x1e0;

hFileMapping = CreateFileMapping (h, NULL, PAGE_READONLY, 0, 0, NULL);
if (hFileMapping == 0)
{
CloseHandle(h);
MessageBoxA(NULL, "Couldn't open file mapping", "Error", MB_OK);

return E_FAIL;
}

//// Get a whole file into memory
PBYTE g_pMappedFileBase = (PBYTE) MapViewOfFile (hFileMapping,
FILE_MAP_READ, 0, 0, 0);
if (g_pMappedFileBase == 0)
{
CloseHandle (hFileMapping);
CloseHandle (h);
return E_FAIL;
}

if (memcmp(g_pMappedFileBase, "hdr1", 4) == 1)
{
CloseHandle(hFileMapping);
CloseHandle(h);
MessageBoxA(NULL, "Error Loading Header", "Error", MB_OK);
return E_FAIL;
}

memcpy (binfile, g_pMappedFileBase+4, size.QuadPart-4);
binfile -= 0x1e0; // back to origin

HRESULT hr = D3DXLoadMeshHierarchyFromXInMemory((LPCVOID) binfile,
size.QuadPart + 0x1e0-4, D3DXMESH_MANAGED, m_pDevice, &Alloc,
NULL, (LPD3DXFRAME*)&(m_pFrameRoot), &m_pAnimController);



if (FAILED(hr))
{
MessageBoxA(NULL, "Can't load mesh", "Error", MB_OK);
}


if (binfile) {

delete binfile;
binfile = NULL;

}


return S_OK;


}




From: Igor Tandetnik on
Jack wrote:
> No, still crashed
> std::vector< BYTE > v( size.QuadPart+0x1e0-4 );
> memset (binfile, 0, size.QuadPart+0x1e4-4);

You are writing past the end of allocated buffer.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6
Prev: Function vs Method
Next: C-type casting