M
Meghavvarnam
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_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; }
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
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_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; }
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