LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

variables reset and memory management

    Hi,

        I'm writing an application that works on sophisticated
    data-structures, and though rely a lot on memory allocations.
    As I wanted to do a clean job as much as possible, I divided
    the work in different modules, and I'm encountering a problem
    while working with two of them.

        I wrote a data library that brings data structures needed
    by the software to work, and the last one I did was a XML wrapper
    for the CVIXML library.

        Sadly, while testing those two libraries to debug and validate
    them, I encountered a 'strange' bug in the reading functions. As
    concrete example is worth any explaination, here it is :

        First of all, I wrote several little functions like this one
    to earn some local declarations and copy/paste in my code.
    (there's xmlget_elt_val(), xmlget_attr_tag() and xmlget_attr_val())

[cf : xmlget_elt_tag() function below]

        I use them a lot through my code, but at one point, in that
    function (not even the first time I'm calling it...), I got quite
    an illogical behaviour.

--
Bernard Pratz
IT Developer
Engineering service
Chelton Telecom & Microwave
0 Kudos
Message 1 of 9
(2,964 Views)
        Using the debugger, I could spot exactly where and when the variable
    gets resetted. It is at the calling of xmlget_elt_tag(), and inside the
    function the variable has a '0' for value.

        Obviously, I never expected such behaviour. I pass the argument by
    value, using the const statement to explicitely tell the compiler 'I don't
    want that variable to be changed !@#$'...

        Then I tried to find out a workaround for it, so I declared a
    CVIXMLElement tmp_el; and made a copy of the cur_el's value :

-----------------------8<-----------------------8<-------------------
[...]
    for (i=0;i<imax;++i) {
        derr(CVIXMLGetChildElementByIndex(root,i,&cur_el));
        tmp_el = cur_el;
        xmlget_elt_tag(cur_el,&str);
[...]
-----------------------8<-----------------------8<-------------------

        And this time, cur_el was resetted. Then, I thought it may be
    something unexpected happening in the internals of the function's
    call system, so I tried to disassemble the code as last hope... But
    as I could expect, I couldn't get much from it, as the compiler adds
    much more things that I could have imagined.
--
Bernard Pratz
IT Developer
Engineering service
Chelton Telecom & Microwave
0 Kudos
Message 2 of 9
(2,961 Views)
        Even deciding to work through using the workaround, I got into
    a similar trouble just a function latter, but at another call. For
    the context, I'm calling the xmlextract_Banc() function several times
    in the code, to get different amount of data.

        So, in the following function (called from xmlextract_Banc()) I
    discovered that I had one attribute never read. Once more, using the
    debugger, I found out that this time the xmlget_attr_val() function
    is resetting the imax variable at the second iteration of the for loop.

[cf: xmlextract_Appareil() below]

        Then I understood that any workaround I could do would only bring that
    problem at another point further in the code, and will not be resolved. I'm
    thinking this maybe some kind of overflow of something, somewhere... But I
    can't tell what and where (...if I'm right).

        I'm using the 'data' library, which defines the structs and all the
    functions such as create_Type() or push_Type() I use in those snippets, with
    the GUI, and the measurement library without any trouble, but that doesn't
    mean it's not guilty...

        I'm really clueless about this problem, at the point I'm trying to find
    out other ways around to get a xml library to work with the application. I'm
    trying out right now to get a 'libxml++' dll to be wrapped and included in the
    project, instead of using genuine's labWindows/CVI XML interface. But as I wrote
    almost 85-90% of the library before getting into that trouble, it'd be really
    more than helpful if someone could help me find out what's wrong.

    Thanks for reading this post,
    I can give more informations about the project if needed
    and thanks again for any answer I could get.

PS: I had to split the message in order to get into the 5000 chars limit per post
    I know there may be a reason for that limit to exists, but I prefered to ignore
    it and make my problem's explaination much verbose
--
Bernard Pratz
IT Developer
Engineering service
Chelton Telecom & Microwave
0 Kudos
Message 3 of 9
(2,958 Views)
-----------------------8<-----------------------8<-------------------
int xmlget_elt_tag(const CVIXMLElement root, char** str) {
    int i;
    
    derr(CVIXMLGetElementTagLength(root,&i));
    if (i < 1)
        return 0;
    
    *str = malloc(i+1);
    derr(CVIXMLGetElementTag(root,*str));
    str[i++] = '\0';
    
    return 1;

}
-----------------------8<-----------------------8<-------------------
--
Bernard Pratz
IT Developer
Engineering service
Chelton Telecom & Microwave
0 Kudos
Message 4 of 9
(2,956 Views)
-----------------------8<-----------------------8<-------------------
Banc* xmlextract_Banc(const CVIXMLElement root) {
    CVIXMLElement cur_el;
    CVIXMLElement cur_attr;
    Banc* banc;
    int i, imax;
    char* str=NULL;
    
    banc = create_Banc();
    
    // For each attribute of <banc>
    derr(CVIXMLGetNumAttributes (root, &imax));
    for (i=0;i<imax;++i) {
        derr(CVIXMLGetAttributeByIndex(root,i,&cur_attr));
        xmlget_attr_name(cur_attr,&str);
        if (strncmp(str,"id",2) == 0) {
            xmlget_attr_val(cur_attr,&str);
            banc->id = atoi(str);
        } else if (strncmp(str,"nom",3) == 0) {
            xmlget_attr_val(cur_attr,&str);
            banc->nom = str;
        }
    }
    
    // For each element, son of <banc>
    derr(CVIXMLGetNumChildElements(root,&imax));
    for (i=0;i<imax;++i) {
        derr(CVIXMLGetChildElementByIndex(root,i,&cur_el));
        xmlget_elt_tag(cur_el,&str);   /* cur_el gets resetted to '0' *
                                        * at this call.               */
        if (strncmp(str,"appareil",8) == 0) {
            push_Appareil_in_Banc(banc,xmlextract_Appareil(cur_el));
        } else if (strncmp(str,"mesure",6) == 0) {
            // nop (for now)
        }
    }
    
    return banc;
}
-----------------------8<-----------------------8<-------------------
--
Bernard Pratz
IT Developer
Engineering service
Chelton Telecom & Microwave
0 Kudos
Message 5 of 9
(2,955 Views)
-----------------------8<-----------------------8<-------------------
Appareil* xmlextract_Appareil(const CVIXMLElement root) {
    CVIXMLElement cur_el=0;
    CVIXMLAttribute cur_attr=0;
    Appareil* ap;                                             
    int i, imax;
    char* str=NULL;
    
    ap = create_Appareil();
    
    // Pour chaque attribut d'appareil
    derr(CVIXMLGetNumAttributes (root, &imax));
    for (i=0;i<imax;++i) {
        derr(CVIXMLGetAttributeByIndex(root,i,&cur_attr));
        xmlget_attr_name(cur_attr,&str);
        if (strncmp(str,"id",2) == 0) {
            xmlget_attr_val(cur_attr,&str);
            ap->id = atoi(str);
        } else if (strncmp(str,"raddr",5) == 0) {
            xmlget_attr_val(cur_attr,&str); /* on second iteration of the loop, *
                                             * imax gets resetted...            */
            ap->raddr = atoi(str);
        } else if (strncmp(str,"nom",3) == 0) {
            xmlget_attr_val(cur_attr,&str);
            ap->nom = str;
        } else {
            DebugPrintf("error: tag '%s' is invalid",str);
        }
    }
 [...] /* not relevant */

    return ap;
}
-----------------------8<-----------------------8<-------------------
--
Bernard Pratz
IT Developer
Engineering service
Chelton Telecom & Microwave
0 Kudos
Message 6 of 9
(2,950 Views)
You posted in the LabVIEW forum (No memory allocations here).

___________________
Try to take over the world!
0 Kudos
Message 7 of 9
(2,945 Views)
    Ugh... I just realised I posted the messages to the wrong forum.
    Could any moderator move the thread to labWindows' forum, please ? 🙂

    really sorry for the inconveniences (very long post, wrong forum...),
    for the excuse I'll tell that this problem drove me so crazy that I
    can't even post in the right place anymore 😛

    and again, all apologies,
--
Bernard Pratz
IT Developer
Engineering service
Chelton Telecom & Microwave
0 Kudos
Message 8 of 9
(2,942 Views)
Hi Guyzmo,
 
I have just had problems with cvixml labwindows functions.
In fact, in my case, my memory problem doesn't occured in debug mode but it occurs in release mode!
The code of the two executables (release and debug) is exactly the same and it is launched from the same directory!
The lines with the problem are the following:
"     if ( xml_error = CVIXMLGetElementTag (parent_element, name))
    return xml_error;
 CVIXMLGetNumChildElements (parent_element,&nb_child);  // the fatal error occured here
If I insert a function between the two lines, a function  that displays a message box or that writes in a log file, the error doesn't occur !!?
It is really amazing !
So I insert my log file writing function before the CVIXMLGetNumChildElements and now, I have no problem 😉
 
Sgs
0 Kudos
Message 9 of 9
(2,830 Views)