Chromium Embedded Framework中文文檔 (使用C API)

來源:互聯網
上載者:User
簡介

CEF的C API是由libcef DLL暴露的基於C的介面,cef_capi.h 標頭檔中定義的介面是由CEF translator tool自動產生的C++ API鏡像。

引用計數

理解引用計數可能是使用CEF C API最困難的部分了,CEF使用引用計數概念類似於COM的概念,這裡有一些基本的規則可以協助你減少使用引用計數時的困難。

1. 當將一個結構傳給它自己的成員函數時,不要進行引用計數的加、減操作:

struct->call_func(struct,...); // no reference counting change on 'struct'

2. 在將結構作為參數傳給其它結構前,增加引用計數:

// Should have already added a reference to 'some_other_struct' struct->call_func(...,some_other_struct,...);

3. 在你使用完從別處以參數傳過來的結構後,減少它的引用計數:

void my_func(...,some_other_struct,...) { // remove a reference from 'some_other_struct' after you're done using it }

4. 在傳入一個處理常式前,增加它的引用,比如,cef_create_browser(),當API不再需要使用某一處理常式時,會移除它的引用。

5. 使用原子的引用計數實現,因為add_ref和release可能會跨線程調用,WinAPI InterlockedIncrement() 和 InterlockedDecrement() 函數可用於此目的。

6. 如果引用計算變成0,處理常式應該在已賦值給結構的releae()回呼函數中刪除自己:

 // custom data structure that extends cef_handler_t

typedef struct _my_handler_t {
  cef_handler_t handler; // cef_handler_t member comes first
  // custom members here, including the reference count variable
} my_handler_t;

// allocate the custom data structure (already initialized to zero)
my_handler_t* my_handler = (my_handler_t*)calloc(1, sizeof(my_handler_t));
// set the size member of cef_base_t appropriately
my_handler->handler.base.size = sizeof(cef_handler_t);
// assign the release callback function (and the other callback functions)
my_handler->handler.base = my_release;
...

// release callback function implementation for my_handler_t
int CEF_CALLBACK my_release(struct _cef_base_t* base) {
  // this cast works because cef_base_t is the first member of
  // cef_handler_t and cef_handler_t is the first member of my_handler_t
  my_handler_t* my_handler = (my_handler_t*)base;
 
  // decrement the reference count (stored as a custom member of my_handler_t)
  // free the my_handler_t structure when the reference count reaches zero
  if(reference_count_is_zero)
    free(my_handler);
}

7. 清除代碼添加給結構的所有附加的引用(比如,在處理常式實現中保持cef_broswer_t指標的引用),最終的釋放引用的機會是cef_handler_t::handle_before_window_closed()。


8. 如果沒有任何的crash產生,在shutdown時也會命中DebugObjCt斷言,那麼就說明你正確的處理了引用計數。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.