CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Nov 2002
    Location
    Lahore, Pakistan
    Posts
    52

    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..........?

  2. #2
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496
    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

  3. #3
    Join Date
    Nov 2002
    Location
    Lahore, Pakistan
    Posts
    52
    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

  4. #4
    Join Date
    Nov 2002
    Location
    Lahore, Pakistan
    Posts
    52
    PadexArt would u like to give me some examples of how to allocate and delete array using std namespace

    thanks in advance

  5. #5
    Join Date
    Nov 2002
    Location
    Lahore, Pakistan
    Posts
    52
    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....

  6. #6
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    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....

  7. #7
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496
    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

  8. #8
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496
    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

  9. #9
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    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....

  10. #10
    Join Date
    Nov 2002
    Location
    Lahore, Pakistan
    Posts
    52
    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

  11. #11
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496
    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

  12. #12
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496
    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
  •  





Click Here to Expand Forum to Full Width

Featured