为什么 QString 到 const char* 的转换会在 Windows 上生成 Debian 标识符?
我正在尝试将 libssh2 包装在 Qt 中,并具有以下代码:
const char* username = inUsername.toLocal8Bit().data();
const char* password = inPass.toLocal8Bit().data();
问题是,用户名和密码无法连接到系统。为什么?
因为,根据调试器,
username "5.1p1 Debian-6ubuntu2"
password "5.1p1 Debian-6ubuntu2"
这些不是我为用户名或密码给出的值。我尝试过 toAscii、toLatin1 和附加(或不附加).data()。尽管如此,我还是得到了这些值,而不是预期值。我使用的是 Windows,这就是为什么它更令人不安,因为据我所知,我没有任何东西是在 Debian 或 Ubuntu 上编译的。
这是怎么回事?
I'm trying to wrap libssh2 in Qt, and have the following code:
const char* username = inUsername.toLocal8Bit().data();
const char* password = inPass.toLocal8Bit().data();
Problem is, that username and password doesn't connect to the system. Why?
Because, according to the debugger,
username "5.1p1 Debian-6ubuntu2"
password "5.1p1 Debian-6ubuntu2"
Those are not the values I've given for the username or password. I've tried toAscii, toLatin1, and appending (or not) the .data(). Still, I get these values, instead of the expected values. I'm on Windows, which is why it's even more troubling, since, as far as I can tell, nothing I have was compiled on Debian or Ubuntu.
What's going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码:
相当于:
你看到发生了什么了吗?当该语句执行时,临时
QByteArray
已被编译器再次删除。由于data()
仅返回指向内部QByteArray
缓冲区的指针,因此username
现在指向已删除/释放的内存。要解决此问题,请使用
username
和password
QByteArray
代替const char*
,并使用username.data()
,password.data()
而不是您之前使用username
,password
的地方。This code:
is equivalent to this:
Do you see what's going on? By the time the statement has executed, the temporary
QByteArray
has been deleted by the compiler again. Sincedata()
only returns a pointer to the internalQByteArray
buffer,username
now points to deleted/freed memory.To solve the problem, make
username
andpassword
QByteArray
s instead ofconst char*
s, and useusername.data()
,password.data()
instead where you usedusername
,password
before.