LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

dynamic memory corrupt when executing Ini_ReadFromFile()

I am running a timer loop. The Ini_ReadFromFile() works perfectly find in the first loop. However, in next loop, dynamic memory corrupt msg occured. Can anybody tell me why??
 
int CVICALLBACK DFFilter1 (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
    StrDFFileName = ReadSiteSettings("Kuantan","File");
   StrDFFileDir = ReadSiteSettings("Kuantan","Folder for Data Filtering");
}
0 Kudos
Message 1 of 16
(4,738 Views)
The ReadSiteSettings() for previous post is as below:
 
char* ReadSiteSettings(char *SectionName, char *ItemName)
{
 FILE *fpFile;
 IniText IniFile;
 char *Str,StrFilePath[500];
 
 //strcpy(StrFilePath,"d:\\Project\\GI Netcom\\NOSMOC\\SiteSettings.ini");
 MakePathname(StrPrjDir,"SiteSettings.ini",StrFilePath);
   
 IniFile = Ini_New(0);
 if(Ini_ReadFromFile(IniFile,StrFilePath)==0)
 {
  if(Ini_SectionExists(IniFile,SectionName))
  {
   Ini_GetRawStringCopy(IniFile,SectionName,ItemName,&Str);
  }
 }
 Ini_Dispose(IniFile);
 
 return Str;
}
0 Kudos
Message 2 of 16
(4,739 Views)
I am using CVI.8.5. This is very urgent. Please help me. My issue is as previous post.
0 Kudos
Message 3 of 16
(4,737 Views)

The local pointer to str inside ReadSiteSettings cannot be passed back to the caller. I have rewritte your functions using pointers to pointers so that Ini_GetRawStringCopy can correctly allocate the string and read its value back; I have added some error checking too. Finally, keep in mind that you must free the allocated strings after you use them

int CVICALLBACK DFFilter1 (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 int  line, error = 0;
 char *StrDFFileName = NULL, *StrDFFileDir = NULL;

 if (event != EVENT_COMMIT) return 0;

 errChk (ReadSiteSettings ("Generale","Manuale", &StrDFFileName));
 errChk (ReadSiteSettings ("Generale","Impostazione", &StrDFFileDir));

 // Use the strings

Error:
 // Free the strings if allocated by the function
 if (StrDFFileName) free (StrDFFileName);
 if (StrDFFileDir) free (StrDFFileDir);
 // Warn the operator of an error
 if (error < 0) {
  ...
 }
 return 0;
}

int ReadSiteSettings (char *SectionName, char *ItemName, char **str)

{
 int  error = 0;
 IniText IniFile = 0;

 nullChk (IniFile = Ini_New(0));
 if (Ini_ReadFromFile (IniFile, FCFG) == 0) {
  if (Ini_SectionExists (IniFile, SectionName)) {
   errChk (Ini_GetRawStringCopy (IniFile, SectionName, ItemName, str));
  }
 }

Error:
 // Free the resource
 if (IniFile) Ini_Dispose (IniFile);
 // Pass the error back to the caller
 return error < 0 ? error : 0;
}

As the last note, your functions could be rewritten so that the IniFile is opened only once and all needed parameters are read together, to avoid unnecessary continuous allocation of resources and access to disk.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 16
(4,719 Views)
Now the problem of dynamic memory corrupt occur when free-ing StrDFFileName. Any idea why?
0 Kudos
Message 5 of 16
(4,714 Views)
How do you declared this variable and at which level?


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 16
(4,710 Views)
I declare locally. I am using CVI 8.5 (just for your information).
The Ini_ReadFromFile() for second loop cant be tested yet as the corruption is in first loop at free-ing the variables.
0 Kudos
Message 7 of 16
(4,701 Views)
Hi Popcorn,

Are you still receiving the memory corrupt message?  Would you be able to post a small example and a screenshot of the error?  Based off the code posted by Roberto, I'm not sure why the error is occurring.  Hopefully with you posting an example I will be able to troubleshoot on my end as well.  Thanks!
Matt S
National Instruments
Applications Engineer
0 Kudos
Message 8 of 16
(4,637 Views)
Hi Matt, it seems to me that this question was solved in this other thread. In any case it would be good to have some sort of feedback from popcorn about it.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 9 of 16
(4,631 Views)
yes, this problem is solved in other thread mention by Roberto.
0 Kudos
Message 10 of 16
(4,622 Views)