DX Infrastructure Manager

Expand all | Collapse all

Working with PDS objects in Perl SDK

  • 1.  Working with PDS objects in Perl SDK

    Posted 04-23-2009 11:29 AM
    (This is probably a question for someone at Nimsoft, but anyone can share any insights they might have...)

    I have been using the Perl SDK for a while and am very comfortable with it for the most part. Due to a bug in the Perl SDK, I was never able to use the pdsDelete() function to clean up my memory usage. Now that the bug is fixed, I am interested in keeping my scripts clean. It also appears a new script is crashing, so I want to find out how I should be handing memory.

    Here is some example Perl that shows what I am doing so far:
    my $pds_snd = pdsCreate();
    pdsPut_PCH($pds_snd, $key1, $value1);
    pdsPut_PCH($pds_snd, $key2, $value2);
    pdsPut_INT($pds_snd, $key3, $value3);
    my ($rc, $pds_rcv) = nimSessionRequest($sess, $cmd, $pds_snd, 60);

    if ( $rc ) {
        nimLog(0, "ERROR $cmd: " . nimError2Txt($rc)) if $logerr;
        return undef;
    }
    return Nimbus::smileytongue:DS->new($pds_rcv)->asHash();
    (I actually use nimNamedRequest() more than nimSessionRequest(), but this happens to be what I am using in my current script.) I really just want to return the hash, but I need to convert the PDS data structure to a PDS object to get access to the asHash() function.

    Question 1: Can I use pdsDelete($pds_snd) immediately after the nimSessionRequest() call? I assume I should delete that PDS, since I create it with pdsCreate().

    Question 2: Even though $pds_rcv came from nimSessionRequest() rather than a pdsCreate(), should I still use pdsDelete() on it before returning? (I would have to create a variable for the hash so that I can still return the hash.)

    I also have a question about the following line of code that makes a PDS object from a PDS data structure:
    $pds_obj = Nimbus::smileytongue:DS->new($pds_ds);
    Question 3: After that line, are $pds_ds and $pds_obj linked in some way? Or are they two independent items that contain the same data? Could $pds_ds be deleted after this and leave $pds_obj around?

    I think that is all of my questions for now...  :-)

    Thanks,
    Keith


  • 2.  Working with PDS objects in Perl SDK

    Posted 04-24-2009 11:43 AM
    Hi Keith,

    I'll try to answer as best I can :smileywink:

    Question 1) Yes, you both can and should pdsDelete $pds_snd after issuing the request.
    Question 2) Yes, the returned PDS is still allocated memory (even if it was allocated by the library) and should be freed with pdsDelete.
    Question 3) Yes, the two are definitely linked! Since new() creates a Perl object (which contains a reference to the PDS) it will be freed by the garbage collection in Perl when it goes out of scope. The DESTROY method will in fact call pdsDelete on the PDS, so you do not have to worry about that. From your code snippet I'm not really sure when it will go out of scope though... If you set loglevel to 2 you should see an entry for when the DESTROY method is called though...

    If you are interested you could always take a look in the PDS.pm under <NIM_HOME>/perllib/Nimbus.It will show you what all the wrapper functions do :smileywink:

    Cheers,
    Stian


  • 3.  Working with PDS objects in Perl SDK

    Posted 04-25-2009 08:12 AM
    Stian,

    Thanks!  This is great information.

    Just to make sure I understand correctly, if I turn the PDS into an object, the pdsDelete() will happen automatically when that object goes out of scope?  However, if I used the PDS as is, I need to use pdsDelete() manually?

    I have seen some log messages about a hash being destroyed, which must be the entries to which you are referring.  It was not clear to me those log messages were about the object, as opposed to the hash I get back from asHash().  I believe that most Perl objects are actually hashes underneath, so this makes sense.

    I assume that the hash I get back from asHash() has no relationship to the PDS or the object once it has been created.  I do not see how that could be the case, since it is just a regular Perl hash.  Correct?

    Thanks,
    Keith


  • 4.  Working with PDS objects in Perl SDK

    Posted 04-25-2009 09:48 AM
    Hi Keith,

    That is correct, if you turn the PDS into an object with PDS->new() then the pdsDelete will happen automatically when it goes out of scope (like all other perl objects do when they go out of scope). There might be a delay in there, since we have no control over perl's garbage collection routines, but it will get removed and the PDS will be cleaned up with an automatic call to pdsDelete. If you use it as-is then you will have to manually call pdsDelete to free up the memory. The nice thing about that is of course that you know exactly when memory is freed instead of having to wait for the garbage collection routines. I guess it's the C programmer in me that kind of likes that :smileywink:

    If I have understood the code correctly then there is no relationship between the hash you get back from asHash and the object itself. You could supply a hash as a parameter to asHash, otherwise it creates an anonymous hash internally and returns that. There is no real difference as far as I can tell.

    Hope that clears it up!

    Cheers,
    Stian


  • 5.  Working with PDS objects in Perl SDK

    Posted 04-25-2009 11:52 AM
    Outstanding!  Thanks again.  This clears things up.