SnmpExtensionQuery系列函数使用注意事项

最近使用SnmpExtensionQuery函数时,获取信息程序运行结果正确,但程序运行在Debug状态时一直提示int 3 中断,Release版运行正常;在使用Google搜索及仔细阅读MSDN后才发现,程序和SNMP sever之间使用的是动态内存进行交换;
具体也就是你需要访问节点的OID( SnmpVarBind.name.ids)需要你的才程序动态申请内存(由Snmp Server进行释放),收到的返回值中不定长数据类型需要你的程序释放内存(
如SnmpVarBind.value.asnValue.string.stream);


GOOGLE仅搜索到一篇相关内容:

>As part of the development of an application I am working on, I need
>to get the MAC address of the local machine. I have found a way to do
>this using SNMP.
>
>However, I am having trouble integrating the code into the
>application. Can someone tell me why the first version of this
>program succeeds and the second program dies when calling
>SnmpExtensionQuery?
>[snip code]


Hi Tron,

I guess the problem has nothing to do with code organization: it's
probably just a side effect. IIRC, you must not use your nEntryType and
nMACAddress arrays directly when you initialize varBind[0].name.ids and
varBind[1].name.ids. You must use SnmpUtilMemAlloc to allocate a block
of identical size of SNMP-compliant memory for each OID, then you must
memcpy each OID array to its respective block, and initialize
varBind[xxx].name.ids to point at this block.

Also, you should call SnmpUtilMemFree() to free the system-allocated
memory at varBind[1].value.asnValue.address.stream, or you'll end up
with memory leaks.

Look carefully in the MSDN for details, SNMP memory management is real
tricky. Also, the netstatp sample at www.sysinternals.com may help (see
http://www.sysinternals.com/ntw2k/source/tcpview.shtml).


MSDN中描述如下:

It is important to note that the SNMP service and the extension agent may need to exchange dynamically allocated memory during a call to the SnmpExtensionQuery SnmpVarBind structure it passes to the extension agent. However, the extension agent must release this memory in order to replace the object identifier when it processes a Get Next request. The extension agent allocates dynamic memory for variable-length object types. The SNMP service releases this memory after the object is placed in the response PDU. function. The service dynamically allocates the object identifier in each

In order to avoid heap corruption and memory leaks, both the SNMP service and the extension agent must use memory allocation routines that resolve to the same heap. The extension agent must use the SnmpUtilMemAlloc function to allocate memory that it passes to the SNMP service. It must use the SnmpUtilMemFree function to release the memory the service passes back to the extension agent. These functions are located in the utility dynamic-link library SNMPAPI.DLL.



教训: 仔细阅读MSDN,不要怕英文

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值