Author Topic: Reactor causes fatal error.  (Read 10610 times)

0 Members and 1 Guest are viewing this topic.

mr_nick

  • Guest
Reactor causes fatal error.
« on: June 15, 2012, 03:31:17 AM »
OK, I'm fairly new to the world of reactors and have run into a problem which I'm hoping somebody can help me with.

I have created a very basic reactor as a test. As you can see, it doesn't actually do anything other than set a variable so I know the error isn't caused by something I'm doing with the reactor.

Code: [Select]
(defun np:test (reactor callbackdata / )
  (setq abc 123)
)
(if (not np:reactor1)
  (setq np:reactor1
    (vlr-command-reactor nil '((:vlr-commandwillstart . np:test)))
  )
)

OK, so the problem I have found is that if I load this reactor into a drawing and then use the MVSETUP command but escape out without completing the command, AutoCAD throws an error and becomes unusable. When I then close AutoCAD down I am met with multiple unhandled exception errors before the app finally closes.

I then altered the reactor from a vlr-command-reactor to a vlr-editor-reactor and found that with this, I am no longer met with the same error while escaping out of MVSETUP. However, when I close CAD down I am eventually left with an 'AutoCAD Application has stopped working' error dialog on screen which reports that acad.exe as crashed.

I am working on AutoCAD Architecture 2012 (although I have tried it on 2013 to with the same results) on Windows 7 64bit. I don't currently have access to a 32 bit machine to test whether it is platform specific or not.

Is there anybody out there who can confirm whether they get the same errors as me or whether I am doing something fundamentally wrong in the way I am handling reactors?

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: Reactor causes fatal error.
« Reply #1 on: June 15, 2012, 11:06:41 AM »
You have to code for ANY event that can happen: CommandStart, CommandEnded, CommandCancelled.  Have a look at the following;
This series of Command Reactors automatically sets the ANNOBJ layerkey current when certain commands fire and resets the previously current layer on CommandEnded.

James Buzbee

Code: [Select]
;;; Command Will Start ;
(defun kb:CommandWillStart (calling-reactor commands / lname)
  (cond ((member (car commands)
                 (list "MTEXT" "-MTEXT" "DTEXT" "TEXT" "-TEXT" "LEADER" "QLEADER" "MLEADER"
                       )
                 )
         (setq lname (AECGENERATELAYERKEY "ANNOBJ"))
         )
        ((member (car commands)
                 (list "DIMLINEAR" "DIMALIGNED" "DIMBASELINE" "DIMCONTINUE" "DIMDIAMETER"
                       "DIMANGULAR" "DIMRADIUS" "DIMORDINATE" "QDIM"
                       )
                 )
         (setq lname (AECGENERATELAYERKEY "DIMLINE"))
         )
        ((member (car commands) (list "VPORTS" "-VPORTS"))
         (setq lname (AECGENERATELAYERKEY "ANNDTOBJ"))
         )
        ((member (car commands) (list "IMAGE" "-IMAGE")) ;"-XREF" "XREF" "XATTACH"
         (setq lname "XREF")
         )
        )
  (if lname
    (kb:setclayer lname T)
    )
  )

;;; Command Ended ;
(defun kb:CommandEnded (calling-reactor commands / dwgdimscale doc)
  (cond ((member (car commands)
                 (list "MTEXT" "-MTEXT" "DTEXT" "TEXT" "-TEXT"
                       "LEADER" "QLEADER" "MLEADER" "DIMLINEAR" "DIMALIGNED" "DIMBASELINE"
                       "DIMCONTINUE" "DIMDIAMETER" "DIMANGULAR" "DIMRADIUS" "DIMORDINATE"
                       "QDIM" "MVIEW" "VPORTS" "-VPORTS" "IMAGE" "-IMAGE"
                       )
                 )
         (kb:resetclayer)
         )
        ((member (car commands) (list "CLOSE" "QUIT"))
         (if (dcl_Form_IsActive ADT_ADTPalette)
           (progn (dcl_form_Close ADT_ADTPalette))
           )
         (if (dcl_Form_IsActive DetailManager_DMPalette)
           (progn (dcl_form_Close DetailManager_DMPalette))
           )
         )
        ((member (car commands) (list "PASTECLIP" "PURGE" "BLOCK" "INSERT" "-PURGE" "-BLOCK" "-INSERT"))
         (if (dcl_Form_IsActive Blocks_BlockManager)
           (c:Blocks_BlockManager_OnInitialize)
           )
         (if (dcl_Form_IsActive ADT_ADTPalette)
           (c:ADT_ADTPalette_OnInitialize)
           )
         )
        )
  )

;;; Command Cancelled ;
(defun kb:CommandCancelled (calling-reactor commands /)
  (cond ((member (car commands)
                 (list "MTEXT" "-MTEXT" "DTEXT" "TEXT" "-TEXT" "LEADER" "QLEADER" "MLEADER"
                       "DIMLINEAR" "DIMALIGNED" "DIMBASELINE" "DIMCONTINUE" "DIMDIAMETER"
                       "DIMANGULAR" "DIMRADIUS" "DIMORDINATE" "QDIM" "MVIEW" "VPORTS"
                       "-VPORTS" "IMAGE" "-IMAGE"
                       )
                 )
         (kb:resetclayer)
         )
        )
  )

;;; Construct the Command Reactor ;
(defun kb::ConstructCommandReactor (/)
  (if (= (type *kbCommandReactor*) 'VLR-Command-Reactor)
    (progn (vlr-remove *kbCommandReactor*) (setq *kbCommandReactor* nil))
    )
  (if (/= (type *kbCommandReactor*) 'VLR-Command-Reactor)
    (setq *kbCommandReactor*
           (VLR-Command-Reactor
             "kbTools Command Reactor" ; Data associated with the editor reactor
             ;; call backs
             '
              ((:VLR-commandWillStart . kb:CommandWillStart)
               (:VLR-commandEnded . kb:CommandEnded)
               (:VLR-commandCancelled . kb:CommandCancelled)
               )
             ) ;_ end of vlr-editor-reactor
          )
    )
  (if (not (vlr-added-p *kbCommandReactor*))
    (progn (vlr-add *kbCommandReactor*)
           (vlr-set-notification *kbCommandReactor* 'active-document-only)
           )
    )
      ;added 09.26.2003
  (princ)
  )

;;; load reactors ;

(if kb::ConstructCommandReactor
  (kb::ConstructCommandReactor)
  )
James Buzbee
Windows 8

BlackBox

  • King Gator
  • Posts: 3770
Re: Reactor causes fatal error.
« Reply #2 on: June 15, 2012, 01:00:35 PM »
OK, I'm fairly new to the world of reactors and have run into a problem which I'm hoping somebody can help me with.

<snip>

Is there anybody out there who can confirm whether they get the same errors as me or whether I am doing something fundamentally wrong in the way I am handling reactors?

Welcome to the world of Reactors!

I've tested the code you posted, and while I would not code it that way personally, the code tests fine using Civil 3D 2011:

Code: [Select]
Command: mvsetup

Initializing...
Enable paper space? [No/Yes] <Y>: *Cancel*

Command: !abc
123

As for the issue you are experiencing, I am unfamiliar with Architecture (being a Civil guy)... But I do use Reactors for several purposes in our setup. Is there something specific you'd like assistance with?

As the code James has posted illustrates, Reactors can be utilized to perform several different tasks, and in different circumstances; they're not all powerful, but they pack quite a punch.

You have to code for ANY event that can happen: CommandStart, CommandEnded, CommandCancelled. 

This statement is incorrect... One need only code for the Events which one intends to monitor, or react to, and _not_ "ANY event that can happen."

However, as a minimum standard (IMO)... A responsible developer will implement CommandCancelled, CommandEnded, and CommandFailed Event Callback(s) when modifying the end-user's environment (i.e., settings, sysvars, etc.) via the CommandWillStart Event.

While a best practice, these are _not_ necessary for the CommandWillStart Callback to perform.
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: Reactor causes fatal error.
« Reply #3 on: June 15, 2012, 01:06:39 PM »
@ mr_nick -

You may find this post to be helpful; (if you scroll down a bit) it contains a few different Command Reactor examples, with some extra goodies as an attachment.

Note - This is not the only way to code a Command Reactor, and the dependent Callback functions. This is only intended to be an example. I'm sure there are better, smarter, faster ways of doing this.

HTH
"How we think determines what we do, and what we do determines what we get."

mr_nick

  • Guest
Re: Reactor causes fatal error.
« Reply #4 on: June 18, 2012, 11:35:47 AM »
Sorry for the delay in responding - not been getting any notifications.

Anyway, I seem to have pinpointed the cause of the error. As it turned out, the MVSETUP command is a lisp file so I was able to put in a few flags and find the source of the error. It seems that in the error routine, command-s has been used rather than the more common command. And it is command-s that is crashing the reactor. If you have the CommandWillStart reactor loaded and then issue this line of code then AutoCAD will crash

(command-s "_.line" "0,0,0" "10,10,0" "")

If I edit the MVSETUP file to use command rather than command-s then no fatal error is encountered.

mr_nick

  • Guest
Re: Reactor causes fatal error.
« Reply #5 on: June 19, 2012, 01:18:12 AM »

...I've tested the code you posted, and while I would not code it that way personally, the code tests fine using Civil 3D 2011:

Code: [Select]
Command: mvsetup

Initializing...
Enable paper space? [No/Yes] <Y>: *Cancel*


Command: !abc
123

Seems that command-s slipped in to the 2012 release of AutoCAD and that's why it works OK for you. I've logged a bug report with Autodesk about this so will wait and see if anything comes back. In the meantime it looks like I'll need to swap out the affected files which contain command-s calls with older copies from 2011 and hope that nothing bad happens  :|

mr_nick

  • Guest
Re: Reactor causes fatal error.
« Reply #6 on: June 19, 2012, 07:54:06 AM »
From AutoCAD Help Files
Important. Although the command-s function is similar to the command function, caution should be taken when using U or UNDO to roll back the system state if there is an AutoCAD command already in progress when the AutoLISP expression is entered. In that case, the results of running UNDO may cause the command in progress to fail or even crash AutoCAD.
-------

So, the fact that a command reactor is running at the time of the command-s undo is what is triggering the crash. Looks like Autodesk didn't consider reactors when implementing this command.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Reactor causes fatal error.
« Reply #7 on: June 19, 2012, 08:16:04 AM »
What is the purpose of the command-s function? Is this new to 2012?

mr_nick

  • Guest
Re: Reactor causes fatal error.
« Reply #8 on: June 19, 2012, 08:20:31 AM »

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Reactor causes fatal error.
« Reply #9 on: June 19, 2012, 08:30:50 AM »

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: Reactor causes fatal error.
« Reply #10 on: June 20, 2012, 03:31:53 PM »
Quote
This statement is incorrect... One need only code for the Events which one intends to monitor, or react to, and _not_ "ANY event that can happen."

Any good developer will: Reactors are dangerous.  It's all well and good for you to hack together bad code for your own use, but until you have a better grasp of reactors you should refrain from giving advice.

Just sayin.

jb
James Buzbee
Windows 8

owenwengerd

  • Bull Frog
  • Posts: 451
Re: Reactor causes fatal error.
« Reply #11 on: June 20, 2012, 04:11:30 PM »
James, please clarify what you mean and why you think it is dangerous.

BlackBox

  • King Gator
  • Posts: 3770
Re: Reactor causes fatal error.
« Reply #12 on: June 20, 2012, 05:30:29 PM »

You have to code for ANY event that can happen: CommandStart, CommandEnded, CommandCancelled. 

This statement is incorrect... One need only code for the Events which one intends to monitor, or react to, and _not_ "ANY event that can happen."

However, as a minimum standard (IMO)... A responsible developer will implement CommandCancelled, CommandEnded, and CommandFailed Event Callback(s) when modifying the end-user's environment (i.e., settings, sysvars, etc.) via the CommandWillStart Event.

While a best practice, these are _not_ necessary for the CommandWillStart Callback to perform.

@ mr_nick -

You may find this post to be helpful; (if you scroll down a bit) it contains a few different Command Reactor examples, with some extra goodies as an attachment.

Note - This is not the only way to code a Command Reactor, and the dependent Callback functions. This is only intended to be an example. I'm sure there are better, smarter, faster ways of doing this.

Any good developer will: Reactors are dangerous.  It's all well and good for you to hack together bad code for your own use, but until you have a better grasp of reactors you should refrain from giving advice.

Just sayin.

James,

I meant no disrespect in my reply to you... Your statement _is_ incorrect.

I am always interested in constructive criticism... Can you provide examples as to where I have:

  • "Hack[ed] together bad code?"
  • Demonstrated an insufficient "grasp of reactors?"

Otherwise, kindly retract your false allegations.
"How we think determines what we do, and what we do determines what we get."

mr_nick

  • Guest
Re: Reactor causes fatal error.
« Reply #13 on: June 20, 2012, 09:11:57 PM »
Gents,

This has all got very messy and decidedly off-topic.

My initial post with the reactor example was purely that - an example. It was not a piece of my actual code and thus it was not constructed with any other 'supporting' features.

My point was, that simply having the reactor present in a session, regardless of what it was actually doing, a fatal error was triggered when the command-s function was used. No manner of building reactors in correct or incorrect ways will get past this issue - Autodesk even went to the trouble of writing in their own help files that command-s will crash AutoCAD when used incorrectly, something they seem to have done after not reading their help. Do as we say, not as we do.

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: Reactor causes fatal error.
« Reply #14 on: June 21, 2012, 09:41:51 AM »
Owen,
I bow to your genius.  My experience with reactors has been predicated with experience with ObjectDBX: A programmer must code for every possible event.  I learned reactors and ObjectDBX from Tony T. Would you not agree?

jb
« Last Edit: June 21, 2012, 10:13:38 AM by jbuzbee »
James Buzbee
Windows 8