Qt4 包装器导致分段错误

发布于 2024-09-27 15:29:13 字数 1635 浏览 4 评论 0原文

我目前正在为应用程序作为插件编写一组 Qt4 包装函数(因为我个人认为用纯 C 编写扩展更容易完成)。

起初,我认为这可以通过简单地抽象以下内容来完成:

#include <QApplication>
#include <QPushButton>

int main(int argc, char *argv[])
{
    QApplication* app = new QApplication(argc, argv);
    QPushButton* hello = new QPushButton("Hello world!");
    hello->resize(500, 300);
    hello->show();
    return app->exec();
}

进入此(事实上,这是我的包装器的 main.c 中的代码):

#include "bind.h"

int main(int argc, char* argv[])
{
    gui_application_t* app;
    gui_pushbutton_t* hello;
    app = gui_application_new(argc, argv);
    hello = gui_pushbutton_new("Hello World");
    gui_pushbutton_resize(hello, 100, 30);
    gui_pushbutton_show(hello);
    return gui_application_exec(app);
}

虽然前者有效(如预期),但我遇到了分段错误在后者明显随机的地方。我希望我的版本能与以前的版本相同...但恐怕根本不会:-(

运行二进制文件后,输出通常是:

 *** GUI Debug: gui_application_new: ctx->app = hex_addr
 *** GUI Debug: gui_pushbutton_new: ctx->button = hex_addr
 *** GUI Debug: gui_pushbutton_resize: ctx->button = hex_addr
 *** GUI Debug: gui_pushbutton_show: ctx->button = hex_addr
Segmentation fault

但是,即使删除对 的调用gui_pushbutton_* (并且仅调用 gui_application_newgui_application_exec),应用程序仍然会崩溃,但在 gui_application_exec

运行输出 。使用 gdb 的二进制文件:http://codepad.org/wBifH1B2

来源:http://dl.dropbox.com/u/863332/wrapper.tar.bz2

非常感谢任何帮助,因为这让我非常困惑......

I'm currently writing a set of wrapper functions of Qt4 for an application as plugin (because I personally think that writing extensions in plain C is easier to accomplish).

At first I though this could be done by simply abstracting the following:

#include <QApplication>
#include <QPushButton>

int main(int argc, char *argv[])
{
    QApplication* app = new QApplication(argc, argv);
    QPushButton* hello = new QPushButton("Hello world!");
    hello->resize(500, 300);
    hello->show();
    return app->exec();
}

Into this (as a matter of fact, that's the code in main.c of my wrapper):

#include "bind.h"

int main(int argc, char* argv[])
{
    gui_application_t* app;
    gui_pushbutton_t* hello;
    app = gui_application_new(argc, argv);
    hello = gui_pushbutton_new("Hello World");
    gui_pushbutton_resize(hello, 100, 30);
    gui_pushbutton_show(hello);
    return gui_application_exec(app);
}

While the former works (as expected), I'm getting segmentation faults at apparently random places in the latter. I hoped my version would just do the same as the former... but I'm afraid it doesn't at all :-(

After running the binary, the output is usually:

 *** GUI Debug: gui_application_new: ctx->app = hex_addr
 *** GUI Debug: gui_pushbutton_new: ctx->button = hex_addr
 *** GUI Debug: gui_pushbutton_resize: ctx->button = hex_addr
 *** GUI Debug: gui_pushbutton_show: ctx->button = hex_addr
Segmentation fault

However, even after removing the calls to gui_pushbutton_* (and only calling gui_application_new and gui_application_exec), the application will still crash, but in gui_application_exec.

Output from running the binary with gdb: http://codepad.org/wBifH1B2

Sources: http://dl.dropbox.com/u/863332/wrapper.tar.bz2

Any help is very appreciated, as this got me very puzzled...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心清如水 2024-10-04 15:29:13

对 gui_allocate: 的调用

ctx = gui_allocate<gui_pushbutton_t>(sizeof(gui_pushbutton_t*));

表明该函数的工作方式类似于 malloc,并获取请求的大小并将地址返回到该大小的缓冲区,但事实并非如此。这是一个模板函数,它的每个版本都知道类型。它实际上更像是 calloc 而不是 malloc,因为调用:

ctx = gui_allocate<gui_pushbutton_t>(1);

将为一个 gui_pushbutton_t 分配足够的空间。 gui_allocate 的参数是数组长度,而不是元素大小。

您分配了一个长度为 sizeof(gui_pushbutton_t *) 的数组。

我真的不明白这会如何导致分段错误,因为它应该会导致分配的内存大于所需的内存,除非是构造函数或代表这些多余数组成员的其他操作导致了您的问题。

A call to gui_allocate:

ctx = gui_allocate<gui_pushbutton_t>(sizeof(gui_pushbutton_t*));

suggests that the function works like malloc and takes a requested size and returns an address to a buffer of that size, but that is not the case. This is a template-ed function, and each version of it knows the type. It is really more like calloc than malloc because the call:

ctx = gui_allocate<gui_pushbutton_t>(1);

would allocate enough space for one gui_pushbutton_t. gui_allocate's parameter is an array length, not an element size.

You were allocating an array of them with length sizeof(gui_pushbutton_t *).

I don't really see how that would cause segmentation faults since it should result in a larger memory than needed being allocated, unless it is either the constructor or some other action on behalf of these excess array members that is causing your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文