小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

c

 quasiceo 2014-01-12

I'm working on wraping libcurl in luajit ffi. My finalizer isn't getting called.

local ffi = require("ffi")

ffi.cdef [[
  typedef struct{} CURL;
  CURL * curl_easy_init();
  void curl_easy_cleanup(CURL *);
]]

local CURL_lib = ffi.load("../lib/libcurl.so")
local CURL_CTX

local CURL_CTX_mt = {
  __gc = function()  print "finalizing"; CURL_lib.curl_easy_cleanup(CURL_CTX); end
}

ffi.metatype("CURL", CURL_CTX_mt)

CURL_CTX = ffi.new("CURL[1]")
CURL_CTX = CURL_lib.curl_easy_init();
print "done"

What am I missing here ? :D

BTW CURL is defined as typedef void CURL; I'm sure the way I am trying to do it isn't clean enough. Any advice ?

eureka ! : self answer -- still interested in comments if there any glaring issues.

asked Feb 22 '12 at 13:42
Hassan Syed
10.2k2583


 
if you discover an answer yourself, you should post it as an actual answer and accept it. –  Necrolis Feb 22 '12 at 14:11

 
aah I thought one had to wait for a timeout before a self-answer can be done. Thanks. Aah you cannot accept your own answer for 2 days. –  Hassan Syed Feb 22 '12 at 14:13
add comment

1 Answer

Compile time types in luajit must be structs (or unions I think) if you initialize your context type as a pointer, it is no longer a struct. So, there is a concept mismatch going on here. So, to fix things, add a void * to your struct, hang the metatype on the struct, and use the void * for the library context.

local ffi = require("ffi")

ffi.cdef [[
  typedef struct { void * ctx; } curl;
  curl * curl_easy_init();
  void curl_easy_cleanup(curl *);
]]

local curl_lib = ffi.load("../lib/libcurl.so")
local curl

local curl_mt = {
  __gc = function()  curl_lib.curl_easy_cleanup(curl.ctx); end
}

local curl_proto = ffi.metatype("curl", curl_mt)

curl = curl_proto(nil)
curl.ctx = curl_lib.curl_easy_init();

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多