Quantcast
Channel: Adobe Community : Discussion List - After Effects Scripting
Viewing all articles
Browse latest Browse all 2143

External Library Entry Point to Free Memory

$
0
0

I've created an After Effects script that extracts data from JSON files downloaded from an HTTPS URL. Even though I'm new to C++ I've managed to create a DLL that accomplishes this based on the examples provided with the After Effects installation (samplelib and basicexternalobject). 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 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.

 

extern "C" TvgAfx_Com_API long DownloadJson(TaggedData* argv, long argc, TaggedData * result)
{     //... first I check the arguments passed

// The returned value type
result->type = kTypeString;


//Converts from string into LPCWSTR ---------------------------------------------------
std::wstring stemp = s2ws(argv[0].data.string);
LPCWSTR jsonLink = stemp.c_str();


std::wstring stemp02 = s2ws(argv[1].data.string);
LPCWSTR jsonHeader = stemp02.c_str();
//--------------------------------------------------------------------------------------


//Class that does the HTTP request
WinHttpClient client(jsonLink, jsonHeader);


//Synchronous request 
if (client.SendHttpsRequest())
{
     string httpResponse = client.GetHttpResponse();     if (httpResponse.length() > 0)     {          //Sends response string back to javascript          result->data.string = getNewBuffer(httpResponse);     }     else     {           //Sends FALSE back to javascript           result->type = kTypeBool;           result->data.intval = 0;     }
}
else
{     //Sends FALSE back to javascript     result->type = kTypeBool;     result->data.intval = 0;
}


return kESErrOK;
}


 

 

The class WinHttpClienttthat does the actual request 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;


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.


Viewing all articles
Browse latest Browse all 2143

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>