【发布时间】:2019-08-29 12:10:24
【问题描述】:
我创建了一个 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 内容的字符串。如果失败,它会返回一个布尔值 (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字符串相关的内存必须被释放。但是请求类不是已经做到了吗?我只是不知道在哪里调用这个方法以及传递给它什么。我将不胜感激任何帮助。非常感谢!
【问题讨论】:
标签: c++ memory-leaks adobe after-effects