External Library Entry Point to Free Memory
I've created an After Effects script that extracts data from JSON files downloaded from an HTTPS URL. I'm new to C++ and I've had a hard time putting together a DLL that does the download, based on the examples provided with the After Effects installation (samplelib and basicexternalobject) and Microsoft's documentation. The problem now is that I don't know how to use the entry point "ESFreeMem()". The JavaScript Tools Guide says that it is "called to free memory allocated for a null-terminated string passed to or from library functions".
I'm using After Effects CC 15.0.0 (build 180) on Windows 7.
This is the C++ function that gets some parameters from the javascript caller and returns a string with the JSON contents. If it fails it returns a bool (FALSE) so that the script can do what is necessary in this case.
The C++ class WinHttpClienttthat does the actual request and frees the memory allocated to the buffer that holds the response. Here's a piece of code:
- // Read the data.
- ZeroMemory(pszOutBuffer, dwSize + 1);
- if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
- {
- //Log error
- }
- else
- {
- resource.append(pszOutBuffer).c_str();
- }
- // Free the memory allocated to the buffer.
- delete[] pszOutBuffer;
-
This is the function that the Adobe example uses to hold the string that will be returned to javascript:
- //brief Utility function to handle strings and memory clean up
- static char* getNewBuffer(string& s)
- {
- // Dynamically allocate memory buffer to hold the string
- // to pass back to JavaScript
- char* buff = new char[1 + s.length()];
- memset(buff, 0, s.length() + 1);
- strcpy(buff, s.c_str());
- return buff;
- }
Now, the manual says this method must be implemented:
- /**
- * \brief Free any string memory which has been returned as function result.
- * JavaScipt calls this function to release the memory associated with the string.
- * Used for the direct interface.
- *
- * \param *p Pointer to the string
- */
- extern "C" SAMPLIB void ESFreeMem (void* p)
- {
- if (p)
- free (p);
- }
What I understand from this is that the memory associated with the json string returned must be released. But didn't the request class already do it? I just don't know where to call this method and what to pass on to it. Can anybody provide me with an example, please? Thanks a lot!
PS: You will probably find flaws in my C++ code and I would appreciate if you could show me the dangerous ones (at least), if any.
