C++ Adobe字符串内存泄漏-在何处调用外部库入口点以释放内存?
我创建了一个After-Effects脚本,它从从HTTPS URL下载的JSON文件中提取数据。问题是,我已经编码了C++ DLL下载它并把它传给脚本。尽管它一直工作正常,但还是有一个内存泄漏的实例——After Effects发布了一个弹出窗口,上面写着“字符串内存泄漏” <>我是C++的新手,但我已经成功地编写了一个DLL,它是基于后效安装(SAMPLILB和BASICEXALL对象)提供的例子下载的文件,以及微软的C++文档。《Adobe JavaScript工具指南》指出,必须“调用”方法“ESFreeMem()”以释放为传递给库函数或从库函数传递的以null结尾的字符串分配的内存”。问题是我不知道如何或在哪里使用它。我正在Windows7上使用After Effects CC 15.0.0(build 180)C++ Adobe字符串内存泄漏-在何处调用外部库入口点以释放内存?,c++,memory-leaks,adobe,after-effects,C++,Memory Leaks,Adobe,After Effects,我创建了一个After-Effects脚本,它从从HTTPS URL下载的JSON文件中提取数据。问题是,我已经编码了C++ DLL下载它并把它传给脚本。尽管它一直工作正常,但还是有一个内存泄漏的实例——After Effects发布了一个弹出窗口,上面写着“字符串内存泄漏” 我是C++的新手,但我已经成功地编写了一个DLL,它是基于后效安装(SAMPLILB和BASICEXALL对象)提供的例子下载的文件,以及微软的C++文档。《Adobe JavaScript工具指南》指出,必须“调用”方法
这是从JavaScript调用方获取一些参数并返回带有JSON内容的字符串的C++函数。如果失败,它将返回一个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字符串关联的内存。但是请求类不是已经做了吗?我只是不知道在哪里调用这个方法,以及传递什么给它。我将感谢任何帮助。非常感谢 为后效创建DLL ExternalObject时,它实际上只是一个通过一些ExtendScript脚本调用的接口。您必须加载AE脚本中的外部对象,一旦加载,C++中创建的方法/函数可以从脚本调用。 你必须知道。然后,您可以调用DLL的方法