[vlc-devel] [vlc-commits] Remove libvlc_free

Steve Lhomme robux4 at ycbcr.xyz
Fri May 24 08:37:20 CEST 2019


On 2019-05-24 7:44, Steve Lhomme wrote:
> On 2019-05-24 7:25, Steve Lhomme wrote:
>> On 2019-05-23 17:11, Rémi Denis-Courmont wrote:
>>> Le torstaina 23. toukokuuta 2019, 13.57.32 EEST Steve Lhomme a écrit :
>>>> Here's another one: we could decide to rewrite libvlc in C++ and keep
>>>> the same ABI. Does that mean we would have to forced libvlc hosts to
>>>> call "delete psz-str" because we don't feel like keeping 
>>>> libvlc_free() ?
>>>
>>> You would have to keep using malloc() otherwise you would break the 
>>> existing
>>> ABI 3.0 or 4.0 anyway that says you can use free(). 
>>
>> That's indeed a problem. I think the free() option should not have 
>> been there in the first place. I would rather vote to remove this option.
>>
>> For example we could decide that for performance reasons we allocate 
>> using aligned_alloc(). On some system that requires an aligned_free(). 
>> We cannot do that if we allow free() of a particular runtime.
> 
> Another use case: building libvlc/vlc with allocation tooling, 
> redirecting malloc/free to debug leaks. Calling free() outside of libvlc 
> with memory allocated this will likely crash or report errors when a 
> legitimate free() is done. We should not tie our hands by allowing 
> free() at all.

Attached is a quick example of a library done incorrectly:

The EXE links with the *debug* DLL C runtime, as most people would use 
while debugging their code.

The DLL links with the *standard* DLL C runtime, as a distributed DLL 
would use.

Calling free() on the memory returned by the DLL crashes with a heap 
allocation error. That doesn't happen is the free() is done in the DLL.
-------------- next part --------------
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MD")
add_library(dll_crt SHARED dll.c)
-------------- next part --------------
#include <windows.h>
#include <stdio.h>

__declspec(dllexport) void * CallInDll(void)
{
    printf("the DLL call\n"); 
    return malloc(100);
}
-------------- next part --------------
#include <windows.h>
#include <stdio.h>

int main(void) {
    printf("the EXE call\n");
    HMODULE dll = LoadLibrary("dll_crt.dll");
    if (dll == 0) {
      printf("DLL not found\n");
      return 1;
    }
    void* (*pfCallInDll)() = GetProcAddress(dll, "CallInDll");
    if (pfCallInDll == NULL) {
      printf("CallInDll not found\n");
      FreeLibrary(dll);
      return 1;
    }
    void *allocated = pfCallInDll();
    printf("the DLL was called\n");
    free(allocated);
    FreeLibrary(dll);
    return 0;
}
-------------- next part --------------
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MDd")
add_executable(exe_crt exe.c)


More information about the vlc-devel mailing list