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 11 4345
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
"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
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
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
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
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
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; }
};
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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):
...
|
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?
|
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,...
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
| |