gsoap和资源管理

时间:2016-10-28 15:02:07

标签: c gsoap

我使用gsoap在我的C程序中实现一些Web服务。

我像这样使用gsoap:

int motion_detector_check_status(const char *endpoint, 
        const motion_detector_subscription_result_t *sr, int *status) {
    int rc = EXIT_SUCCESS;
    double _timeout = difftime(sr->termination_time, sr->current_time);
    char timeout[TIMEOUT_MAX_LEN];
    struct soap *soap = soap_new();
    struct _tev__PullMessages request;
    struct _tev__PullMessagesResponse response;
    char *motion_state;

    if (!soap) return EXIT_FAILURE;

    snprintf(timeout, TIMEOUT_MAX_LEN, "PT%.0fS", _timeout < 5.0 ? _timeout : 5.0);

    request.MessageLimit = 1;
    request.Timeout = _timeout < 5.0 ? (int64_t)_timeout : 5.0;
    request.__any = timeout;
    request.__size = sizeof(timeout) + 1;

    rc = soap_call___tev__PullMessages(soap, endpoint, SOAP_ACTION_PULL_MESSAGE,
            &request, &response);

    if (rc != SOAP_OK) {
        soap_print_fault(soap, stderr);
        goto end;
    }

    if (response.wsnt__NotificationMessage && 
            response.wsnt__NotificationMessage->Topic &&
            response.wsnt__NotificationMessage->Topic->__any) {
        if (strstr(response.wsnt__NotificationMessage->Topic->__any, MOTION_ALARM_SRC)) {
            motion_state = strstr(response.wsnt__NotificationMessage->Message.__any,
                    "<tt:SimpleItem Name=\"State\" Value=");

            if (strcasestr(motion_state, "true"))
                *status = 1;
            else
                *status = 0;
        }
    }

    motion_detector_check_status_free_response(&response);
end:
    soap_end(soap);
    soap_destroy(soap);
    soap_free(soap);
    return rc;
}

一切都运作良好,但struct _tev__PullMessagesResponse有一些指向char和其他结构的指针。

当我在valgring中运行我的程序时,它告诉我我有内存泄漏,response内部通过指针引用的资源在motion_detector_check_status结束后不可用。当然我可以自己编写这个函数,但是我想知道gsoap是否提供了释放这些资源的函数?

提前致谢,对不起我的英语。

1 个答案:

答案 0 :(得分:0)

好吧,在soapcpp2的docomentation中走得更深,我发现选项-Ed会为gsoap使用的每种类型生成soap_del_T个宏。