Solved

DAMAGE after normal block?

Posted on 2000-04-07
9
406 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Define unique primary key 9 92
Path of Workbook 3 73
unix example issues 18 89
Work with App store 7 51
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

816 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

12 Experts available now in Live!

Get 1:1 Help Now