473,766 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 -
~TrapDestinatio n () :

"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 templateFileNam e;
void (*old_unexpecte d) ();

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

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

else
cout << "uncaught_excep tion() is FALSE" <<
endl;
}

char *getIPAddress () throw () { return ipAddress; }
int getPort () throw () { return port; }
char *getcommunityNa me () throw () { return communityName; }
int getsnmpVersion () throw () { return snmpVersion; }
char *gettemplateFil eName () throw () { return
templateFileNam e; }

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 setcommunityNam e (char *aCommunityName ="public") {
communityName = (pchar) new char (strlen
(aCommunityName ) + 1);
if (communityName)
strcpy (communityName, aCommunityName) ;
}

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

void settemplateFile Name (char *templFileName= "") {
templateFileNam e = (pchar) new char (strlen
(templFileName) + 1);
if (templateFileNa me)
strcpy (templateFileNa me, templFileName);
}
};

class SnmpTrapGen {
private:
char *ipAddress;
int port;
char *community;
int snmpVersion;
public:
SnmpTrapGen (char *ipAddress=NULL , int port=162, char
*community="pub lic", 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="pu blic") {
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_terminato r() {
cout << "terminate called local_terminato r !!!" << 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(l ocal_terminator *);
td.setIPAddress ("127.0.0.1" );
td.setcommunity Name ();
td.settemplateF ileName ("tempname") ;
cout << "IP address = " << td.getIPAddress () << endl;
printf ("communityN ame = %s.\n", td.getcommunity Name
());
printf ("templateFileN ame = %s.\n",
td.gettemplateF ileName ());
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 4471
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 -
~TrapDestinatio n () :

"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
"Meghavvarn am" <me********@yah oo.com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.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 templateFileNam e;
void (*old_unexpecte d) ();

public:
TrapDestination () : ipAddress (NULL), communityName (NULL),
templateFileNam e (NULL) {
}
~TrapDestinatio n () throw () {
cout << "TrapDestinatio n Destructor" << endl;
//set_unexpected( old_unexpected) ; // restore org handler
if (uncaught_excep tion())
cout << "uncaught_excep tion() is TRUE" << endl;
else
cout << "uncaught_excep tion() is FALSE" << endl;
}

char *getIPAddress () throw () { return ipAddress.get (); }
int getPort () throw () { return port; }
char *getcommunityNa me () throw () { return communityName.g et (); }
int getsnmpVersion () throw () { return snmpVersion; }
char *gettemplateFil eName () throw () { return templateFileNam e.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 setcommunityNam e (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 settemplateFile Name (char *templFileName= "") {
templateFileNam e = (pchar) new char [strlen (templFileName) + 1];
if (templateFileNa me.get ())
strcpy (templateFileNa me.get (), templFileName);
}
};
class SnmpTrapGen {
private:
char *ipAddress;
int port;
char *community;
int snmpVersion;
public:
SnmpTrapGen (char *ipAddress=NULL , int port=162, char
*community="pub lic", 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="pu blic") {
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 templateFileNam e;
void (*old_unexpecte d) ();

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

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

char *getIPAddress () throw () { return ipAddress.get (); }
int getPort () throw () { return port; }
char *getcommunityNa me () throw () { return communityName.g et (); }
int getsnmpVersion () throw () { return snmpVersion; }
char *gettemplateFil eName () throw () { return templateFileNam e.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 setcommunityNam e (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 settemplateFile Name (char *templFileName= "") {
templateFileNam e = (pchar) new char [strlen (templFileName) + 1];
if (templateFileNa me.get ())
strcpy (templateFileNa me.get (), templFileName);
}

};
class SnmpTrapGen {
private:
char *ipAddress;
int port;
char *community;
int snmpVersion;
public:
SnmpTrapGen (char *ipAddress=NULL , int port=162, char
*community="pub lic", 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="pu blic") {
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

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

Similar topics

4
5152
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 "../empLib/empImg.h"
6
5540
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 0x00300160 (Press Retry to debug the application) ------------------
5
859
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 because of an exception that gets thrown when I call delete ipAddress; in the destructor - ~TrapDestination () :
1
2393
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): CRichEditCtrl* ctrl = &GetRichEditCtrl(); int lineLen = ctrl->LineLength(lineNum); char* buf = new char; ctrl->GetLine(lineNum, buf, lineLen);
18
2163
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
3332
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, which can be either on or off. I'm having trouble with the constructor though. class block { private: SDL_Surface *screen;
3
1456
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 <set> #include <stack> #include <sstream>
1
1546
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 this in C using OpenCV. Here is the sample code where i am getting error message:
0
1228
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 restoration procedures must be followed. It is not as simple as just sucking up the water and cleaning the carpet and letting the area dry naturally. It is not the wet carpet that is the problem it is the underlay and the wood or concrete floor...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10009
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8835
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.