Search code examples
pythoncpython-c-apicpythonpython-embedding

Shall strings pointed by paramaters to PySys_SetArgvEx() be kept in memory until Py_Finalize()?


i have the following piece of code

int nArgs;
if (LPWSTR * const szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs))
{
  PySys_SetArgvEx(nArgs, szArglist, false);
  LocalFree(szArglist);
}

I cannot find in Python documentation if memory pointed by szArglist shall be preserved until Python is shutdown or i can free it immediately.

Can anybody put some light on this, please?

Thank you! Vladimir


Solution

  • The Python C API looks like it is using a new PyList object to fill out the args, and is allocating its own memory for the char* arguments.

    On strings longer than 1 characters, PySys_SetArgvEx will malloc its own memory for the string.

    So it's safe to delete any memory allocated that you passed to PySys_SetArgvEx.