Author Topic: error: null interface pointer ..  (Read 18601 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
error: null interface pointer ..
« on: November 02, 2005, 08:24:31 PM »
I've spent the last day or so trying to track down an intermittent error in an associates code.

I'll post more on this later, as time allows ; unless someone else fills in the gaps.

Long story short :

AutoDesk help files and some commentators recommend the following functionality ..
Code: [Select]
;;--------------------------------
(setq *acad-object* nil)                          ; Initialize global variable
(defun acad-object ()
  (cond (*acad-object*)                           ; Return the cached object
        (t (setq *acad-object* (vlax-get-acad-object)))
  )
)
;;--------------------------------
(setq *active-document* nil)                      ; Initialize global variable
(defun active-document ()
  (cond (*active-document*)                       ; Return the cached object
        (t (setq *active-document* (vla-get-activedocument (acad-object))))
  )
)
;;--------------------------------
(setq *model-space* nil)                          ; Initialize global variable
(defun model-space ()
  (cond (*model-space*)                           ; Return the cached object
        (t (setq *model-space* (vla-get-modelspace (active-document))))
  )
)
;;--------------------------------

 with the idea that it will be called like this ..

Code: [Select]
....
(setq myCircle (vla-addCircle (model-space)   (vlax-3d-point '(3.0 3.0 0.0)) 2.0))
....

If you are currently using code similar to that posted, it may be time to have another look at it .. and try something like this :
Code: [Select]
;;; ----- Release Bound Activex Objects --------------------------
;;;
(defun releaseObjects (ListOfQuotedVarnames / tmp)
  (foreach varname ListOfQuotedVarnames
    (if (= (type (setq tmp (vl-symbol-value varname))) 'vla-object)
      (if (not (vlax-object-released-p tmp))
        (vlax-release-object tmp)
      )
    )
    (set varname nil)
  )
)
;;--------------------------------
(releaseObjects (list '*model-space* '*active-document* '*acad-object*))

;;--------------------------------                     
(defun acad-object ()
  (cond ((and *acad-object* (not (vlax-object-released-p *acad-object*)))
         *acad-object*
        )
        (t (setq *acad-object* (vlax-get-acad-object)))
  )
)
;;--------------------------------                 
(defun active-document ()
  (cond ((and *active-document*
              (not (vlax-object-released-p *active-document*))
         )
         *active-document*
        )
        (t (setq *active-document* (vla-get-activedocument (acad-object))))
  )
)
;;--------------------------------                       
(defun model-space ()
  (cond ((and *model-space* (not (vlax-object-released-p *model-space*)))
         *model-space*
        )
        (t (setq *model-space* (vla-get-modelspace (active-document))))
  )
)
;;--------------------------------

The original code was causing an issue because he <sometimes> ran this sort of code :-
Code: [Select]
;; Global variables
 (setq thisDoc (active-document)
       mspace  (model-space)
 )
  ;;
  ;; bla, bla
  ;;
  (vlax-release-object mspace)
  (vlax-release-object thisDoc)
  ;;
  ;; bla, bla
  ;; 


Because his variable mspace  and the Global *model-space* pointed to the same object ;
 releasing that object using the mspace variable resulted in the *model-space* variable returning something invalid like this #<VLA-OBJECT 00000000>

.. So .. as well as testing that the variable exists, also check that it holds a legitimate value ie: hasn't been released.
.. hence the optional code.

kwb
« Last Edit: November 02, 2005, 08:38:28 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: error: null interface pointer ..
« Reply #1 on: November 02, 2005, 09:02:40 PM »
Forgive me for asking a dumb question but why wouldn't you use something like this?
Code: [Select]
(defun model-space ()
  (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: error: null interface pointer ..
« Reply #2 on: November 02, 2005, 09:13:22 PM »
From the Docs ..
Quote
Performance Considerations :

Repeated calls to access the AutoCAD Application, active Document, and ModelSpace objects should be avoided, as they negatively impact performance. You should design your applications to obtain these objects one time, and refer to the obtained object pointers throughout the application.


So theoretically, .. its better to assign it once and use the variable, rather than repeatedly burrow down into the Object Model.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: error: null interface pointer ..
« Reply #3 on: November 02, 2005, 09:16:50 PM »
There is an issue even if the variables are local ..

Try this ; alternatively using the AutoDesk code preloaded, then mine preloaded ..
Code: [Select]
(defun c:Test (/ mspace myCircle)
  (setq mspace (model-space))
  (setq myCircle (vla-addcircle mspace (vlax-3d-point '(3.0 3.0 0.0)) 2.0))
  (vlax-release-object mspace)
)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: error: null interface pointer ..
« Reply #4 on: November 02, 2005, 09:25:34 PM »
PS : I'm not going to suggest for a moment that this assignment be done any one particular way. ... Just that there is an issue if you blindly follow the code published by AutoDesk and some commentators.  ... as has been done in the code I was testing/debugging.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: error: null interface pointer ..
« Reply #5 on: November 02, 2005, 09:31:51 PM »
Yes I get an error with the ACAD version.
Code: [Select]
Command: test
; error: null interface pointer: #<VLA-OBJECT 00000000>
; reset after error
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: error: null interface pointer ..
« Reply #6 on: November 02, 2005, 09:33:36 PM »
I'm off to add this to my library of 'Borrowed Code'

Thanks Kerry
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: error: null interface pointer ..
« Reply #7 on: November 02, 2005, 09:35:18 PM »
heh, Test it first ...  :lmao:

no problems Alan.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: error: null interface pointer ..
« Reply #8 on: November 02, 2005, 09:36:13 PM »
Right or wrong here's my take Kerry  --

I dropped the use of wrapped globals for activedocument etc. long ago for a number of reasons, including the testing that the wrappers tend to do.

Instead I tend to write functions so they take the activedocument, modelspace, or layers collection etc. as an argument. The calling function is shouldered with the responsibility to pass a valid argument, and for any subsequent cleanup etc. Any overhead associated with repeated calls to create the object is avoided IF the caller takes care of setting aside a reference to said object before making the functions calls.

In it's simplest form.

Code: [Select]
(defun c:MtProggy ( / foo main )

    (defun foo ( object )
        ;;  foo takes an object, but doesn't
        ;;  care how it comes into existance
        ...
    )

    (defun main ( / object )

        ;;  create the object
   
        (setq object
            (SomeCallThatResultsInObject)
        )

        ;;  use the object ad nauseum
       
        (repeat (SomeCallThatReturnsIterationCount)
            (foo object)
        )

        ;;  dump it
       
        (vl-catch-all-apply
           '(lambda ( )
                (vlax-release-object object)
            )
        )
       
        (princ)
       
    )
   
    (main)

)

No worries of double references, things going out of scope etc. Perhaps I look at this too simplistically, but this works and performs well for me.

/2¢
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: error: null interface pointer ..
« Reply #9 on: November 02, 2005, 09:42:03 PM »
Quote
... Perhaps I look at this too simplistically, but this works and performs well for me.

I concur .. About it working well.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: error: null interface pointer ..
« Reply #10 on: November 02, 2005, 09:47:08 PM »
User Beware :

The solution I was trying to demonstrate in this particular case suited the circumstances because the calling shown was used extensively. Reworking the wrapped stuff seemed the most expedient solution .. though not the best long term, perhaps.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

LE

  • Guest
Re: error: null interface pointer ..
« Reply #11 on: November 02, 2005, 10:22:48 PM »
I only use vlax-release-object on code like this:

Code: [Select]
  (setq vlaLock (vlax-create-object "xxxxxxx.xxxxxxxx"))
  (if (and vlaLock (= (type vlaLock) 'vla-object))
    (progn
      (vlax-put-property
vlaLock
'SoftwareName
dtt_softname)
      (vlax-put-property vlaLock 'LiberationKeyLength dtt_num)
...
      ;; release the activex control
      (vlax-release-object vlaLock)
      (gc))
« Last Edit: November 02, 2005, 10:33:38 PM by LE »

Sdoman

  • Guest
Re: error: null interface pointer ..
« Reply #12 on: November 02, 2005, 10:36:11 PM »
Interesting topic.

I've been using code for a few years now that is almost exactly as the AutoDesk sample code shown in the first post to this thread.  But I've never had a problem with a null pointer or anything like that (to my knowledge).
 

LE

  • Guest
Re: error: null interface pointer ..
« Reply #13 on: November 02, 2005, 10:45:48 PM »
There is only few cases when a release is needed on visual lisp code.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: error: null interface pointer ..
« Reply #14 on: November 02, 2005, 10:56:56 PM »
This exact situation is one of the reasons why I attempt to use unique names for any global variables I use.
All it takes is for code in a VLX or FAS that the user has no control over to use code as demonstrated with the same variable names as you and the whole lot falls down.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.