LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

memory management and variable resets

Hi,

I mistakenly sent my problem to a wrong forum, whose link is below. If a
forum's moderator could move the thread here, it'd be great 😉

long version :
http://forums.ni.com/ni/board/message?board.id=170&message.id=136661#M136661

Just to sum up my problem, I wrote a XML library who acts as a wrapper
of the CVIXML library for the application I'm working on. I could get it
to work for writing XML files quite easily, though for writing, it's another
story.

I'm getting some local variables reset at some functions calling (I
wrote), and once the values has been changed, obviously, the algorithms
won't work as expected. I'm being stuck on that problem, because it goes
beyond what I'm able to know or find out... But though I'm trying to find
other ways, resolving it would help me check if such behaviour could happen
in other modules, and even use the lib I wrote which is 80-90% from being
done.

thanks for help,
apologies for being stupid,
--
Bernard Pratz
IT Developer
Engineering service
Chelton Telecom & Microwave
0 Kudos
Message 1 of 7
(3,827 Views)

I am not certain why this might be happening. Try adding a watch for those variables and configure the watch to break when the value of those variables change. Hopefully that might give you an idea of who is changing this.  Another thing to look at would be to make sure you don't have variables with the same name in any other module in your project that are not globally available. Maybe the variable scoping is getting causing something like this to happen. Just search the project for those variable names and make sure they are not being used in other modules. If they are, see if you can make them static. It's hard to guess why this might be happening without seeing the project, so I'm just guessing here.

I hope this helps

Bilal Durrani
NI
0 Kudos
Message 2 of 7
(3,809 Views)
    Hi,

        First of all, thanks for your answer, and after further research on that problem, I came to
    believe that some CVIXML Functions are doing wrong things. My guess is that I want to
    pass local variables, by reference, to the library function, those functions may not have been
    designed this way.

        Then I attached the library's source code to my project, and I did a step by step debug. As
    I have to continue my work, and can't spent too much time on that (that's why I posted on forums,
    actually), I can't go further and validate of my hypothesis. But I found out that the functions do
    actually reset the referenced variables, and that those references are being kept in static functions.

        So now, I think I know *where* my variables are being resetted, but I can't explain why and when.
    I mean, though the xmlget_* functions I wrote are calling some functions that call the static functions,
    it seems that care has been taken while writing the static functions, and they should'nt do wrong with
    my variables, even if declared locally.

        So, as a conclusion, I'll say that my local variables are getting for some reason out of scope in
    some libraries functions, and get resetted as a side effect of those functions not being able to work
    on them. Then, the most likely solution which came obviously to me, was to use the heap, and just
    manually allocate those variables, to get rid of any scope problem... Though doing a malloc(sizeof(int));
    sounds pretty weird to me 😛

    I'll be glad to hear what you think of that, if my reasoning is right, or if am I completelly out...

    Thanks,
--
Bernard Pratz
IT Developer
Engineering service
Chelton Telecom & Microwave
0 Kudos
Message 3 of 7
(3,800 Views)
Maybe you know this, and I appologize if you do, but its important to remember that the "static" keyword has a different meaning in a "function" declaration than a "variable" declaration.  For a function, it only limits the scope of the function to the file that its specified in, where as for a variable, it keeps previous values between function calls.
 
IOW declaring a function as "static" does not mean that it retains all its variable's values between fucntion calls.  That's only true for variable's declared as static.
 
IMO, that was a bad decision to use the same word for such different functions.  Oh well,.......
 
Terry
 
 
0 Kudos
Message 4 of 7
(3,791 Views)
I do know this, but the need and the urge on finding a solution made
me go into conclusions without thinking correctly. For sure, my conclusions
were wrong, and I thank you to have pointed me such 'stupid' detail... But
after some tests, I had no bug using the heap, so it seems to be the solution,
so I may not have been *that* wrong 😛

Anyway I don't think I should stop looking into it, because I still don't
know what was really wrong, and if I don't understand that, it could become
a problem forth.

Thanks for your clever suggestion,
--
Bernard Pratz
IT Developer
Engineering service
Chelton Telecom & Microwave
0 Kudos
Message 5 of 7
(3,785 Views)
I was thinking that something like another variable called imax that is used in another module and is being (accidently?) extern'ed into this one might caused weird variable resetting behavior too. You didn't mention if the "break when value changes" watch point helped figure this out any. I'm assuming it didnt.

If you have a sample I could have a look at, I can verify the results and pass any feedback on to the developers. Something small would be nice.

Thanks

Bilal Durrani
NI
0 Kudos
Message 6 of 7
(3,773 Views)
Hi,

ok, but then, why would it behaves the same with cur_el ? And when I did the test
when I made a copy of cur_el in tmp_el, tmp_el has been resetted... That's why I didn't
believe that was that kind of bug.

I didn't have anything more using break when value change. If you want I can test
with something else than imax. I'll look into making a small example tuesday, because now
it's time for the week-end, here, and I need to rest 😉

thanks,
--
Bernard Pratz
IT Developer
Engineering service
Chelton Telecom & Microwave
0 Kudos
Message 7 of 7
(3,774 Views)