11-25-2009 08:27 AM
I found out what the problem was
errChk(Ini_GetStringCopy(IniTextHandle,g_sOperatorID, "OperatorID",
&g_sOperatorID));////////like you said why i am passing the same parameter for the section parameter
that was a copy and paste error, basically earlier in my code Im putting a string into section parameter, then later I want to be able get the same string
from memory that i stored earlier in my code, because its going to have some values under the section
11-25-2009 08:52 AM - edited 11-25-2009 08:52 AM
darnell wrote:well the reason Im naming it as a global is because I want to remember where I am, Im using that global in many places through the source file
its define as g_sOperatorID[BUF_LEN];
So if it's statically defined with a fixed lenght you cannot allocate memory on that variable (neither can you free it!)
darnell wrote:
also can you explain this?""it is dimensioned, used and freed in the same callback""
Inside the callback you had both calloc and free, and you are using it in Ini_GetStringCopy: all this made me wonder wether you really need a global variable or not, since no information is returned from the callback once you free it!
darnell wrote:I found out what the problem was
errChk(Ini_GetStringCopy(IniTextHandle,g_sOperatorID, "OperatorID",
&g_sOperatorID));////////like you said why i am passing the same parameter for the section parameter
that was a copy and paste error, basically earlier in my code Im putting a string into section parameter, then later I want to be able get the same string
from memory that i stored earlier in my code, because its going to have some values under the section
You shouldn't use Ini_GetStringCopywith a statically defined variable: that command uses StrDup to dynamically allocate the string into which to copy element value, so you are likely to get errors in it.
I suggest you to use Ini_GetStringIntoBuffer instead, which does not allocate the buffer.
I don'understand exactly what you are doing with Section Name parameter but it's perfectly legal to pass a string into this parameter: as far as its value identifies an existing section you can regularly read the element value.
11-25-2009 08:55 AM
Roberto is trying to say that, if this variable (g_sOperatorID) is global, why are you "free"ing it?
If you call free for g_sOperatorID, then you won't be able to get its value from another part in your code.
I also realized that you actually allocate memory for g_sOperatorID after you use it for Ini_GetStringCopy.
That does not make sense!
If it already has a value, there is no need to allocate memory space for it.
Do not overuse a single variable for different purposes.
11-25-2009 09:05 AM
sorry about that guys I forgot add the other changes
its declare at the top char *g_sOperatotID2=0;
so do this make since now?
int Boresight_Command(double *dAZposX,double *dELposY)
{
int error=0;
char *sErrorMsg1=0;
char sAZposX[BUF_LEN]={0};
char sELposY[BUF_LEN]={0};
char sCrtCommand[BUF_LEN]={0};
char sDirPath[BUF_LEN_FOR_DIRECTORY];
IniText IniTextHandle=0; // Handle for the text file.
char sPathName[BUF_LEN_FOR_DIRECTORY];
GetProjectDir(sDirPath);
MakePathname(sDirPath,KEY_TEXT_FILE,sPathName);
//Verify if there is enough memory
nullChk(IniTextHandle=Ini_New (0));
g_sOperatorID2 = calloc( BUF_LEN, sizeof(char) );
//Verify if file exist on disk
error=FileExists(sPathName,0);
if(error<=VI_SUCCESS)
{
Terminate_Executable();
return FAILURE;
}
errChk(Ini_ReadFromFile (IniTextHandle,sPathName));
//g_sOperatorID = calloc( BUF_LEN, sizeof(char) );
errChk(Ini_GetStringCopy(IniTextHandle,g_sOperatorID, "OperatorID",
&g_sOperatorID2));
LOG_FILE(g_sOperatorID2);
errChk(Ini_GetDouble(IniTextHandle,g_sSkrSerialNumberk,X_OFFSET_POS,
dAZposX));
errChk(Ini_GetDouble(IniTextHandle,g_sSkrSerialNumberk,Y_OFFSET_POS,
dELposY));
Fmt(sAZposX,"%f",*dAZposX);
sprintf(sCrtCommand,"SERVO, WRITE, 098000034, %s",sAZposX);
Send_SERVO_Command(sCrtCommand);
Fmt(sELposY,"%f",*dELposY);
sprintf(sCrtCommand,"SERVO, WRITE, 098000038, %s",sELposY);
Send_SERVO_Command(sCrtCommand);
Error:
if(IniTextHandle)
{
//Cleaning memory
Ini_Dispose (IniTextHandle);
//Cleaning memory
free (g_sOperatorID2);
}
if(error < VI_SUCCESS)
{
sErrorMsg1 = GetGeneralErrorString (error);
MessagePopup ("ERROR", sErrorMsg1);
Terminate_Executable();
}
return error;
}
11-25-2009 09:21 AM
I still do not understand why using a global variable for g_sOperatorID2...
If your goal is to pass the value to LOG_FILE () function you do not need a global for it: use a local variable and that's all.
char g_sOperatorID2[BUF_LEN];
errChk(Ini_GetStringIntoBuffer (IniTextHandle, g_sOperatorID, "OperatorID",
g_sOperatorID2, BUF_LEN - 1));
LOG_FILE(g_sOperatorID2);
No need for a global, nor for calloc-free stuff here!
If your goal is to return g_sOperatorID2 to the caller, why free-ing it at callback end?????
By the way: there were two errors in code: the variable is defined as g_sOperatotID2but used as g_sOperatorID2. Aren't you copying your actual code? Or as usual you are making us discuss on hypotheses/attempts/non-actual-code/non-working-examples or so?
11-25-2009 09:34 AM
I just tested my code and fix the errors, I was going off of assumptions, got a question?
errChk(Ini_GetStringIntoBuffer (IniTextHandle, g_sOperatorID, "OperatorID",
g_sOperatorID2, BUF_LEN - 1));////////////////////////////////////why BUF_LEN-1, what was the purpose of that, why are you subtracting one
and again why you chose the Ini_GetStringIntoBuffer.
because earlier in my call Im using Ini_PutString
11-25-2009 10:37 AM
BUF_LEN - 1: my fault, I was dealing with the terminating NUL byte: since it is included in the byte count you can pass BUF_LEN instead.
I chose Ini_GetStringIntoBuffer instead of Ini_GetStringCopy because you are using a statically dimensioned string. Ini_GetStringCopy expects a pointer to a string to allocate dynamically. You should not be able even to compile a code with the pointer to an array passed to the function instead of a pointer to a pointer.
11-25-2009 10:55 AM
errChk(Ini_GetStringIntoBuffer (IniTextHandle, g_sOperatorID, "OperatorID",
g_sOperatorID2, BUF_LEN));
doesnt the error check suppose to turn to Error, if it didnt get nothing from the buffer at all? instead it passes over the Error:
when I step through it just gave me "", so doesnt it suppose to return to Error:
also this check if(happOJB) is equaivalent to if(happOJB!=VI_SUCCESS)?
is it equivalent to any other check?
11-25-2009 11:14 AM - edited 11-25-2009 11:16 AM
This is the help for the return code of the function:
status:
Indicates whether a tag/string pair was found with the specified
tagName in the section specified by
sectionName. A positive return value indicates that a tag/string pair was found. If the string portion of the tag/string pair was empty, such as in tag 1 = "" the string is considered to have been found, and it is the empty string (""). If the tag's value is empty, such as in tag 1 = no tag/value pair is found. A zero indicates that no tag/string pair was found. A negative value indicates an error. If the value is from –1 to –999, it is one of the error values defined in userint.h. If the value is from –5000 to –5999, it is one of the error values defined in toolbox.h. For this function, the only error you are likely to encounter is:
|
That is:
As you should have seen looking at the definition of errChk in toolbox.h (as I suggested you many times), the macro jumps to the error label if the return code if <0.
So if you want to react to the not-found/empty-string case you must add a check:
errChk(Ini_GetStringIntoBuffer (IniTextHandle, g_sOperatorID, "OperatorID",
g_sOperatorID2, BUF_LEN));
if (error == 0) {
error = ToolErr_MissingItem; // defined in toolbox.h
goto Error;
}
11-25-2009 10:57 PM
Hi for this can I just call GetTooLError, to be able to pull from the enum that is defined in toolbox.h
for the ToolError