-
October 22nd, 2003, 05:50 AM
#1
Error Deleting BYTE Array
This is code where I have got assertion on deleting memory
This code ran scuccessfully about 1000 times but after about 4-5 hours of excution I got assertion on deleting GlobalpBuffer
if ( GlobalpBuffer != NULL )
{
delete [] GlobalpBuffer;
GlobalpBuffer = NULL;
}
Assertion occured in file "DBGHEAP.C" on these statements
/* if we didn't already check entire heap, at least check this object */
if (!(_crtDbgFlag & _CRTDBG_CHECK_ALWAYS_DF))
{
/* check no-mans-land gaps */
if (!CheckBytes(pHead->gap, _bNoMansLandFill, nNoMansLandSize))
_RPT3(_CRT_ERROR, "DAMAGE: before %hs block (#%d) at 0x%08X.\n",
szBlockUseName[_BLOCK_TYPE(pHead->nBlockUse)],
pHead->lRequest,
(BYTE *) pbData(pHead));
if (!CheckBytes(pbData(pHead) + pHead->nDataSize, _bNoMansLandFill, nNoMansLandSize))
_RPT3(_CRT_ERROR, "DAMAGE: after %hs block (#%d) at 0x%08X.\n",
szBlockUseName[_BLOCK_TYPE(pHead->nBlockUse)],
pHead->lRequest,
(BYTE *) pbData(pHead));
}
what might have been the cause how could I make this code more secure..........?
-
October 22nd, 2003, 06:14 AM
#2
Make sure you haven't already delete the array somewhere else.
How did you allocate the array: new or malloc?
A soluton for safer array manipulation would be to use the std structures.
Har Har
-
October 22nd, 2003, 06:45 AM
#3
I am only deleting my memory at one place and after deleting I am assining GlobalpBuffer to NULL
and before deleting I check that whether it is NULL or not
any other suggestions.....
waiting for reply
thanks in advance
-
October 22nd, 2003, 06:55 AM
#4
PadexArt would u like to give me some examples of how to allocate and delete array using std namespace
thanks in advance
-
October 22nd, 2003, 07:15 AM
#5
I am really in deep problem
my code worked fine for about 4-5 hours and nothing happened and suddenly I got error in DBGHEAP.C
can anyone help me...
thanks in advance....
-
October 22nd, 2003, 07:17 AM
#6
the code above tells you you strayed into no-mans land....which means you strayed into the default 4 byte padding around new in the debug CRT RTL...which means you have a memory overwrite....
start your code, grab the address of your pointer, then set a 'data' breakpoint when just beyond the address changes, you'll see the no-mans land fill as FD FD FD FD set the break when that changes...then find out why it changed....
-
October 22nd, 2003, 07:18 AM
#7
Bevfore anything else, if you are developing the project using visual Studio, rebuild your enritre project first ( Rebuild all ). Sometimes the Microsoft linker makes mistakes
Har Har
-
October 22nd, 2003, 07:38 AM
#8
Here is a small sample of how to use the STL structures ( vector). More help is available on MSDN and other sources.From what I know about STL, the loss of speed is insignificant.
However I think that a good old rebuild will solve you problem.
Code:
#include "stdafx.h"
#include <windows.h>
#include <vector>
#include <iostream.h>
int main(int argc, char* argv[])
{
std::vector<BYTE> vctByte;
char buf[10];
//enbsure there is enough space and create the vector's elements
vctByte.reserve( 200);
vctByte.assign( 200, 0);
//you can assign values just as for a regular array
for( int i = 0; i < vctByte.size(); ++i)
{
vctByte[i] = i;
}
//you can read values just as for a regular array
cout << endl << "Array like access" << endl;
for( i = 0; i < vctByte.size(); ++i)
{
sprintf( buf, "%-5d", vctByte[i]);
cout << buf;
}
//the iterators are great also
cout << endl << "Iterator access" << endl;
for( std::vector<BYTE>::iterator it = vctByte.begin(); it != vctByte.end(); ++it)
{
sprintf( buf, "%-5d", *it);
cout << buf;
}
return 0;
}
Hope it helps.
Har Har
-
October 22nd, 2003, 07:43 AM
#9
Originally posted by PadexArt
However I think that a good old rebuild will solve you problem.
For a memory bounds overwrite a rebuild will do nothing....I agree advocating changing to using the STL, but we don't see the code so we can't make assumptions....
-
October 22nd, 2003, 07:47 AM
#10
Originally posted by PadexArt
Here is a small sample of how to use the STL structures ( vector). More help is available on MSDN and other sources.From what I know about STL, the loss of speed is insignificant.
However I think that a good old rebuild will solve you problem.
Code:
#include "stdafx.h"
#include <windows.h>
#include <vector>
#include <iostream.h>
int main(int argc, char* argv[])
{
std::vector<BYTE> vctByte;
char buf[10];
//enbsure there is enough space and create the vector's elements
vctByte.reserve( 200);
vctByte.assign( 200, 0);
//you can assign values just as for a regular array
for( int i = 0; i < vctByte.size(); ++i)
{
vctByte[i] = i;
}
//you can read values just as for a regular array
cout << endl << "Array like access" << endl;
for( i = 0; i < vctByte.size(); ++i)
{
sprintf( buf, "%-5d", vctByte[i]);
cout << buf;
}
//the iterators are great also
cout << endl << "Iterator access" << endl;
for( std::vector<BYTE>::iterator it = vctByte.begin(); it != vctByte.end(); ++it)
{
sprintf( buf, "%-5d", *it);
cout << buf;
}
return 0;
}
Hope it helps.
Thanks PadexArt
u have declared that memory on stack how we would declare that memory on heap and how we would delete that
-
October 22nd, 2003, 07:50 AM
#11
For a memory bounds overwrite a rebuild will do nothing
Well it happened to me in practice when adding/removing attributes in classes. When I followed the application's flow with the debugger, there were really weird jumps in the code ( the goto style jumps ) ...
The only logical explanation for me was a bad link .... performed the rebuild ... and my supposition proved to be correct
but we don't see the code so we can't make assumptions....
I agree with you here. My sentence, However I think that a good old rebuild will solve you problem is a bit too much.
Let's see if Shehzad_Salim has made any progress.
Har Har
-
October 22nd, 2003, 07:55 AM
#12
Well the beauty when using the vector is that you don't care where, how and who is allocating the memory. At least not for simple tasks.
Regarding your question, allocate the vector object on the heap and free it when you are done.
Code:
std::vector<BYTE>* pVctByte = new std::vector<BYTE>();
pVctByte->reserve( 200);
pVctByte->assign( 200, 0);
//... use it
//somewhere in your code release it
delete pVctByte;
Har Har
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|