简体   繁体   English

libgit2和Qt错误

[英]libgit2 and Qt error

I want to use git from an Qt application. 我想从Qt应用程序使用git。 So far, I use QProcess, but I do not want to use that. 到目前为止,我使用QProcess,但是我不想使用它。 So I found libgit2. 所以我找到了libgit2。

This works as expected: 这按预期工作:

#include <QApplication>
#include "git2.h"
int main(int argc, char* argv[])
{
    git_repository* repo = 0;
    git_clone(&repo, "/path_to/barerep", "/path_to/test_clone", NULL);
    git_repository_free(repo);
    repo = 0;
}

But here, git_clone crashes. 但是在这里,git_clone崩溃了。

int main(int argc, char* argv[])
{
    QApplication a(argc, argv);

    git_repository* repo = 0;
    git_clone(&repo, "/path_to/barerep", "/path_to/test_clone", NULL);
    git_repository_free(repo);
    repo = 0;

    return a.exec();
}

The error is: *** Error in `/path_to/gittest': free(): invalid pointer: 0x09d53a88 *** 错误是:***`/ path_to / gittest'中的错误:free():无效的指针:0x09d53a88 ***

Any suggestions? 有什么建议么? Of course, omitting QApplication is not an alternative. 当然,省略QApplication并不是替代方案。 The same error occurs without return a.exec() . return a.exec()会发生相同的错误。

Note: Actually, there is a class GitRepository with a method clone(const QString & url) (the path is stored somewhere in the class). 注意:实际上,有一个带有方法clone(const QString & url) GitRepository类(路径存储在该类的某个位置)。

Again, this works 同样,这有效

int main(int argc, char* argv[])
{
    GitRepository g;
    g.clone("path_to/barerep");
}

But this does not. 但这不是。 (QObject!) (QObject的!)

int main(int argc, char* argv[])
{
    QObject();  // <--
    GitRepository g;
    g.clone("path_to/barerep");
}

does not. 才不是。

bool GitRepository::clone(const QString & url)
{
    git_repository* repo = 0;
    git_clone(&repo, CSTR(url), CSTR(path()), NULL);
    git_repository_free(repo);
    repo = 0;

    //loadFromTempDir();

    return true;
}

Replacing QApplication by QObject in the first example suppresses the error. 在第一个示例中,用QObject替换QApplication可消除该错误。

You need to call git_libgit2_init() before calling any other libgit2 functions. 您需要先调用git_libgit2_init(),然后再调用其他任何libgit2函数。 As the documentation says: 如文档所述:

This function must the called before any other libgit2 function in order to set up global state and threading. 为了设置全局状态和线程,必须在任何其他libgit2函数之前调用此函数。

These kind of errors are really hard to find. 这些错误确实很难找到。 I also had problems mixing Qt libraries with other libraries. 我还遇到了将Qt库与其他库混合的问题。 The trick is to organize your code in a way that there is only one library included in one compilation unit. 诀窍是采用一种编译单元中仅包含一个库的方式来组织代码。

Create a class, which wraps around libgit2, and only include the libgit2 headers in the cpp file of this class. 创建一个环绕libgit2的类,并且仅在此类的cpp文件中包含libgit2标头。 Do not include any qt headers in the same file. 请勿在同一文件中包含任何qt标头。

Only refer to libgit throw your wrapper. 只引用libgit抛出包装。 Sure it seems like a lot of work, but as a result your code will be cleaner, and these misterious errors will be gone. 当然,这似乎需要大量工作,但结果是您的代码将更加简洁,这些繁琐的错误也将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM