Solved

DAMAGE after normal block?

Posted on 2000-04-07
9
417 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 31

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

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
 
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 31

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 134
ODBC Connection Logging, ADO.NET 6 71
has77  challenge 9 98
post4 challenge 28 113
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.

749 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