当前位置:网站首页>VS2005 project call free() compiled with static libcurl library reported heap error

VS2005 project call free() compiled with static libcurl library reported heap error

2022-06-26 08:28:00 Tonyfield

curl There are many places to use strdup, but windows The next has been abandoned “strdup”, And ask for “_strdup” replace (

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strdup-wcsdup?view=vs-2019).

That's what makes libcurl Static library call free() The cause of the abnormality in the process .

    Actually libcurl The differences of memory allocation and release functions under different platforms have been considered , Library implementation malloc,calloc,strdup,free Such as function , What's actually called is easy.c As defined in Curl_cmalloc,Curl_ccalloc,Curl_cstrdup,Curl_cfree A function pointer , Initialization function global_init() Will define these function pointers .


/**
 * curl_global_init() globally initializes curl given a bitwise set of the
 * different features of what to initialize.
 */
static CURLcode global_init(long flags, bool memoryfuncs)
{
  if(initialized++)
    return CURLE_OK;

  if(memoryfuncs) {
    /* Setup the default memory functions here (again) */
    Curl_cmalloc = (curl_malloc_callback)malloc;
    Curl_cfree = (curl_free_callback)free;
    Curl_crealloc = (curl_realloc_callback)realloc;
    Curl_cstrdup = (curl_strdup_callback)system_strdup;
    Curl_ccalloc = (curl_calloc_callback)calloc;
#if defined(WIN32) && defined(UNICODE)
    Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
#endif
  }

  if(!Curl_ssl_init()) {
    DEBUGF(fprintf(stderr, "Error: Curl_ssl_init failed\n"));
    goto fail;
  }

#ifdef WIN32
  if(Curl_win32_init(flags)) {
    DEBUGF(fprintf(stderr, "Error: win32_init failed\n"));
    goto fail;
  }
#endif

    among , Only Curl_cstrdup The default value of is special , It points to system_strdup . It can be seen that , Only in _WIN32_WCE Under defined conditions strdup Point to _strdup, Otherwise, call curlx_strdup or strdup , And because the config-win32.h In the definition of #define HAVE_STRDUP 1, So use VS Compilation will not use curlx_strdup, That's all strdup 了 .

  #if defined(_WIN32_WCE)
  #define system_strdup _strdup
  #elif !defined(HAVE_STRDUP)
  #define system_strdup curlx_strdup
  #else
  #define system_strdup strdup
  #endif

     We add here WIN32 Judge the condition , In this way visual studio Call down strdup Will point to the desired security function _strdup.

- #if defined(_WIN32_WCE)
+ #if defined(_WIN32_WCE) || defined(_WIN32) ||defined(WIN32)
  #define system_strdup _strdup
  #elif !defined(HAVE_STRDUP)
  #define system_strdup curlx_strdup
  #else
  #define system_strdup strdup
  #endif
原网站

版权声明
本文为[Tonyfield]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170557056368.html