读书人

ACE 的 BUG ? ACE_Process_Options:se

发布时间: 2013-07-09 09:50:48 作者: rapoo

ACE 的 BUG ? ACE_Process_Options::setenv
本帖最后由 mymtom 于 2013-03-14 16:30:44 编辑 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


/**
* @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;
}

这么冷的帖
[解决办法]
ACE 的 BUG ? ACE_Process_Options:setenv,该怎么解决
[解决办法]
呃,这重载不起作用了?直接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, ...)。 所以说,,,

读书人网 >UNIXLINUX

热点推荐