[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DAMAGE after normal block?

Posted on 2000-04-07
9
Medium Priority
?
457 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 400 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:Vinayak Kumbar
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:Vinayak Kumbar
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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:Vinayak Kumbar
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:Vinayak Kumbar
ID: 2700102
Hi,

Oh, IC, Thanks for that.

VinExpert
0
 
LVL 8

Expert Comment

by:Vinayak Kumbar
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

Industry Leaders: 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

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: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month19 days, 23 hours left to enroll

872 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