View Full Version : exception occurred when in the program ends
jxtp
8th April 2015, 18:49
hi, I have written a program,However,exception occurred when in the program ends, tips"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Exceptions appear in avisynth.h ~AVSValue (AVS_BakedCode) (AVS_LinkCall) (AVSValue_DESTRUCTOR) ()
This is my leaner code
#define AVS_LINKAGE_DLLIMPORT
#include "avisynth.h"
#pragma comment(lib, "avisynth.lib")
int main(int argc, const char* argv [])
{
const char* infile = "in.avs";
IScriptEnvironment* env = CreateScriptEnvironment(AVISYNTH_INTERFACE_VERSION);
AVSValue res = env->Invoke("Import", infile);
env->DeleteScriptEnvironment();
return 0;
}
In my opinion, this code is normal, I do not understand this exception.
How to solve the problem?
PS:My English is poor,If you don't understand my question.you can compile the code.
StainlessS
8th April 2015, 19:40
I've never compiled a program using Avisynth externally (only plugins), but I think this might work.
#define AVS_LINKAGE_DLLIMPORT
#include "avisynth.h"
#pragma comment(lib, "avisynth.lib")
int main(int argc, const char* argv [])
{
const char* infile = "in.avs";
IScriptEnvironment* env = CreateScriptEnvironment(AVISYNTH_INTERFACE_VERSION);
AVSValue InvokeArgs[1] = {infile};
AVSValue res = env->Invoke("Import", AVSValue(InvokeArgs,1) ); // 1 = number of elements in InvokeArgs
env->DeleteScriptEnvironment();
return 0;
}
EDIT: I think it may have been interpreting infile as a pointer to an AVSValue (which it aint, and problems when trying to destruct).
wonkey_monkey
8th April 2015, 19:41
I've just quickly compared this against some basic code of mine that works, and the differences are:
AVSValue res = env->Invoke("Import", infile);
<->
AVSValue res = env->Invoke("Import", AVSValue(infile));
and
env->DeleteScriptEnvironment();
<->
delete env;
No idea if either of those will make any difference to your code, but it's all I can suggest.
Groucho2004
8th April 2015, 21:42
hi, I have written a program,However,exception occurred when in the program ends, tips"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Exceptions appear in avisynth.h ~AVSValue (AVS_BakedCode) (AVS_LinkCall) (AVSValue_DESTRUCTOR) ()
This is my leaner code
#define AVS_LINKAGE_DLLIMPORT
#include "avisynth.h"
#pragma comment(lib, "avisynth.lib")
int main(int argc, const char* argv [])
{
const char* infile = "in.avs";
IScriptEnvironment* env = CreateScriptEnvironment(AVISYNTH_INTERFACE_VERSION);
AVSValue res = env->Invoke("Import", infile);
env->DeleteScriptEnvironment();
return 0;
}
In my opinion, this code is normal, I do not understand this exception.
How to solve the problem?
PS:My English is poor,If you don't understand my question.you can compile the code.
A few pointers (no pun intended :D):
- Wrap your code in try - catch statements
- Don't assume that "DeleteScriptEnvironment()" is available
- See here (http://forum.doom9.org/showthread.php?p=1631151&highlight=delete+env#post1631151) for proper release of IScriptEnvironment()
- Link avisynth.dll dynamically.
Have a look at the code of AVSMeter in my signature, it works pretty well.
jxtp
9th April 2015, 04:37
I've just quickly compared this against some basic code of mine that works, and the differences are:
AVSValue res = env->Invoke("Import", infile);
<->
AVSValue res = env->Invoke("Import", AVSValue(infile));
and
env->DeleteScriptEnvironment();
<->
delete env;
No idea if either of those will make any difference to your code, but it's all I can suggest.
Your suggestion is not workable.
jxtp
9th April 2015, 05:04
A few pointers (no pun intended :D):
- Wrap your code in try - catch statements
- Don't assume that "DeleteScriptEnvironment()" is available
- See here (http://forum.doom9.org/showthread.php?p=1631151&highlight=delete+env#post1631151) for proper release of IScriptEnvironment()
- Link avisynth.dll dynamically.
Have a look at the code of AVSMeter in my signature, it works pretty well.
try - catch Unable to catch the exception.
Link avisynth.dll dynamically is feasible,I just started is dynamically linked,But I write not CLI programs,"const AVS_Linkage* AVS_linkage" Cannot be defined in the class,I may need to use more than one instance of this class,Repeatedly changing the value of AVS_linkage, Then there will be an exception.
I hope you can compile my code
jxtp
9th April 2015, 05:12
I hope that you can compile and run my code,Reply me again:)
Groucho2004
9th April 2015, 07:39
But I write not CLI programs
Yet your example code contains "int main...".
"const AVS_Linkage* AVS_linkage" Cannot be defined in the class,I may need to use more than one instance of this class,Repeatedly changing the value of AVS_linkage, Then there will be an exception.
You define this variable globally:
const AVS_Linkage *AVS_linkage = 0;
After you initialized your IScriptEnvironment, you use this:
AVS_linkage = env->GetAVSLinkage();
and release it with this:
AVS_linkage = 0;
Be aware that this only works with a avisynth.dll >= 2.6 Alpha4.
Groucho2004
9th April 2015, 07:59
Here's a working example:
BOOL foo(std::string &s_error, std::string s_avsfile)
{
BOOL bRet = TRUE;
HINSTANCE hDLL = ::LoadLibrary("avisynth");
if (!hDLL)
{
s_error = "Failed to load avisynth.dll";
return FALSE;
}
try
{
IScriptEnvironment *(__stdcall *CreateEnvironment)(int) = (IScriptEnvironment *(__stdcall *)(int))::GetProcAddress(hDLL, "CreateScriptEnvironment");
if (!CreateEnvironment)
{
s_error = "Failed to load CreateScriptEnvironment()";
::FreeLibrary(hDLL);
return FALSE;
}
IScriptEnvironment *AVS_env = CreateEnvironment(AVISYNTH_INTERFACE_VERSION);
if (!AVS_env)
{
s_error = "Could not create IScriptenvironment";
::FreeLibrary(hDLL);
return FALSE;
}
AVS_linkage = AVS_env->GetAVSLinkage();
AVSValue AVS_main;
AVS_main = AVS_env->Invoke("Import", s_avsfile.c_str());
if (!AVS_main.IsClip()) //not a clip
AVS_env->ThrowError("Script did not return a video clip:\n(%s)", s_avsfile.c_str());
PClip AVS_clip;
VideoInfo AVS_vidinfo;
AVS_clip = AVS_main.AsClip();
AVS_vidinfo = AVS_clip->GetVideoInfo();
unsigned int uiTotalFrames = (unsigned int)AVS_vidinfo.num_frames;
for (unsigned int uiFrame = 0; uiFrame < uiTotalFrames; uiFrame++)
{
//Start reading frames
PVideoFrame src_frame = AVS_clip->GetFrame(uiFrame, AVS_env);
...
...
}
AVS_clip = 0;
AVS_main = 0;
AVS_env->DeleteScriptEnvironment();
}
catch (AvisynthError err)
{
s_error = err.msg;
bRet = FALSE;
}
catch (...)
{
bRet = FALSE;
s_error = OSErrorMessage();
if (s_error == "")
s_error = "Unknown exception";
}
AVS_linkage = 0;
::FreeLibrary(hDLL);
return bRet;
}
std::string OSErrorMessage()
{
LPVOID lpMsg;
DWORD dwLastError = GetLastError();
if (dwLastError == ERROR_SUCCESS)
return "";
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwLastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), (LPTSTR)&lpMsg, 0, NULL);
return (LPTSTR)lpMsg;
}
Note that this only works with Avisynth 2.6 Alpha4 and above.
jxtp
9th April 2015, 10:39
Yet your example code contains "int main...".
You define this variable globally:
const AVS_Linkage *AVS_linkage = 0;
After you initialized your IScriptEnvironment, you use this:
AVS_linkage = env->GetAVSLinkage();
and release it with this:
AVS_linkage = 0;
Be aware that this only works with a avisynth.dll >= 2.6 Alpha4.
example code just is example code,In fact, I've written is a DLL.
Although that's not what I'm looking for an answer,But, I from your reply content found inspiration
(http://forum.doom9.org/showthread.php?p=1631151&highlight=delete+env#post1631151).
I have solved.
And thank you!
jxtp
9th April 2015, 10:55
I have solved.
Solution is to manually perform destructor,
Inspired by "http://forum.doom9.org/showthread.php?p=1631151&highlight=delete+env#post1631151"
I can't explain this,But, is solved.
This is a revised code
#define AVS_LINKAGE_DLLIMPORT
#include "avisynth.h"
#pragma comment(lib, "avisynth.lib")
int main(int argc, const char* argv [])
{
const char* infile = "in.avs";
IScriptEnvironment* env = CreateScriptEnvironment(AVISYNTH_INTERFACE_VERSION);
AVSValue res = env->Invoke("Import", infile);
PClip clip = res.AsClip();
clip.~PClip();//key
res.~AVSValue();//key
env->DeleteScriptEnvironment();
return 0;
}
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.