Solved

access violation when writing ini file

Posted on 2004-08-08
4
330 Views
Last Modified: 2010-04-05
I have been working on this for most of the day.  its driving me nuts.    I have a series of textfields that are filled in from an ini file.    Here is all relevent code:

to read the information

procedure TForm1.Settings2Click(Sender: TObject);
begin
settingsini := TIniFile.Create('.\settings.ini');
       try

       ftphost := settingsini.readstring('Server Settings','host', 'none' );
       ftpusername := settingsini.readstring('Server Settings','username', 'none');
       ftppassword := settingsini.readstring('Server Settings','password', 'none');
       ftppath := settingsini.readstring('Server Settings','path', 'none');

       frmsettings.txtFtpHost.text := ftphost;
       frmsettings.txtFtpusername.text := ftpusername;
       frmsettings.txtFtppassword.text := ftppassword;
       frmsettings.txtFtpPath.text := ftppath;
       except
      showmessage('Settings.ini - problem reading inifile');
       end;                                                            
       free;




frmsettings.Show;
end;







to write it back to file when "save" button is clicked

procedure Tfrmsettings.PDJXPButton1Click(Sender: TObject);
begin

settingsini := TIniFile.Create('.\settings.ini');
  with settingsini do
      begin
    try
          writestring('Server Settings','host', frmsettings.txtFtpHost.text);
          writestring('Server Settings','username', frmsettings.txtFtpusername.text);
          writestring('Server Settings','password', frmsettings.txtFtppassword.text);
          writestring('Server Settings','path', frmsettings.txtFtpPath.text);
     except
     showmessage('there was a problem writing the file');
     end;
        free;
      end;
      frmsettings.hide;
end;



/////////////////////////////////////end code

I have no problem READING the file.  When I try to WRITE the file again I run into an access violation.   I dont know why this happens.   I have tried a number of things many of them obviously incorrect even to me but still have not had luck.  I tried removing the second tinifile.create (the writing one) based on the idea that it had already been done.  This neither fixed the problem nor made it worse.

Anyone have any ideas? I'm always on a deadline but this one is rather worse than usual.




0
Comment
Question by:hibbidiji
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
LRHGuy earned 300 total points
ID: 11748872
It would be helpful, if you know it, to reveal at what point the error pops up? (which line is the culprit?)

I tried your code, and it worked fine here. The only change I made was:
 
in settings2clisk, change the Free to settingsini.free

I didn't try the creating the input fields, but I doubt if that's where the error lies.
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11748889
I tried it again, and making it settingsini.free in settings2click should fix it. I put it back the way you had it, and forced it to read, then call the write routine, and I got your error message.

Hope that helps!
0
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 200 total points
ID: 11749055
What you're doing is basically plain stupid.

Sorry but lines like:
* writestring('Server Settings','host', frmsettings.txtFtpHost.text);
Just don't make sense if you define them in a class named Tfrmsettings. Chances are that the form just doesn't even know the right value for frmsettings. Try using
* writestring('Server Settings','host', txtFtpHost.text);
instead... The difference is that at the moment you call PDJXPButton1Click, the variable frmsettings is probably unassigned. If not, the value frmsettings might be pointing at the wrong form. And that's what creates your access violation...

Just an example of erroneous copying&pasting code, I guess. :-)
0
 
LVL 1

Author Comment

by:hibbidiji
ID: 11750029
The program was crashing because of both problems.   I gave the extra 100 to the first answer on the buzzer :)   Thanks guys and alex, dont apologize for my MISERABLE coding. I'm a php guy and new to delphi.  Its just my inexperiance talking...  :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question