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,不要怕英文
具体也就是你需要访问节点的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,不要怕英文
相关文章推荐
- python2.7系列下的print函数使用注意事项
- VC中使用OnSize函数,设置控件位置的注意事项
- 函数的使用及注意事项
- fgets函数使用注意事项
- Linux socket 编程中 write 函数使用的注意事项
- Linux使用带时区转换的时间函数注意事项(负负得正)
- vector的所有函数和使用的注意事项
- setbuf函数使用注意事项
- PHP IN_ARRAY 函数使用注意事项
- ORACLE分区表梳理系列(一)- 分区表概述、分类、使用方法及注意事项
- java 分隔符函数split的使用注意事项
- C标准库函数strtok使用注意事项
- Java 分隔符函数split的使用注意事项
- 5.1 Python 函数-默认参数,*args,**kwargs的使用;作用域-局部空间使用全局变量需要注意的事项等
- lucene.net 使用过程中的 几个注意事项(含termquery 和QueryParser 的区别)
- socket 编程下 inet_ntoa() 函数的使用与注意事项
- vector size函数使用注意事项
- sql 函数的使用注意事项
- 《Visual C# 最佳实践》第四章 函数 (四):使用函数注意事项
- Linux C ftruncate 函数清空文件注意事项(要使用 lseek 重置偏移量)