Adobe字符串内存泄漏-在哪里调用外部库入口指向可用内存?

时间:2019-04-08 10:54:37

标签: c++ memory-leaks adobe after-effects

我创建了一个After Effects脚本,该脚本从从HTTPS URL下载的JSON文件中提取数据。问题出在我编码用来下载C ++ DLL并将其传递回脚本的地方。即使它工作正常,也有一个内存泄漏的实例-After Effects发布了一个弹出窗口,说“ STRING MEMORY LEAK”。

我是C ++的新手,但我设法编写了一个DLL,它根据After Effects安装随附的示例(samplelib和basicexternalobject)以及Microsoft的C ++文档下载了一个文件。 《 Adob​​e 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字符串关联的内存。但是请求类不是已经做到了吗?我只是不知道在哪里调用该方法以及将其传递给什么方法。我将不胜感激任何帮助。非常感谢!

1 个答案:

答案 0 :(得分:0)

当为After Effects创建DLL ExternalObject时,它实际上只是通过某些ExtendScript脚本调用的接口。您必须将外部对象加载到AE脚本中,一旦加载,就可以从脚本中调用在C ++类中创建的方法/函数。

您必须了解how to load the DLL in an ExtendScript script file。然后,您可以调用DLL的方法。