📄 putenv.cpp
字号:
#include "putenv.h"#include <QTextStream>#include <QProcess>#include <QList>#include <QStringList>#include <cstring>#include <stdlib.h>class EnvStrings : public QList<char*> { public: ~EnvStrings() { /* This works: */ int l = count(); for (int i=0; i < l; ++i) { delete[] at(i); } /* The below foreach() causes a segfault. foreach (char* ptr, *this) { delete[] ptr; } */ /** In order to understand why, you need to know that a foreach always makes a copy of the collection. Since we are copying the object we are destroying, and copy needs to be destroyed too, we have infinite recursion, combined with repeated deletions the same memory. */ }};void qstd::putenv(QString key, QString value) { static EnvStrings newEnv; QString str = QString("%1=%2").arg(key).arg(value); int len = str.length() + 1; char* charStar = new char[len+1]; strncpy(charStar, str.toAscii(), len); newEnv << charStar; ::putenv(charStar); /* call Global version */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -