Solved

DAMAGE after normal block?

Posted on 2000-04-07
9
400 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 92
How to know only "File created" with EventLog 3 32
mapAB Challlenge 35 126
unix example issues 18 75
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

15 Experts available now in Live!

Get 1:1 Help Now