Solved

DAMAGE after normal block?

Posted on 2000-04-07
9
389 Views
Last Modified: 2013-11-20
DAMAGE after normal block.

I get this error message when I bt to the 'delete buffer' part of the code below.

The program I'm using also uses the timer, so could that mess up the stack in some way?

I've never seen this whole trying to delete a char buffer (or any other buffer).  *any* help or comments or pointer would really help.


Here's the code:

.
.
.
      int iLen = sUrl.GetLength();
      char *buffer = new char[iLen];
      DWORD dwBuffLen = 1000;
      int iVal =InternetCanonicalizeUrl(sUrl, buffer, &dwBuffLen, ICU_ENCODE_SPACES_ONLY );


      if (iVal == ERROR_BAD_PATHNAME ) {
            AfxMessageBox("Error:  bad pathname in URL");
            return;
      }
      else if (iVal == ERROR_INSUFFICIENT_BUFFER ) {
            AfxMessageBox("Error:  Insufficient buffer");
            return;
      }
      else if (iVal == ERROR_INTERNET_INVALID_URL) {
            AfxMessageBox("Error:  Internet invalid url");
            return;
      }
      else if (iVal == ERROR_INVALID_PARAMETER ) {
            AfxMessageBox("Error: Invalid parameter");
            return;
      }


      CString strTmp = buffer;
      

      if (sComment == "") {
            strTmp += "%20";
      }



      CInternetSession Connection;
      CHttpConnection* pHttpConnection = NULL;
      CHttpFile* pFile = NULL;

      delete buffer;

      try {
            
            pHttpConnection = Connection.GetHttpConnection(strTmp);//sUrl);
            pFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET,"/");
            pFile->SendRequest();
            CString s;
            for (int j = 0; j < 10; j++) {
                  if (pFile->ReadString(s))
                  {
                        CString str = "set to: ";
                        str += s;
.
.
.
.
.


Thanks,

TimB
0
Comment
Question by:Moth
  • 5
  • 3
9 Comments
 
LVL 30

Accepted Solution

by:
Zoppo earned 100 total points
ID: 2692962
Hi Moth,

a buffer allocated with 'buf = new type[x]' has to be deleted with 'delete [] buf' instead of 'delete buf'.

ZOPPO
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 2693020
Hi,

Zoppo, as he has defined it as a pointer, i dont think he should use [] for deleting.

Are U sure that, whatever u have allocated is sufficient?. pls check it out.

int iLen = sUrl.GetLength();
char *buffer = new char[iLen + 2];
memset(buffer, 0, (iLen + 2));
DWORD dwBuffLen = iLen + 2;
int iVal =InternetCanonicalizeUrl(sUrl, buffer, &dwBuffLen, ICU_ENCODE_SPACES_ONLY );

and while deleting

if(buffer)
{
delete buffer;
buffer = NULL;
}

Try it out.

VinExpert
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 2693029
Hi,

Zoppo, as he has defined it as a pointer, i dont think he should use [] for deleting.

Moth,
Are U sure that, whatever u have allocated is sufficient?. pls check it out.

Try changing Ur code as -

int iLen = sUrl.GetLength();
char *buffer = new char[iLen + 2];
memset(buffer, 0, (iLen + 2));
DWORD dwBuffLen = iLen + 2;
int iVal =InternetCanonicalizeUrl(sUrl, buffer, &dwBuffLen, ICU_ENCODE_SPACES_ONLY );

and while deleting

if(buffer)
{
delete buffer;
buffer = NULL;
}

Try it out.

VinExpert
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 2693120
To VinExpert: using the 'array'-form of new (with []) requires using of array form of delete. I do not understand what you mean with 'as he has defined it as a pointer'. What else should he define? He defines a pointer to type char which is set to an allocated array of chars. Then of course he has to delete the array as an array.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Author Comment

by:Moth
ID: 2693337
Brilliant,

Both of these have helped no-end.  I've increased the buffer size as VinExpert suggested, and this stopped the error message.

I was also having problem of the URL connection not working correctly after repeated calls, so I also used the delete [] change that Zoppo gave me.

This part of the program works a treat now,  Thanks.

Moth
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 2695609
Hi Zoppo,

Yes, I too agree with u, but for my comment, the base something that i did. I just added the following lines and checked for the memory leaks.

char *buffer = new char[10];
memset(buffer,0,10);
delete buffer;

Now according to my prev experience, i thought, i will get memory leak. But its not!!!. Just u can try it. I checked it in VC 6.0, there was no memory dump in the output window. Thats why I gave that. Now correct me if I am wrong.

Thanks,

VinExpert
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 2699420
Hmm, I only know that every C++ book tells you have to do delete[]... with new ...[]. Perhaps the only difference is that delete []... makes sure that dtor is called for all objects in the array.

Perhaps there's even a difference between MSVC's debug and release versions of new and delete.

ZOPPO
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 2700102
Hi,

Oh, IC, Thanks for that.

VinExpert
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 2700108

>I only know that every C++ book tells you have to do delete[].
But, why we should beleive them like that only?.

VinExpert
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
while loop over for loop 7 79
post4 challenge 28 83
zeroFront challenge 7 71
changeXy challenge 13 57
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video discusses moving either the default database or any database to a new volume.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now