Releasing Objects

Discussion in 'AutoCAD' started by Alexander V. Koshman, Apr 10, 2004.

  1. Hello to everybody!
    -------------------------
    Sorry for disturbing you all with the question
    that may not be very clever!

    In some code that one can see in this NG and in others here,
    for example, the answer of L.E. (Hi, Luis! ;^) from:

    * autodesk.autocad.2000general
    * 05/05/2001 by Albert Giuliano
    * Re: a button for toggling vport locking??

    I see the next object realesing method at the end of a function:

    (if (vlax-object-released-p obj)
    (setq obj nil)
    (vlax-release-object obj)
    )

    and some others.

    My questions:
    --------------
    1. Why do set 'obj' to 'nil'? Is it nesessery or it's a "dump" operation?
    2. If *there are no* 'obj', you'll get an error!

    So, why do not examine the existense of 'obj' first:

    (if obj
    (if (not (vlax-object-released-p obj))
    (vlax-release-objectobj)
    )
    )

    or examine that it IS and it IS a VLA-object
    and then release it and set it to 'nil':

    (if (= (type obj) 'VLA-OBJECT)
    (if (vlax-object-released-p obj)
    (setq obj nil)
    (progn
    (vlax-release-object obj)
    (setq obj nil)
    ) ; - 'progn'
    ) ; - 'if obj NOT RELEASED YET'
    ) ; - 'if obj EXISTS'

    What do you think about that?

    Thank you all for your time and help and ideas!
     
    Alexander V. Koshman, Apr 10, 2004
    #1
  2. See below.




    The above will not work, because vlax-release-object
    requires a valid vla-object, but the expression before
    it sets the obj variable to nil.
    See above.

    As to the remainder, you should not conditionally test
    an object to see if you need to release it. You should
    write your code to explicitly release it if you created
    it, and do that only once. If the object is not valid,
    that means there is an error somewhere else in the code,
    and allowing (vlax-release-object) to _fail_ will serve
    to indicate that.

    By testing the validity of the vla-object and not releasing
    it if it isn't valid, you may be concealing another error
    in the code.

    Try to avoid writing code that hides errors.
     
    Tony Tanzillo, Apr 10, 2004
    #2
  3. See below.

    Thanks Tony!
     
    Alexander V. Koshman, Apr 11, 2004
    #3
  4. Tony,

    Luis' code _does_ work. I'm sure you simply missed that he is setting the
    variable to nil if it has already been released (then statement), and
    releases it if it was not released (else statement). However, Luis is not
    setting the variable to nil in the else clause, so it isn't really complete
    either.

    We all appreciate you wise comments about avoiding concealing errors.


    --
    R. Robert Bell


    "Tony Tanzillo" <tony.tanzillo/at/caddzone/dot/com> wrote in message
    See below.




    The above will not work, because vlax-release-object
    requires a valid vla-object, but the expression before
    it sets the obj variable to nil.
    See above.

    As to the remainder, you should not conditionally test
    an object to see if you need to release it. You should
    write your code to explicitly release it if you created
    it, and do that only once. If the object is not valid,
    that means there is an error somewhere else in the code,
    and allowing (vlax-release-object) to _fail_ will serve
    to indicate that.

    By testing the validity of the vla-object and not releasing
    it if it isn't valid, you may be concealing another error
    in the code.

    Try to avoid writing code that hides errors.
     
    R. Robert Bell, Apr 12, 2004
    #4
  5. Hi Robert!
    --------------
    I think so and I was very astonished about this...
    My comments STILL half-wise for a years...


    Searching this NG I found a very interesting discussion:
    "vlax-release-object...cont'd"
    started by Cliff Middleton Sep/27/01
    with participation of you Robert & John Uhden & Tony Tanzillo
    & Frank Oquendo & Owen Wengerd & Luis Esquivel & others...

    It's cool!

    And many others about that.

    So I made a conclusion:
    ============================
    1. Not to set VLA-objects to NIL *before* releasing them.
    2. Release them and NOT to set them to NIL after if they are locals.
    3. Don't use globals like ACAD and DOC ("no globals - no problem...")
    4. Release VLA-objects ALWAYS if they were:
    a) external (applications like Excel) - not tryed yet...
    b) created with (vla-add-... )
    c) created with (vlax-ename->vla-object ...)
    d) selection sets (it seems to me that I had some problems wich were
    gone after releasing SSs)

    Is it true?

    Thank you all!
     
    Alexander V. Koshman, Apr 12, 2004
    #5
  6. Your summary looks good to me. I can't think of anything else at the moment.

    --
    R. Robert Bell


    Hi Robert!
    --------------

    ....

    Searching this NG I found a very interesting discussion:
    "vlax-release-object...cont'd"
    started by Cliff Middleton Sep/27/01
    with participation of you Robert & John Uhden & Tony Tanzillo
    & Frank Oquendo & Owen Wengerd & Luis Esquivel & others...

    It's cool!

    And many others about that.

    So I made a conclusion:
    ============================
    1. Not to set VLA-objects to NIL *before* releasing them.
    2. Release them and NOT to set them to NIL after if they are locals.
    3. Don't use globals like ACAD and DOC ("no globals - no problem...")
    4. Release VLA-objects ALWAYS if they were:
    a) external (applications like Excel) - not tryed yet...
    b) created with (vla-add-... )
    c) created with (vlax-ename->vla-object ...)
    d) selection sets (it seems to me that I had some problems wich were
    gone after releasing SSs)

    Is it true?
     
    R. Robert Bell, Apr 12, 2004
    #6
  7. Humble question:

    Why should we explicitly release VLA-objects when the symbols are local to the
    function? Aren't they released and cleaned up automatically when the function
    ends or the symbol goes out of scope?

    e.g. (given e = ename)

    (defun foo (/ e Obj)
    (if (setq e (car (entsel "\nSelect an object: ")))
    (progn
    (setq Obj (vlax-ename->vla-object e))
    (..do something interesting here..)
    (vlax-release-object obj) ;<-- do I need this? If so, why?
    )
    )
    (princ)
    )

    Matt

     
    Matt Stachoni, Apr 13, 2004
    #7
  8. Hi Luis!
    -----------
    From: fishspawned <>
    Newsgroups: autodesk.autocad.2000general
    Subject: a button for toggling vport locking??
    Date: Thu, 3 May 2001 06:39:26 -0700

    Albert Giuliano said that was your code...
    hm... a piece of self-criticism...
    And how do you do it all without releasing them???

    Thank your for your good (and not good) ideas! ; )
     
    Alexander V. Koshman, Apr 13, 2004
    #8
  9. Hello Matt!
    ---------------
    You have to read a great and very didactic discussions about that in this
    NG!

    For example:

    vlax-release-object...cont'd
    Posted by: Middleton, Cliff
    Date: Sep/27/01 - 05:15 (GMT)

    In this NG!
    and

    Vlax-release-object help neede in VLisp (8 replies)
    Posted by: McManamy, Rodney
    Date: Jul/26/00 - 21:20 (GMT)

    Search the Web-side of the Autodesk NG:
    http://discussion.autodesk.com/forum.jspa?forumID=130
    something like "release" or "releasing" as I did!

    You'll be satisfied! ; )
     
    Alexander V. Koshman, Apr 13, 2004
    #9
  10. Glad to hear you Luis!
    -------------------------
    Hm! I use them local! But it seems to me that I got reactor
    crashes before I've heard about VLA-objects releasing ...
    I will try! Thank for good NEWS!
     
    Alexander V. Koshman, Apr 13, 2004
    #10
  11. Never mind, I was hoping for a quick answer without resorting to a lengthy time
    wasting search.

    Matt

     
    Matt Stachoni, Apr 13, 2004
    #11
  12. Hi Matt!
    ---------
    See my reply to Luis: before he sad he don't use releasing now
    I WAS certain in fact that NOT releasing Objects cause AutoCAD closes
    unexpectedly and such a problems!..
    I HAD permanent errors and AutoCAD crashes when DIDN'T
    (vlax-release-object obj) in Reactors. Maybe it's a problem only
    in reactors? Maybe it was a problem only IN MY reactors?..
    So I was surprised a lot by Luis post...
    Now I would know: HOW DOES HE LIVES without releasinig
    VLA-objects in his code (and reactors???)...

    My English don't allow me to paraphrase the long discussions in here about
    that.
    I *would* release them in my code, because of possible errors and AutoCAD
    crashes (in my reactors) - the only place when I try to use the new VLA
    possibilities.
    In my other code I rarely use VLA-functions & VLA-objects yet.

    Many happy LISP to you! ; )
     
    Alexander V. Koshman, Apr 14, 2004
    #12
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.