1 注意参数的型别和函数参数的匹配
检查COMMLIB部分,发现GCS部分一个告警。
pet_gcs_interface.cpp:212: warning: passing NULL to non-pointer argument 3 of 'ssize_t ACE_SOCK_Stream::send_n(const void*, size_t, int, const ACE_Time_Value*, size_t*) const'
检查代码。报告告警的地方在。
petappframe->FrameHeadEncode();
ret = client_stream.send_n((void *)petappframe,len,NULL);
跳过去检查了一下send_n函数的重载函数,发现了问题。ACE的send_n有两个重载函数,
/// Try to send exactly @a len bytes from @a buf to the connection socket.
ssize_t send_n (const void *buf,
size_t len,
int flags,
const ACE_Time_Value *timeout = 0,
size_t *bytes_transferred = 0) const;
/// Try to send exactly @a len bytes from @a buf to the connected socket.
ssize_t send_n (const void *buf,
size_t len,
const ACE_Time_Value *timeout = 0,
size_t *bytes_transferred = 0) const;
对于ret = client_stream.send_n((void *)petappframe,len,NULL); 这个调用,上面两个函数都可以匹配,所以对编译器要么无法正确编译,要么要选择一个。(其实无法编译应该更好)。原来的开放者希望匹配的函数应该是ssize_t send_n (const void *buf,size_t len, const ACE_Time_Value *timeout = 0,size_t *bytes_transferred = 0) const。但是由于使用了NULL,这个可爱的参数,(#define NULL 0),第3个参数被当作了int参数。实际匹配的函数成了ssize_t send_n (const void *buf,size_t len,int flags, const ACE_Time_Value *timeout = 0,size_t *bytes_transferred = 0) const;。
这个代码应该改为。明确这个指针的类型,避免编译错误。
ACE_Time_Value *time_wait = NULL;
ret = client_stream.send_n((void *)petappframe,len,time_wait);
Effect C++里面应该有一节讲过这个问题。
问题教训和总结:
注意参数的型别和函数参数的匹配,
对于重载函数的参数要精心设计,避免出现这样参数,ACE这两个函数的设计就会导致这个问题,所以它的参数设计其实是失败。
对于NULL是什么要有清楚认识,NULL在C++中往往就是被定义为0,而不是((void *)0),所以格外要当心。