我创建了一个 After Effects 脚本,它从从 HTTPS URL 下载的 JSON 文件中提取数据。问题在于我编写的 C++ DLL 下载它并将其传递回脚本。尽管它一直运行良好,但还是出现了一个内存泄漏实例 - After Effects 发出了一个弹出窗口,上面写着“STRING MEMORY LEAK”。
我是 C++ 新手,但我设法编写了一个 DLL,它根据 After Effects 安装(samplelib 和 basicexternalobject)提供的示例以及 Microsoft 的 C++ 文档下载文件。 Adobe JavaScript 工具指南说方法“ESFreeMem()”必须被“调用以释放分配给传递到库函数或从库函数传递的空终止字符串的内存”。问题是我不知道如何或在哪里使用它。我在 Windows 7 上使用 After Effects CC 15.0.0(内部版本 180)。
这是一个 C++ 函数,它从 javascript 调用者获取一些参数并返回一个带有 JSON 内容的字符串。如果失败,它会返回一个 bool 值 (FALSE),以便脚本可以在这种情况下执行必要的操作。
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;
}
执行实际请求的类 WinHttpClient 释放分配给保存响应的缓冲区的内存。这是一段代码:
// 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;
这是 Adobe 示例用来保存将返回给 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;
}
现在,手册说必须实现此方法:
/**
* \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);
}
我从中了解到的是,必须释放与返回的json字符串关联的内存。但是请求类不是已经做到了吗?我只是不知道在哪里调用这个方法以及传递给它什么。我将不胜感激任何帮助。非常感谢!
最佳答案
当您为 After Effects 创建 DLL ExternalObject 时,它实际上只是您通过一些 ExtendScript 脚本调用的接口(interface)。您必须在 AE 脚本中加载 ExternalObject,一旦加载,您在 C++ 类中创建的方法/函数就可以从脚本中调用。
你要知道how to load the DLL in an ExtendScript script file .然后,您可以调用 DLL 的方法。
关于c++ - Adobe String Memory Leak - 在哪里调用外部库入口点来释放内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55571807/