Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Development

Reply
 
Thread Tools Search this Thread Display Modes
Old 8th April 2015, 18:49   #1  |  Link
jxtp
kamisama
 
Join Date: Jun 2013
Posts: 7
exception occurred when in the program ends

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
Code:
~AVSValue (AVS_BakedCode) (AVS_LinkCall) (AVSValue_DESTRUCTOR) ()

This is my leaner code
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.
jxtp is offline   Reply With Quote
Old 8th April 2015, 19:40   #2  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
I've never compiled a program using Avisynth externally (only plugins), but I think this might work.

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 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).
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 8th April 2015 at 19:56.
StainlessS is offline   Reply With Quote
Old 8th April 2015, 19:41   #3  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 2,496
I've just quickly compared this against some basic code of mine that works, and the differences are:

Code:
AVSValue res = env->Invoke("Import", infile);
<->
AVSValue res = env->Invoke("Import", AVSValue(infile));
and

Code:
env->DeleteScriptEnvironment();
<->
delete env;
No idea if either of those will make any difference to your code, but it's all I can suggest.
__________________
My AviSynth filters / I'm the Doctor
wonkey_monkey is offline   Reply With Quote
Old 8th April 2015, 21:42   #4  |  Link
Groucho2004
 
Join Date: Mar 2006
Location: Barcelona
Posts: 5,034
Quote:
Originally Posted by jxtp View Post
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
Code:
~AVSValue (AVS_BakedCode) (AVS_LinkCall) (AVSValue_DESTRUCTOR) ()

This is my leaner code
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 ):
- Wrap your code in try - catch statements
- Don't assume that "DeleteScriptEnvironment()" is available
- See here for proper release of IScriptEnvironment()
- Link avisynth.dll dynamically.

Have a look at the code of AVSMeter in my signature, it works pretty well.
__________________
Groucho's Avisynth Stuff

Last edited by Groucho2004; 8th April 2015 at 22:06. Reason: Quoted wrong post
Groucho2004 is offline   Reply With Quote
Old 9th April 2015, 04:37   #5  |  Link
jxtp
kamisama
 
Join Date: Jun 2013
Posts: 7
Quote:
Originally Posted by davidhorman View Post
I've just quickly compared this against some basic code of mine that works, and the differences are:

Code:
AVSValue res = env->Invoke("Import", infile);
<->
AVSValue res = env->Invoke("Import", AVSValue(infile));
and

Code:
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 is offline   Reply With Quote
Old 9th April 2015, 05:04   #6  |  Link
jxtp
kamisama
 
Join Date: Jun 2013
Posts: 7
Quote:
Originally Posted by Groucho2004 View Post
A few pointers (no pun intended ):
- Wrap your code in try - catch statements
- Don't assume that "DeleteScriptEnvironment()" is available
- See here 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 is offline   Reply With Quote
Old 9th April 2015, 05:12   #7  |  Link
jxtp
kamisama
 
Join Date: Jun 2013
Posts: 7
I hope that you can compile and run my code,Reply me again
jxtp is offline   Reply With Quote
Old 9th April 2015, 07:39   #8  |  Link
Groucho2004
 
Join Date: Mar 2006
Location: Barcelona
Posts: 5,034
Quote:
Originally Posted by jxtp View Post
But I write not CLI programs
Yet your example code contains "int main...".

Quote:
Originally Posted by jxtp View Post
"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:
Code:
const AVS_Linkage *AVS_linkage = 0;
After you initialized your IScriptEnvironment, you use this:
Code:
AVS_linkage = env->GetAVSLinkage();
and release it with this:
Code:
AVS_linkage = 0;
Be aware that this only works with a avisynth.dll >= 2.6 Alpha4.
__________________
Groucho's Avisynth Stuff
Groucho2004 is offline   Reply With Quote
Old 9th April 2015, 07:59   #9  |  Link
Groucho2004
 
Join Date: Mar 2006
Location: Barcelona
Posts: 5,034
Here's a working example:
Code:
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.
__________________
Groucho's Avisynth Stuff

Last edited by Groucho2004; 13th April 2015 at 21:42.
Groucho2004 is offline   Reply With Quote
Old 9th April 2015, 10:39   #10  |  Link
jxtp
kamisama
 
Join Date: Jun 2013
Posts: 7
Quote:
Originally Posted by Groucho2004 View Post
Yet your example code contains "int main...".


You define this variable globally:
Code:
const AVS_Linkage *AVS_linkage = 0;
After you initialized your IScriptEnvironment, you use this:
Code:
AVS_linkage = env->GetAVSLinkage();
and release it with this:
Code:
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.ph...nv#post1631151).
I have solved.
And thank you!
jxtp is offline   Reply With Quote
Old 9th April 2015, 10:55   #11  |  Link
jxtp
kamisama
 
Join Date: Jun 2013
Posts: 7
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
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;
}
jxtp is offline   Reply With Quote
Reply

Tags
avisynth

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:46.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.