Difference between revisions of "LibVLC Memory Management"

From VideoLAN Wiki
Jump to navigation Jump to search
(Initial Creation.)
 
m (+{{Lowercase}}, syntaxhighlight)
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
Todo: Improve readability.
+
{{Lowercase}}
 
 
 
== Reference Counting ==
 
== Reference Counting ==
LibVLC uses a certain number of objects. Each one maintains a references count, which is used to know when the object must be released. The three related functions are:
 
  
* libvlc_object_name_'''new'''() ''Create an object with a refcount equals to 1''
+
[[libVLC]] uses a certain number of objects. Each one maintains a references count, which is used to know when the object must be released.  The three related functions are:
  
 +
* libvlc_object_name_'''new'''()  ''Create an object with a refcount equal to 1.''
 
* libvlc_object_name_'''retain'''()  ''Increment the object refcount by 1.''
 
* libvlc_object_name_'''retain'''()  ''Increment the object refcount by 1.''
 
 
* libvlc_object_name_'''release'''()  ''Decrement the refcount by 1, and release the object if the new refcount is less than zero.''
 
* libvlc_object_name_'''release'''()  ''Decrement the refcount by 1, and release the object if the new refcount is less than zero.''
  
Object that don't follow this rule:
+
=== Example ===
 
+
<syntaxhighlight lang="c">
=== Examples ===
+
  libvlc_media_list_t *p_ml = libvlc_media_list_new(libvlc_inst)
libvlc_exception_t e;
 
libvlc_exception_init( &e );
 
 
  libvlc_media_list_t * p_ml = libvlc_media_list_new( libvlc_inst, &e)
 
 
   
 
   
 
  /* Free the memory used by media_list release */
 
  /* Free the memory used by media_list release */
  libvlc_media_list_release( p_ml );
+
  libvlc_media_list_release(p_ml);
+
</syntaxhighlight>
 +
 
 +
=== Objects that don't follow this rule ===
 +
 
 +
* libvlc_event_manager_t: Those are supposed to be accessed only during the life of their parent libvlc_* object.
  
 
== Getters and Setters ==
 
== Getters and Setters ==
Usually object Property are set through getters and setters.
+
 
 +
Usually object properties are set through getters and setters.
  
 
* Usually getters are in the form:
 
* Usually getters are in the form:
Line 32: Line 31:
  
 
* Variations:
 
* Variations:
 
 
** Setter: libvlc_media_list_add_media_descriptor
 
** Setter: libvlc_media_list_add_media_descriptor
 
 
** Getter: libvlc_media_list_item_at_index
 
** Getter: libvlc_media_list_item_at_index
  
 
For setters and getters the memory management rules are:
 
For setters and getters the memory management rules are:
* Getters internally '''retain''' the object before returning it, so is means you must '''release''' the object after uses.
+
* Getters internally '''retain''' the object before returning it, so is means you must '''release''' the object after use.
 
* Setters internally '''retain''' the object, so basically you never need to keep the object around.
 
* Setters internally '''retain''' the object, so basically you never need to keep the object around.
(This also applies to strings where retain is strdup and release is free)
+
(This also applies to strings where retain is '''strdup''' and release is '''free''')
  
 
Notable exceptions to those rules are:
 
Notable exceptions to those rules are:
* libvlc_''object_name''_'''event_manager''': This getter never retain the object prior returning it. That means you '''mustn't release''' it.
+
* libvlc_''object_name''_'''event_manager''': This getter never retains the object prior returning it. That means you '''mustn't release''' it.
 +
 
 +
[[Category:libVLC]]

Latest revision as of 08:15, 4 March 2019

Reference Counting

libVLC uses a certain number of objects. Each one maintains a references count, which is used to know when the object must be released. The three related functions are:

  • libvlc_object_name_new() Create an object with a refcount equal to 1.
  • libvlc_object_name_retain() Increment the object refcount by 1.
  • libvlc_object_name_release() Decrement the refcount by 1, and release the object if the new refcount is less than zero.

Example

 libvlc_media_list_t *p_ml = libvlc_media_list_new(libvlc_inst)
 
 /* Free the memory used by media_list release */
 libvlc_media_list_release(p_ml);

Objects that don't follow this rule

  • libvlc_event_manager_t: Those are supposed to be accessed only during the life of their parent libvlc_* object.

Getters and Setters

Usually object properties are set through getters and setters.

  • Usually getters are in the form:

libvlc_object_name_object

  • Setters are:

libvlc_object_name_set_object

  • Variations:
    • Setter: libvlc_media_list_add_media_descriptor
    • Getter: libvlc_media_list_item_at_index

For setters and getters the memory management rules are:

  • Getters internally retain the object before returning it, so is means you must release the object after use.
  • Setters internally retain the object, so basically you never need to keep the object around.

(This also applies to strings where retain is strdup and release is free)

Notable exceptions to those rules are:

  • libvlc_object_name_event_manager: This getter never retains the object prior returning it. That means you mustn't release it.