您的位置:首页 > 产品设计 > UI/UE

SnmpExtensionQuery系列函数使用注意事项

2007-06-11 13:24 633 查看
最近使用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,不要怕英文
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: