2013-01-07 18 views
3

我正在開發一個簡單的基於moongose的web服務器發送一個文件作爲參數通過HTTP傳遞,無論請求是什麼,但每次請求我得到一個堆棧溢出錯誤。對每個請求的貓鼬堆棧溢出

這裏是我的代碼:

#include <stdio.h> 
#include <string.h> 
#include "mongoose.h" 

// file path 
char *path; 

static void *callback(enum mg_event event, struct mg_connection *conn) 
{ 
    const struct mg_request_info *request_info = mg_get_request_info(conn); 
    mg_send_file(conn, path); 

    return ""; 
} 

int main(int argc,char *argv[]) 
{  
    struct mg_context *ctx; 
    const char *options[] = {"listening_ports", "8081", NULL}; 

    // registers file 
    path = argv[1]; 
    ctx = mg_start(&callback, NULL, options); 
    printf("%s", path); 
    getchar(); // Wait until user hits "enter" 
    mg_stop(ctx); 

    return 0; 
} 

我使用Visual Studio 2010來構建項目

有沒有人有什麼可能導致此錯誤的任何想法?

+1

順便說一句,你是否將該程序編譯爲C或C++程序?請保留其中一個語言標籤並刪除其他語言標籤。 –

+0

@Rob抱歉,這只是一個簡單的C程序,在Visual Studio 2010下編譯,我刪除了錯誤的標籤 – Ben

回答

1

您爲您的回調函數分配沒有返回值,回調函數根據定義是未定義的行爲。請檢查回撥的正確退貨結果要求,因爲void *void不同義。我很確定返回值是回調事件相關的,但不要在此引用我。 (該死......太晚了)。

從貓鼬頭(至少我的版本訪問)描述提供給mg_start()回調函數的目的和責任措施:

// Prototype for the user-defined function. Mongoose calls this function 
// on every event mentioned above. 
// 
// Parameters: 
// event: which event has been triggered. 
// conn: opaque connection handler. Could be used to read, write data to the 
//   client, etc. See functions below that accept "mg_connection *". 
// request_info: Information about HTTP request. 
// 
// Return: 
// If handler returns non-NULL, that means that handler has processed the 
// request by sending appropriate HTTP reply to the client. Mongoose treats 
// the request as served. 
// If callback returns NULL, that means that callback has not processed 
// the request. Handler must not send any data to the client in this case. 
// Mongoose proceeds with request handling as if nothing happened. 

typedef void * (*mg_callback_t)(enum mg_event event, 
        struct mg_connection *conn, 
        const struct mg_request_info *request_info); 
+0

我試過返回「」或者甚至是NULL,但是錯誤依然存在。我基於我的代碼在這個例子:https://github.com/valenok/mongoose/blob/master/examples/hello.c,我看不出太大的區別 – Ben

+0

@BenoitHnte你可能想更新你的問題的來源這反映了你的變化,如果是這樣的話,它肯定會引發更多類似的反應。此外,還提供操作系統和編譯器工具鏈信息。通過標題中的規範,這個或那個函數*必須*提供一個有效的返回值。我會繼續挖掘,看看是否有我失蹤的東西。 – WhozCraig

0

確保「路徑」是不爲NULL。指定一個默認值。