损坏:正常阻塞后(#45) [英] DAMAGE: after Normal block (#45)

查看:62
本文介绍了损坏:正常阻塞后(#45)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我刚刚加入这个小组并且是VC ++的新手。我编写了下面的代码,然后使用VC ++ 6.0企业版构建并使用VC ++ 6.0企业版进行
测试。


我有一个异常,因为当我调用delete [] ipAddress时会抛出

;在析构函数中 -

~TrapDestination():


损坏:正常阻止后(#45)


// ~~~~~~~~~~~

// plugIn.h

// ~~~~~~~~~~ 〜

#include< memory>

#include< iostream>

#include< exception>

#include< assert.h>

使用命名空间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 <<如果(ipAddress)

删除[] ipAddress;

if(communityName)
communityName;

if(templateFileName)

delete [] templateFileName;


// set_unexpected(old_unexpecte * d); //恢复组织

处理程序

if(uncaught_exception())

cout<< uncaught_exception()为TRUE << endl;


else

cout<< uncaught_exception()为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);

}

};


类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(){返回端口; }

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);

如果(社区)

strcpy(社区,社区);

}

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


};

// ~~~~~~~~~~~~

// plugIn.cpp

// ~~~~~~~~~~~~

#include" net-snmp \\\
et-snmp-config.h" ;

#include" net-snmp \\\
et-snmp-includes.h"

#include" plugIn.h"


void local_terminator(){

cout<< 终止称为local_terminator !!! <<结束;

退出(1);

}


void(* old_terminate)();


void my_unexpected(){

//这不能返回 - 相反它可以调用std :: terminate()或

抛出异常


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地址= << td.getIPAddress()<< endl;

printf(" communityName =%s。\ n",td.getcommunityName

());

printf(" ; templateFileName =%s。\ n",

td.gettemplateFileName());

返回0;

} catch(bad_exception const &){b / b
printf(" Caught bad_exception\\\
"); //虽然这样一个

例外是

永远不会抛出

返回0;

} catch(... ){

cout<< Inside main'抓住所有 <<结束;

返回0;

}

}

任何亮点:

1.为什么删除导致抛出此异常可能会有所帮助。

2.解决方法是什么。


将很棒!


只是要补充一下,类似于这个场景的早期帖子有一个回复

,在使用new为字符串分配空间时,请确保为''\0''添加1

。但是我已经在我的代码中处理了这个问题并且

但是当它遇到删除时我有这个例外。


我真的需要专家的帮助。


谢谢,

Meghavvarnam Satish

解决方案

Meghavvarnam写道:

我刚刚加入这个小组并且是VC ++的新手。我在C ++的下一篇文章中编写了代码,并使用VC ++ 6.0 Enterprise Edition进行构建和测试。


除非行为是针对该编译器的,并且你尝试了其他的行为,并且发现了,你使用的编译器是无关紧要的。我们在这里不讨论

产品。我们讨论语言。

我有以下错误消息,因为当我调用delete [] ipAddress时会抛出异常。在析构函数中 -
〜TrapDestination():

损坏:正常阻止后(#45)
[...]



这通常是由内存溢出造成的(写入超出数组的b / b
的界限)或使用未初始化的指针(巧合只是
$ b) $ b指向一些真实的记忆)。使用内存调试工具(比如

BoundsChecker或Purify或Insure ++)来查找问题,或者手动在

调试器中进行。你的目标是学习你的工具,而不是使用这个

新闻组作为远程调试器。


V

" Meghavvarnam" <我******** @ yahoo.com>在消息中写道

news:11 ********************* @ g49g2000cwa.googlegro ups.com


[snip]

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



new的语法错误(在这里和其他地方你用它)。它应该是



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


ie,使用方括号,而不是圆括号(你的演员到pchar是多余的,但这不是造成问题的原因)。方括号

给出一系列字符,这就是你想要的。圆括号是指

你指定单个对象的构造函数的参数。


-

John Carson


您可以尝试使用auto_ptr而不是使用原始指针。我没有

看到你想在这里使用原始指针的任何具体原因。
Ik


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

解决方案

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


这篇关于损坏:正常阻塞后(#45)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆