473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

DAMAGE: after Normal block (#45)

Hi all,

I just joined this group and am new to VC++. I wrote the code following
the next para in C++ and used VC++ 6.0 Enterprise Edition to build and
test.

I had the following error message because of an exception that gets
thrown when I call delete [] ipAddress; in the destructor -
~TrapDestination () :

"DAMAGE: after Normal block (#45)"

// ~~~~~~~~~~~
// plugIn.h
// ~~~~~~~~~~~
#include <memory>
#include <iostream>
#include <exception>
#include <assert.h>
using namespace std;

typedef char* pchar;

class TrapDestination {
private:
pchar ipAddress;
int port;
pchar communityName;
int snmpVersion;
pchar templateFileName;
void (*old_unexpected) ();

public:
TrapDestination () : ipAddress (NULL), communityName (NULL),
templateFileName (NULL) { //old_unexpected =
set_unexpected(::my_unexpected*);
}
~TrapDestination () throw () {
cout << "TrapDestination Destructor" << endl;
if (ipAddress)
delete [] ipAddress;
if (communityName)
delete [] communityName;
if (templateFileName)
delete [] templateFileName;

//set_unexpected(old_unexpecte*d); // restore org
handler
if (uncaught_exception())
cout << "uncaught_exception() is TRUE" << endl;

else
cout << "uncaught_exception() is FALSE" <<
endl;
}

char *getIPAddress () throw () { return ipAddress; }
int getPort () throw () { return port; }
char *getcommunityName () throw () { return communityName; }
int getsnmpVersion () throw () { return snmpVersion; }
char *gettemplateFileName () throw () { return
templateFileName; }

void setIPAddress (char *ipAddr="") throw () {
ipAddress = (pchar) new char(strlen (ipAddr) + 1);
if (ipAddress)
strcpy (ipAddress, ipAddr);
}

void setPort (int aPort=162) { port = aPort; }

void setcommunityName (char *aCommunityName="public") {
communityName = (pchar) new char (strlen
(aCommunityName) + 1);
if (communityName)
strcpy (communityName, aCommunityName);
}

void setsnmpVersion (int snmpVer=2) { snmpVersion = snmpVer; }

void settemplateFileName (char *templFileName="") {
templateFileName = (pchar) new char (strlen
(templFileName) + 1);
if (templateFileName)
strcpy (templateFileName, templFileName);
}
};

class SnmpTrapGen {
private:
char *ipAddress;
int port;
char *community;
int snmpVersion;
public:
SnmpTrapGen (char *ipAddress=NULL, int port=162, char
*community="public", int snmpVersion=2){
}
~SnmpTrapGen () {
if (ipAddress)
delete [] ipAddress;
if (community)
delete [] community;
}

sendTrap (char *alertToSend);
char *getIPAddress () { return ipAddress; }
int getPort () { return port; }
char *getcommunity () { return community; }
int getsnmpVersion () { return snmpVersion; }

void setIPAddress (char *ipAddr="") {
ipAddress = new char (strlen (ipAddr) + 1);
if (ipAddress)
strcpy (ipAddress, ipAddr);
}
void setPort (int aPort=162) { port = aPort; }
void setcommunity (char *aCommunity="public") {
community = new char (strlen (aCommunity) + 1);
if (community)
strcpy (community, aCommunity);
}
void setsnmpVersion (int snmpVer=2) { snmpVersion = snmpVer; }

};
// ~~~~~~~~~~~~
// plugIn.cpp
// ~~~~~~~~~~~~

#include "net-snmp\net-snmp-config.h"
#include "net-snmp\net-snmp-includes.h"
#include "plugIn.h"

void local_terminator() {
cout << "terminate called local_terminator !!!" << endl;
exit(1);
}

void (*old_terminate)();

void my_unexpected () {
// This cannot return - instead it can either call std::terminate() or
throw an exception

cout << "Am in my_unexpected " << endl;
}

int main (int argc, char **argv) {
try {
TrapDestination td;

old_terminate = set_terminate(local_terminator*);
td.setIPAddress ("127.0.0.1");
td.setcommunityName ();
td.settemplateFileName ("tempname");
cout << "IP address = " << td.getIPAddress () << endl;
printf ("communityName = %s.\n", td.getcommunityName
());
printf ("templateFileName = %s.\n",
td.gettemplateFileName ());
return 0;
} catch (bad_exception const &) {
printf("Caught bad_exception\n"); // though such an
exception was
never thrown
return 0;
} catch (...) {
cout << "Inside main's catch all" << endl;
return 0;
}
}
Any light on :

1. Why is delete causing this exception to be thrown could be helpful.
2. What the solution for this is.

will be great!

Just to add, an earlier post similar to this scenario had a response
that while allocating space for a string using new, make sure you add 1
for the '\0'. However I have taken care of doing that in my code and
yet I have this exception when it hits the delete.

I really need help from an expert.

Thank you,
Meghavvarnam Satish

Jul 23 '05 #1
11 4423
Meghavvarnam wrote:
I just joined this group and am new to VC++. I wrote the code following
the next para in C++ and used VC++ 6.0 Enterprise Edition to build and
test.
Unless the behaviour is specific to that compiler and you tried other ones
and found that out, the compiler you used is irrelevant. We don't discuss
products here. We discuss the language.
I had the following error message because of an exception that gets
thrown when I call delete [] ipAddress; in the destructor -
~TrapDestination () :

"DAMAGE: after Normal block (#45)"
[...]


This is usually caused by memory overrun (writing beyond the bounds of
an array) or by using an uninitialised pointer (which by coincidence just
points to some real memory). Use a memory debugging tool (something like
BoundsChecker or Purify or Insure++) to find the problem, or do it in the
debugger, manually. Your goal is to learn your tools, not to use this
newsgroup as your remote debugger.

V
Jul 23 '05 #2
"Meghavvarnam" <me********@yahoo.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com

[snip]
void setIPAddress (char *ipAddr="") throw () {
ipAddress = (pchar) new char(strlen (ipAddr) + 1);
if (ipAddress)
strcpy (ipAddress, ipAddr);
}

The syntax for new is wrong (here and everywhere else that you use it). It
should be

ipAddress = new char[strlen (ipAddr) + 1];

i.e., use square brackets, not round brackets (your cast to pchar is
redundant, but that is not what is causing the problem). Square brackets
give an array of chars, which is what you want. Round brackets are for when
you are specifying the argument for the constructor of a single object.

--
John Carson
Jul 23 '05 #3
CI
You could try to use auto_ptr rather than using raw pointers. I dont
see any specific reason why you want to use raw pointer here.
Ik

Jul 23 '05 #4
Hey John!

I tried your suggestion. And made changes where ever I call new for the
character pointers.

It worked fine this time around!!! Thank you so much !!! :)

I infact looked back into "The C++ Pragraming Language" Third Edition
by Bjarne Stroustup, pg. 131 where he says for a built in type T, T(e)
is equivalent to (T) e ; e being any value. This implies that "for
built-in types", T(e) is not safe.

So my earlier code -
ipAddress = (pchar) new char (strlen (ipAddr) + 1);
was actually looked at as
ipAddress = (pchar) new (char) strlen (ipAddr) + 1;

Thus resulting in an int being type casted to a char and then being
passed to new. And thus we had an implementation defined behaviour over
there.

Thanks a lot again!!!
Megh

Jul 23 '05 #5
Hi,

My original code had auto_ptr instead of a plain char *.

However when I debugged and looked at the call stack, the destructor
would call delete. delete would then call free. free would then call
_free_dbg. And then I would get that exception.

I doubted auto_ptr and to make it simpler, I used char *. However as
you all know now, the behaviour did not change.

However now I will use auto_ptr and see how it behaves. We wont need to
call delete at that point of time specifically, because our friend
auto_ptr would do that for us.

Will be back with the update folks !

Thank you!
Megh

Jul 23 '05 #6
Meghavvarnam wrote:
My original code had auto_ptr instead of a plain char *.

However when I debugged and looked at the call stack, the destructor
would call delete. delete would then call free. free would then call
_free_dbg. And then I would get that exception.

I doubted auto_ptr and to make it simpler, I used char *. However as
you all know now, the behaviour did not change.

However now I will use auto_ptr and see how it behaves. We wont need to
call delete at that point of time specifically, because our friend
auto_ptr would do that for us.

Will be back with the update folks !


'auto_ptr' is not going to work for you. It does NOT work with arrays.
And you apparently need arrays.

Stop using the array of char and switch to 'std::string'.

V
Jul 23 '05 #7
Hi again,

Below is the auto_ptr version. It works fine too !!! :)

// ~~~~~~~~
// plugIn.h
// ~~~~~~~~

#include <memory>
#include <iostream>
#include <exception>
#include <assert.h>
using namespace std;

typedef auto_ptr <char> pchar;

//typedef char* pchar;

class TrapDestination {
private:
pchar ipAddress;
int port;
pchar communityName;
int snmpVersion;
pchar templateFileName;
void (*old_unexpected) ();

public:
TrapDestination () : ipAddress (NULL), communityName (NULL),
templateFileName (NULL) {
}
~TrapDestination () throw () {
cout << "TrapDestination Destructor" << endl;
//set_unexpected(old_unexpected); // restore org handler
if (uncaught_exception())
cout << "uncaught_exception() is TRUE" << endl;
else
cout << "uncaught_exception() is FALSE" << endl;
}

char *getIPAddress () throw () { return ipAddress.get (); }
int getPort () throw () { return port; }
char *getcommunityName () throw () { return communityName.get (); }
int getsnmpVersion () throw () { return snmpVersion; }
char *gettemplateFileName () throw () { return templateFileName.get
(); }

void setIPAddress (char *ipAddr="") throw () {
ipAddress = (pchar) new char [strlen (ipAddr) + 1];
if (ipAddress.get ())
strcpy (ipAddress.get (), ipAddr);
ipAddress.get ()[strlen(ipAddr)] = '\0';
}

void setPort (int aPort=162) { port = aPort; }

void setcommunityName (char *aCommunityName="public") {
communityName = (pchar) new char [strlen (aCommunityName) + 1];
if (communityName.get ())
strcpy (communityName.get (), aCommunityName);
}

void setsnmpVersion (int snmpVer=2) { snmpVersion = snmpVer; }

void settemplateFileName (char *templFileName="") {
templateFileName = (pchar) new char [strlen (templFileName) + 1];
if (templateFileName.get ())
strcpy (templateFileName.get (), templFileName);
}
};
class SnmpTrapGen {
private:
char *ipAddress;
int port;
char *community;
int snmpVersion;
public:
SnmpTrapGen (char *ipAddress=NULL, int port=162, char
*community="public", int snmpVersion=2){
}
~SnmpTrapGen () {
if (ipAddress)
delete [] ipAddress;
if (community)
delete [] community;
}

sendTrap (char *alertToSend);

char *getIPAddress () { return ipAddress; }
int getPort () { return port; }
char *getcommunity () { return community; }
int getsnmpVersion () { return snmpVersion; }

void setIPAddress (char *ipAddr="") {
ipAddress = new char [strlen (ipAddr) + 1];
if (ipAddress)
strcpy (ipAddress, ipAddr);
}
void setPort (int aPort=162) { port = aPort; }
void setcommunity (char *aCommunity="public") {
community = new char [strlen (aCommunity) + 1];
if (community)
strcpy (community, aCommunity);
}
void setsnmpVersion (int snmpVer=2) { snmpVersion = snmpVer; }
};

Jul 23 '05 #8
Hi all:

Apologies for the confusion in my previous post. Am pasting the above
piece of code that works again. Have reformatted it.

Please note the usage of the member function get () to get the address
of an auto_ptr variable.

// ~~~~~~~~~
// plugIn.h
// ~~~~~~~~~

#include <memory>
#include <iostream>
#include <exception>
#include <assert.h>
using namespace std;

typedef auto_ptr <char> pchar;

class TrapDestination {
private:
pchar ipAddress;
int port;
pchar communityName;
int snmpVersion;
pchar templateFileName;
void (*old_unexpected) ();

public:
TrapDestination () : ipAddress (NULL), communityName (NULL),
templateFileName (NULL) {
//old_unexpected = set_unexpected(::my_unexpected);
}
~TrapDestination () throw () {
cout << "TrapDestination Destructor" << endl;

if (uncaught_exception())
cout << "uncaught_exception() is TRUE" << endl;
else
cout << "uncaught_exception() is FALSE" << endl;
}

char *getIPAddress () throw () { return ipAddress.get (); }
int getPort () throw () { return port; }
char *getcommunityName () throw () { return communityName.get (); }
int getsnmpVersion () throw () { return snmpVersion; }
char *gettemplateFileName () throw () { return templateFileName.get
(); }

void setIPAddress (char *ipAddr="") throw () {
ipAddress = (pchar) new char [strlen (ipAddr) + 1];
if (ipAddress.get ())
strcpy (ipAddress.get (), ipAddr);
ipAddress.get ()[strlen(ipAddr)] = '\0';
}

void setPort (int aPort=162) { port = aPort; }

void setcommunityName (char *aCommunityName="public") {
communityName = (pchar) new char [strlen (aCommunityName) + 1];
if (communityName.get ())
strcpy (communityName.get (), aCommunityName);
}

void setsnmpVersion (int snmpVer=2) { snmpVersion = snmpVer; }

void settemplateFileName (char *templFileName="") {
templateFileName = (pchar) new char [strlen (templFileName) + 1];
if (templateFileName.get ())
strcpy (templateFileName.get (), templFileName);
}

};
class SnmpTrapGen {
private:
char *ipAddress;
int port;
char *community;
int snmpVersion;
public:
SnmpTrapGen (char *ipAddress=NULL, int port=162, char
*community="public", int snmpVersion=2){
}
~SnmpTrapGen () {
if (ipAddress)
delete [] ipAddress;
if (community)
delete [] community;
}

sendTrap (char *alertToSend);

char *getIPAddress () { return ipAddress; }
int getPort () { return port; }
char *getcommunity () { return community; }
int getsnmpVersion () { return snmpVersion; }

void setIPAddress (char *ipAddr="") {
ipAddress = new char [strlen (ipAddr) + 1];
if (ipAddress)
strcpy (ipAddress, ipAddr);
}
void setPort (int aPort=162) { port = aPort; }
void setcommunity (char *aCommunity="public") {
community = new char [strlen (aCommunity) + 1];
if (community)
strcpy (community, aCommunity);
}
void setsnmpVersion (int snmpVer=2) { snmpVersion = snmpVer; }

};

Thank you again
Megh

Jul 23 '05 #9
Hi V,

When we declare, we use auto_ptr. As you might already know, auto_ptr
is a mechanism that provides ownership to take care of releasing the
memory that we allocate with a new.

However, while calling new, specifically for an inbuilt type, we
specify the size of memory that we request with in square brackets.

And once the memory is allocated, we could use it like an array. We
could index into it too.

Anyway thanks for your suggestion about the 'std::string'

Regards,
Megh

Jul 23 '05 #10
Meghavvarnam wrote:
When we declare, we use auto_ptr. As you might already know, auto_ptr
is a mechanism that provides ownership to take care of releasing the
memory that we allocate with a new.

However, while calling new, specifically for an inbuilt type, we
specify the size of memory that we request with in square brackets.

And once the memory is allocated, we could use it like an array. We
could index into it too.
That would be wrong. 'auto_ptr' destroys the object using 'delete'.
If you allocate it using 'new[]', you _have_to_ use 'delete[]' and not
'delete'. So, 'auto_ptr' is not a suitable mechanism here.
[...]


V
Jul 23 '05 #11
Meghavvarnam wrote:
Apologies for the confusion in my previous post. Am pasting the above
piece of code that works again. Have reformatted it.

Please note the usage of the member function get () to get the address
of an auto_ptr variable.
[...]


Your code has undefined behaviour. If it happens to work on your
system, it's no indication that it's correct.

V
Jul 23 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chucker | last post by:
Hi Folks, I got an error that drives me crazy because it only occurs sometimes and I can`t even reproduce it. I got a __gc class here is it`s header: #pragma once #include...
6
by: Anuradha | last post by:
Below is the code which was written VC++ 6.0 under windows environment. Executing the same throws: ------------------ Debug Error! Program: ccheck.exe DAMAGE: after normal block (#41) at...
5
by: Meghavvarnam | last post by:
Hi all, I just joined this group and am new to VC++. I wrote the code following the next para in C++ and used VC++ 6.0 Enterprise Edition to build and test. I had the following error message...
1
by: Adam Clauss | last post by:
I have a doc/view app w/ a CRichEditView. In it, I define: void CSyntaxView::ParseLine(long lineNum); This function starts off by getting the text of the specified line (into a CString): ...
18
by: John Salerno | last post by:
I'm still tyring to figure out what "Pythonic" means, and I have a feeling the answer to my question may fall into that category. Are block comments somehow unpythonic?
8
by: Alvin | last post by:
I'm making a very simple game in SDL, and I'm not asking for SDL help I hope - this looks like something C++ related, so I'll ask here. I have a class for a simple block, or tile, in the game,...
3
by: raan | last post by:
Whats wrong with the code ? delete tp; is throwing DAMAGE: After normal block(#56) at 0x00321480 Environment, VS2003, XP #include <iostream> #include <fstream> #include <string> #include...
1
by: Anjaneya | last post by:
Hi all, Here is the error message which i am getting: Damage: After normal block(#253) at 0x009E6A98 I am trying to calculate the dense disparity map for colour images. I am trying to do...
0
by: Now You Know | last post by:
Water Damage Restoration 24 hour open 7 Days Phone 1 877 364 5264 When disaster strikes such as a washing machine overflow, bath overflow, burst pipe, rainwater from balcony etc, water damage...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.