在 libuv 应用程序中删除后使用?
Use-after-delete in libuv app?
我正在尝试使用 libuv 以跨平台方式启动进程。为了测试该库,我编写了一个调用 sleep 1
然后退出的小型 C++ 应用程序。问题是有时(约 5% 的时间)它会因 libuv 的以下错误而崩溃:
EFAULT bad address in system call argument
这是我的代码:
#include <iostream>
#include <string>
#include <memory>
#include <cstring>
#include <uv.h>
char* a;
char* b;
char** args;
std::string error_to_string(int const& error) {
return std::string(uv_err_name(error)) +
" " +
std::string(uv_strerror(error));
}
void on_exit(uv_process_t* req, int64_t exit_status, int term_signal) {
std::cout << "I'm back! " << std::endl;
std::cout << "exit_status " << exit_status
<< " term_signal " << term_signal << std::endl;
uv_close((uv_handle_t*)req, nullptr);
}
int main(int argc, const char** argv) {
auto* loop = new uv_loop_t();
uv_loop_init(loop);
auto* process = new uv_process_t();
uv_process_options_t options = {};
a = new char[100];
b = new char[100];
strcpy(a, "sleep[=11=]");
strcpy(b, "1[=11=]");
args = new char*[2];
args[0] = a;
args[1] = b;
options.exit_cb = on_exit;
options.file = "sleep";
options.args = args;
std::cout << "Going to sleep..." << std::endl;
int const r = uv_spawn(loop, process, &options);
if (r < 0) {
std::cout << error_to_string(r) << std::endl;
return 1;
}
uv_run(loop, UV_RUN_DEFAULT);
return 0;
}
我正在使用 libuv 1.11.0、Clang 4.0.1 和 C++ 14。
你能发现我的错误吗?
您的 args
数组设置错误。应该是:
args = new char*[3];
args[0] = a;
args[1] = b;
args[2] = NULL;
否则uv_spawn
不知道数组的末尾在哪里
我正在尝试使用 libuv 以跨平台方式启动进程。为了测试该库,我编写了一个调用 sleep 1
然后退出的小型 C++ 应用程序。问题是有时(约 5% 的时间)它会因 libuv 的以下错误而崩溃:
EFAULT bad address in system call argument
这是我的代码:
#include <iostream>
#include <string>
#include <memory>
#include <cstring>
#include <uv.h>
char* a;
char* b;
char** args;
std::string error_to_string(int const& error) {
return std::string(uv_err_name(error)) +
" " +
std::string(uv_strerror(error));
}
void on_exit(uv_process_t* req, int64_t exit_status, int term_signal) {
std::cout << "I'm back! " << std::endl;
std::cout << "exit_status " << exit_status
<< " term_signal " << term_signal << std::endl;
uv_close((uv_handle_t*)req, nullptr);
}
int main(int argc, const char** argv) {
auto* loop = new uv_loop_t();
uv_loop_init(loop);
auto* process = new uv_process_t();
uv_process_options_t options = {};
a = new char[100];
b = new char[100];
strcpy(a, "sleep[=11=]");
strcpy(b, "1[=11=]");
args = new char*[2];
args[0] = a;
args[1] = b;
options.exit_cb = on_exit;
options.file = "sleep";
options.args = args;
std::cout << "Going to sleep..." << std::endl;
int const r = uv_spawn(loop, process, &options);
if (r < 0) {
std::cout << error_to_string(r) << std::endl;
return 1;
}
uv_run(loop, UV_RUN_DEFAULT);
return 0;
}
我正在使用 libuv 1.11.0、Clang 4.0.1 和 C++ 14。
你能发现我的错误吗?
您的 args
数组设置错误。应该是:
args = new char*[3];
args[0] = a;
args[1] = b;
args[2] = NULL;
否则uv_spawn
不知道数组的末尾在哪里