c++ - Adobe String Memory Leak - 在哪里调用外部库入口点来释放内存?

标签 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++ 文档下载文件。 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;  


这是 Adob​​e 示例用来保存将返回给 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/

相关文章:

c++ - 为分配的 char 数组赋值失败

javascript - React 同级组件之间的状态泄漏

iphone - iPhone中使用NSData的内存泄漏问题

c++ - 在 QApplication 中使用可视化检漏仪

c++ - 错误 C2679 : binary '+' : no operator found which takes a right-hand operand of type

c++ - C 编程中 fscanf() 的问题

c++ - 在 OSX 上使用 CMake 将 Adob​​e 的 XMP 工具包构建为共享库的最简单方法是什么?

javascript - 插画脚本: How to print layers name?

coldfusion - 从 ColdFusion MX(我认为是 6)迁移到 ColdFusion 7

C++ 仿函数状态和首选项