Solved

ifstream::getline exception???? help please!!!

Posted on 2004-05-02
4
1,306 Views
Last Modified: 2007-12-19
My application is running the difficult part, but what should be the easy part is blowing up in my face
I have a java GUI using JNI to call a cpp .dll file. I tried to pass an object to the native method, this gave me all sorts of problems so I decided to csrap that, now the app just calls the native method and stores the object in a text file, then the cpp method opens the text file and is supposed to read just 6 pieces of string and work on from there. However when I run the program it throws an exception claiming there is a problem with the getline function in the dll.

the java code looks like so:
******************************************************

public void Login(){
                MachineDetails dClass = new MachineDetails();//object storing the details
      
      dClass.BSTRMachineName = jTextField1.getText();     //
      dClass.UserName          = jTextField2.getText();         //  These are strings
      dClass.PassWd          = jPasswordField1.getText(); //

      dClass.AvailMemory     = jCheckBox1.isSelected();      //
      dClass.LoadPercentage  = jCheckBox2.isSelected();    //   These are boolean
      dClass.DiskSpaceFree   = jCheckBox3.isSelected();     //

      FileOutputStream out;
      PrintStream p;
      try{
            out = new FileOutputStream("machineDetails.dat");
            p = new PrintStream( out );

            p.println (dClass.BSTRMachineName);
            p.println (dClass.UserName);
            p.println (dClass.PassWd);
            p.println (dClass.AvailMemory);
            p.println (dClass.DiskSpaceFree);
            p.println (dClass.LoadPercentage);

            p.close();
      }catch(Exception e)
      {
            System.err.println ("Error writing to file");
      }

      driver(); //call the native method.
}
***********************************************************
This compiles fine and the .dat file displays what it is supposed to so I dont think there is any problem with the java
Here is the native method source code which starts up fine but crashes when it tries to access the data in the .dat file.
************************************************************

JNIEXPORT void JNICALL Java_guineyd_java_ProgramStartup_driver
  (JNIEnv * env, jobject obj)
{
      printf("We have lift off");      //just to show that JNI is working ok.
      ..............
                ..............
      
      char* javaMcName = NULL;
      char* javaUserName = NULL;
      char* javaPassWd = NULL;
      char* javaAvailMemory = NULL;
      char* javaDiskSpaceFree = NULL;
      char* javaLoadPercentage = NULL;

      ifstream input("machineDetails.dat");
      if (input.fail()){
            printf("Cannot open machinDetails.dat");
            exit(1);
                }
 
                printf("data file opened ok\n");  //This proves that the data file opened

      input.getline(javaMcName, 100, '\n');//, 1024, '\n');
      input.getline(javaUserName, 100, '\n');
      input.getline(javaPassWd, 100, '\n');
      input.getline(javaAvailMemory, 6, '\n');
      input.getline(javaDiskSpaceFree, 6, '\n');
      input.getline(javaLoadPercentage, 6, '\n');
      input.close();

                .....................//work on with the data ..........
                .....................
}
************************************************************
I have two printf statements to show how far the program actually goes.
This is what I get when I run the app.

************************************************************

We have lift off         //Here you can see the two printf statements working properly.
data file opened ok   //

An unexpected exception has been detected in native code outside the VM.
Unexpected Signal : EXCEPTION_ACCESS_VIOLATION (0xc0000005) occurred at PC=0x208
BE5F0
Function=std::basic_istream<char,std::char_traits<char> >::getline+0x1B0   //function is getline?????
Library=C:\WINNT\system32\MSVCP71D.dll

Current Java thread:
        at guineyd.java.ProgramStartup.driver(Native Method)
        at guineyd.java.ProgramStartup.Login(ProgramStartup.java:237)
        at guineyd.java.ProgramStartup$Window_actionAdapter.actionPerformed(Prog
ramStartup.java:159)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:17
86)
        at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Abstra
ctButton.java:1839)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:420)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258
)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:245)
        at java.awt.Component.processMouseEvent(Component.java:5100)
        at java.awt.Component.processEvent(Component.java:4897)
        at java.awt.Container.processEvent(Container.java:1569)
        at java.awt.Component.dispatchEventImpl(Component.java:3615)
        at java.awt.Container.dispatchEventImpl(Container.java:1627)
        at java.awt.Component.dispatchEvent(Component.java:3477)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483
)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)

        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
        at java.awt.Container.dispatchEventImpl(Container.java:1613)
        at java.awt.Window.dispatchEventImpl(Window.java:1606)
        at java.awt.Component.dispatchEvent(Component.java:3477)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:201)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

Dynamic libraries:
0x00400000 - 0x00406000         C:\JBuilderX\jdk1.4\bin\java.exe
0x77F80000 - 0x77FFD000         C:\WINNT\system32\ntdll.dll
0x7C2D0000 - 0x7C332000         C:\WINNT\system32\ADVAPI32.dll
0x7C570000 - 0x7C628000         C:\WINNT\system32\KERNEL32.DLL
0x77D30000 - 0x77DA1000         C:\WINNT\system32\RPCRT4.DLL
0x78000000 - 0x78045000         C:\WINNT\system32\MSVCRT.dll
0x08000000 - 0x08136000         C:\JBuilderX\jdk1.4\jre\bin\client\jvm.dll
0x77E10000 - 0x77E75000         C:\WINNT\system32\USER32.dll
0x77F40000 - 0x77F7E000         C:\WINNT\system32\GDI32.DLL
0x77570000 - 0x775A0000         C:\WINNT\system32\WINMM.dll
0x10000000 - 0x10007000         C:\JBuilderX\jdk1.4\jre\bin\hpi.dll
0x007C0000 - 0x007CE000         C:\JBuilderX\jdk1.4\jre\bin\verify.dll
0x007D0000 - 0x007E8000         C:\JBuilderX\jdk1.4\jre\bin\java.dll
0x007F0000 - 0x007FD000         C:\JBuilderX\jdk1.4\jre\bin\zip.dll
0x18270000 - 0x1837F000         C:\JBuilderX\jdk1.4\jre\bin\awt.dll
0x77800000 - 0x7781E000         C:\WINNT\system32\WINSPOOL.DRV
0x76620000 - 0x76630000         C:\WINNT\system32\MPR.DLL
0x75E60000 - 0x75E7A000         C:\WINNT\system32\IMM32.dll
0x77A50000 - 0x77B3F000         C:\WINNT\system32\ole32.dll
0x18380000 - 0x183D0000         C:\JBuilderX\jdk1.4\jre\bin\fontmanager.dll
0x51000000 - 0x5104B000         C:\WINNT\system32\ddraw.dll
0x728A0000 - 0x728A6000         C:\WINNT\system32\DCIMAN32.dll
0x5C000000 - 0x5C0C7000         C:\WINNT\system32\D3DIM700.DLL
0x20860000 - 0x20890000         C:\COLLEGE\attempt\cpp\guineyd\guineyd.dll
0x779B0000 - 0x77A4B000         C:\WINNT\system32\OLEAUT32.dll
0x20890000 - 0x2094C000         C:\WINNT\system32\MSVCP71D.dll
0x20950000 - 0x209D7000         C:\WINNT\system32\MSVCR71D.dll
0x77920000 - 0x77943000         C:\WINNT\system32\imagehlp.dll
0x72A00000 - 0x72A2D000         C:\WINNT\system32\DBGHELP.dll
0x690A0000 - 0x690AB000         C:\WINNT\system32\PSAPI.DLL

Heap at VM Abort:
Heap
 def new generation   total 576K, used 81K [0x10010000, 0x100b0000, 0x104f0000)
  eden space 512K,   3% used [0x10010000, 0x10014420, 0x10090000)
  from space 64K, 100% used [0x10090000, 0x100a0000, 0x100a0000)
  to   space 64K,   0% used [0x100a0000, 0x100a0000, 0x100b0000)
 tenured generation   total 1408K, used 277K [0x104f0000, 0x10650000, 0x14010000
)
   the space 1408K,  19% used [0x104f0000, 0x105355f8, 0x10535600, 0x10650000)
 compacting perm gen  total 4352K, used 4226K [0x14010000, 0x14450000, 0x1801000
0)
   the space 4352K,  97% used [0x14010000, 0x14430890, 0x14430a00, 0x14450000)

Local Time = Mon May 03 00:35:37 2004
Elapsed Time = 5
#
# The exception above was detected in native code outside the VM
#
# Java VM: Java HotSpot(TM) Client VM (1.4.2_01-b06 mixed mode)
#
# An error report file has been saved as hs_err_pid2248.log.
# Please refer to the file for further information.
#
Press any key to continue . . .

****************************************************************

I know its a long load of code to go through but Im realy desperate, I have to have thios thing working very soon, please.

0
Comment
Question by:vassim
  • 3
4 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 10974001
For what it's worth, 0xc0000005 is an access denied.

I presume it happened on the first getline()?
0
 

Author Comment

by:vassim
ID: 10974019
Im not sure, I presume so, I tried making all the attributes in the MachineDetails class in the java as public but it didnt work.

Why would it deny me access when its just reading a text file????
0
 
LVL 22

Accepted Solution

by:
cookre earned 500 total points
ID: 10974201
javaMcName is typed as a pointer to a string and initialized to NULL.

The getline() then tries to store its input string to location NULL.

Oops.
0
 
LVL 22

Expert Comment

by:cookre
ID: 10974206
You should either

char javaMcName[someappropriateamount];

or, with its current declaration as a char *

javaMcName=(char *)malloc(someappropriateamount);
...
free(javaMcName);

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
 shows up in Outlook, not OWA or on phone 3 58
VB 6.0 printer how to align 6 49
Path of Workbook 3 45
Not needed 13 58
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now