ACE 的 BUG ? ACE_Process_Options::setenv
C++NPv1 8.3 的例子程序,会 Memory Fault.
#109 0x2838a53e in vsnprintf () from /lib/libc.so.7
#110 0x281aa607 in ACE_Process_Options::setenv (this=0xbfbfe094, variable_name=0x8048fad "PROGRAM=%s",
format=0xbfbfed16 "factorial") at OS_NS_stdio.inl:1044
#111 0x08048b99 in main (argc=1, argv=0xbfbfeba4) at factorial.cpp:25
发现希望调用的函数是
int ACE_Process_Options::setenv (const ACE_TCHAR *format, ...),
结果调用的却是
int ACE_Process_Options::setenv (const ACE_TCHAR *variable_name,
const ACE_TCHAR *format, ...)
这样设计函数有问题啊,运动会口号
options.setenv ("PROGRAM=%s", ACE::basename (argv[0]));
可以两个都可以调用啊。
8.3 The ACE_Process_Options Class
C/C++ code?/** * @file factorial.cpp * @brief */ #include "ace/OS.h" #include "ace/ACE.h" #include "ace/Process.h" int main(int argc, char *argv[]) { ACE_Process_Options options; FILE *fp = NULL; char *n_env = NULL; int n; if (argc == 1) { // Top-level process. n_env = ACE_OS::getenv ("FACTORIAL"); n = n_env == 0 ? 0 : atoi (n_env); options.command_line ("%s %d", argv[0], n == 0 ? 10 : n); const char *working_dir = ACE_OS::getenv ("WORKING_DIR"); if (working_dir) options.working_directory (working_dir); fp = fopen ("factorial.log", "a"); options.setenv ("PROGRAM=%s", ACE::basename (argv[0])); } else { fp = fopen ("factorial.log", "a"); if (atoi (argv[1]) == 1) { // Base case fprintf (fp, "[%s|%d]: base case\n", ACE_OS::getenv ("PROGRAM"), ACE_OS::getpid ()); fclose (fp); return 1; } else { n = atoi (argv[1]); options.command_line ("%s %d", argv[0], n - 1); } } ACE_Process child; // Make ''recursive'' call. child.spawn (options); child.wait (); int factorial = n * child.exit_code (); // Compute n factorial fprintf (fp, "[%s | %d]: %d! == %d\n", ACE_OS::getenv ("PROGRAM"), ACE_OS::getpid (), n, factorial); fclose (fp); return 0; }
这重载不起作用了?直接options.setenv ("%s=%s", "PROGRAM", ACE::basename(argv[0]));算了。。。
发现希望调用的函数是
int ACE_Process_Options::setenv (const ACE_TCHAR *format, ...),
结果调用的却是
int ACE_Process_Options::setenv (const ACE_TCHAR *variable_name,
const ACE_TCHAR *format, ...)
函数调用有个最佳匹配的问题,当只有一个参数时,肯定调用int ACE_Process_Options::setenv (const ACE_TCHAR *format, ...), 而大于等于两个参数时就是int ACE_Process_Options::setenv (const ACE_TCHAR *variable_name,
const ACE_TCHAR *format, ...)。 所以说,,,
char buf[128] = {0}; snprintf(buf, 128, "PROGRAM=%s", ACE::basename(argv[0])); options.setenv(buf);