Prev: Function vs Method
Next: C-type casting
From: Jack on 16 Dec 2009 05:37 Thanks Jochen once again as you come back to this Frustrated because when I added the line delete binfile; it just crashed with heap corruption When I cancelled it, There were 4 bytes of memory leak chunks repeatedly. Thanks Jack
From: Giovanni Dicanio on 16 Dec 2009 06:09 "Jack" <jl(a)knight.com> ha scritto nel messaggio news:uSWo3SjfKHA.1648(a)TK2MSFTNGP05.phx.gbl... > PBYTE binfile = new BYTE[size.QuadPart+0x1e0-4]; > > // > delete binfile; > binfile = NULL; > > These statements are located in the same method. > What would be the probable causes of heap corruption > (written past the end of the heap buffer, buffer overrun i guess) You should do (note [ and ] ) delete [] binfile; Giovanni
From: Giovanni Dicanio on 16 Dec 2009 06:13 "Jack" <jl(a)knight.com> ha scritto nel messaggio news:urifhWjfKHA.4112(a)TK2MSFTNGP06.phx.gbl... > One might ask why I used new BYTE[...]; > instead of std::vector<BYTE> > > because I want to advance the pointer at my own will > such as this > binfile += 0x1e0; If you use std::vector, you can use a raw C pointer to point inside vector, e.g.: std::vector< BYTE > v( << some size >> ); BYTE * binfile = &v[0]; The benefit of using vector instead of new[] is that your code is now exception safe (e.g. if an exception is thrown, the vector releases the heap allocated bytes). HTH, Giovanni
From: Jack on 16 Dec 2009 06:17 Hi Giovanni, > std::vector< BYTE > v( << some size >> ); > > BYTE * binfile = &v[0]; > Sounds like that is what I am after. Thanks Giovanni Jack
From: Jack on 16 Dec 2009 06:44
No, still crashed GetFileSizeEx(h, &size); std::vector< BYTE > v( size.QuadPart+0x1e0-4 ); BYTE * binfile = &v[0]; memset (binfile, 0, size.QuadPart+0x1e4-4); memcpy (binfile, 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 // mucking around with it :) Thanks Jack |