1

我正在处理下面的示例,但是当我使用 valgrind 运行它时会出现内存泄漏

static struct mosquitto *m = NULL;

int main(){

    mosquitto_lib_init();
    printf("LIBMOSQUITTO %d\n", LIBMOSQUITTO_VERSION_NUMBER);

    if ((m = mosquitto_new("rtr", 1, NULL)) == NULL) {
        fprintf(stderr, "Out of memory.\n");
        exit(1);
    }

    int rc = mosquitto_tls_set(m,
            "/home/ca.crt",     /* cafile */
            NULL,           /* capath */
            "/home/client.crt",     /* certfile */
            "/home/client.key",     /* keyfile */
            NULL            /* pw_callback() */
            );

    if (rc != MOSQ_ERR_SUCCESS) {
        fprintf(stderr, "Cannot set TLS CA: %s (check path names)\n",
                mosquitto_strerror(rc));
        exit(3);
    }
#if 1
    mosquitto_tls_opts_set(m,
            SSL_VERIFY_PEER,
            NULL,           /* tls_version: "tlsv1.2", "tlsv1" */
            NULL            /* ciphers */
            );
    mosquitto_tls_insecure_set(m, 1);
#endif
    if ((rc = mosquitto_connect(m, "localhost", 8884, 20)) != MOSQ_ERR_SUCCESS) {
        fprintf(stderr, "%d: Unable to connect: %s\n", rc,
                mosquitto_strerror(rc));
        perror("");
        exit(2);
    }

    //mosquitto_loop_forever(m, -1, 1);
    mosquitto_destroy(m);
    mosquitto_lib_cleanup();
}

Valgrind 输出:

==4264== HEAP SUMMARY:
==4264==     in use at exit: 64 bytes in 2 blocks
==4264==   total heap usage: 4,913 allocs, 4,911 frees, 364,063 bytes allocated
==4264== 
==4264== LEAK SUMMARY:
==4264==    definitely lost: 0 bytes in 0 blocks
==4264==    indirectly lost: 0 bytes in 0 blocks
==4264==      possibly lost: 0 bytes in 0 blocks
==4264==    still reachable: 64 bytes in 2 blocks
==4264==         suppressed: 0 bytes in 0 blocks
==4264== Rerun with --leak-check=full to see details of leaked memory
==4264== 
==4264== For counts of detected and suppressed errors, rerun with: -v
==4264== Use --track-origins=yes to see where uninitialised values come from
==4264== ERROR SUMMARY: 13582 errors from 542 contexts (suppressed: 0 from 0)

我该如何解决这些问题?

4

0 回答 0