14 Replies - 7079 Views - Last Post: 07 November 2012 - 08:19 PM Rate Topic: -----

#1 dwillatberwick   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 07-November 12

Access violation reading location error when class object destructs

Posted 07 November 2012 - 10:10 AM

I am using Visual Studio Express C++ 2010 edition on Windows 7 and I am having trouble trying to debug an Access violation reading location error when a class object, that opens and closes a file, destructs.

I don't know if I am using fstream properly when I want to open a close a file within a class, and use that class to open different files in the main program.

I recieve the following error output when the program crashes:

Quote

First-chance exception at 0x6081aa1e (msvcp100d.dll) in test_fstream_use.exe: 0xC0000005: Access violation reading location 0x00000004.
Unhandled exception at 0x6081aa1e (msvcp100d.dll) in test_fstream_use.exe: 0xC0000005: Access violation reading location 0x00000004.


This is the call stack from the debugger:

Quote

> msvcp100d.dll!std::basic_iostream<char,std::char_traits<char> >::~basic_iostream<char,std::char_traits<char> >() Line 982 + 0x2e bytes C++
test_fstream_use.exe!std::basic_fstream<char,std::char_traits<char> >::~basic_fstream<char,std::char_traits<char> >() Line 1303 + 0x20 bytes C++
test_fstream_use.exe!std::basic_fstream<char,std::char_traits<char> >::`vbase destructor'() + 0x2e bytes C++
test_fstream_use.exe!surf_import::~surf_import() Line 22 + 0x36 bytes C++
test_fstream_use.exe!main() Line 12 + 0x1c bytes C++
test_fstream_use.exe!__tmainCRTStartup() Line 555 + 0x19 bytes C
test_fstream_use.exe!mainCRTStartup() Line 371 C


Following is my code:

stdafx.h
//stdafx.h
#pragma once

#include "targetver.h"

#include <stdio.h>
#include <iostream>
#include "surf_import.h"


test_fstream_use.cpp
//test_fstream_use.cpp
#include "stdafx.h"

int main()
{
	surf_import surfdata;
	surfdata.fname = "test.sur";
	surfdata.import();
	return 0;
}



surf_import.h
//surf_import.h
#ifndef _IMPORT_SURF_H
#define _IMPORT_SURF_H

#include <fstream>
#include <string>

class surf_import
{
private:
	std::fstream sur_file;

public:
	surf_import(void);
	~surf_import(void);
	void import();
	std::string fname;
};

#endif


surf_import.cpp
//surf_import.cpp
#include "StdAfx.h"
#include "surf_import.h"

surf_import::surf_import(void){};

void surf_import::import()
{
	sur_file.open(fname.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
	if (sur_file.is_open())
	{
		std::cout<<"file is open..."<<std::endl;
		/*code if open*/
	}
	sur_file.close();
}

surf_import::~surf_import(void)
{

        std::cout<<"surf import destroyed!"<<std::endl;
}



My console output shows:

Quote

file is open...
surf import destroyed!


I am new to C++ and I am sure there is a simple explanation but I am having trouble searching the web for equivalent issues that pertains to opening and closing files. Advice is greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Access violation reading location error when class object destructs

#2 jimblumberg   User is online

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,062
  • Joined: 25-December 09

Re: Access violation reading location error when class object destructs

Posted 07 November 2012 - 10:18 AM

Without a complete program I will only be able to guess but in the following snippet:
void surf_import::import()
{
	sur_file.open(fname.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
	if (sur_file.is_open())
	{
		std::cout<<"file is open..."<<std::endl;
		/*code if open*/
	}
	sur_file.close();
}


The file close() should be inside the if statement. You only need to close the file if it is open. Also I recommend that you check all the flags, not just the open flag. It is remotely possible that the file opened correctly but can not be used. You can check all the flags like:

if(sur_file)


Also note that in your snippet above you don't actually need to call the close() function. The stream will be properly closed when you exit the function by the stream destructor.


Jim
Was This Post Helpful? 1
  • +
  • -

#3 dwillatberwick   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 07-November 12

Re: Access violation reading location error when class object destructs

Posted 07 November 2012 - 10:56 AM

This is a complete program - I have cut the code down to what is posted while acquiring the same error during debugging to understand how to resolve this error. I will try to use the flag check as you suggest to see if something odd is happening, but in the original code I am able to access, read, and get the values I need from the binary file I intend to use.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Access violation reading location error when class object destructs

Posted 07 November 2012 - 10:59 AM

In fact, if you're only going to use the file on import, I would suggest avoiding making the fstream a member at all and just use a local fstream. If you're also going to do an export function, you do the same there.

EDIT: If there's significantly more to the program, then it's possible you're trashing your memory somewhere, and the effect is not seen until destruction of the object.
Was This Post Helpful? 1
  • +
  • -

#5 dwillatberwick   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 07-November 12

Re: Access violation reading location error when class object destructs

Posted 07 November 2012 - 11:36 AM

My apologies if I am not making myself clear - I am trying to debug the above code as posted and I get the posted error and call stack from the debugger when I compile and run the posted code above.

So I took JackOfAllTrades suggestion of using the fstream object locally and use the flag check suggestion from jimblumberg in my surf_import function declaration. I also changed the input file "text.sur" to be read as a text file that contains the text "read and got the line" to see if I can recieve file inputs into a string variable finput.

//surf_import.h
#ifndef _IMPORT_SURF_H
#define _IMPORT_SURF_H

#include <fstream>
#include <string>

class surf_import
{
private:
	//std::fstream sur_file;

public:
	surf_import(void);
	~surf_import(void);
	void import();
	std::string fname;
	std::string finput;
};

#endif


//surf_import.cpp
#include "StdAfx.h"
#include "surf_import.h"

surf_import::surf_import(void)
{
	std::cout<<"surf import created!"<<std::endl;
};

void surf_import::import()
{
	std::fstream sur_file;
	sur_file.open(fname.c_str(), std::ios::in);
	if (sur_file)//.is_open())
	{
		std::cout<<"file is open..."<<std::endl;
		std::getline(sur_file, finput);
		std::cout<<"This is in the file: "<<finput<<std::endl;
		/*code if open*/

	sur_file.close();

	}

}

surf_import::~surf_import(void)
{
	std::cout<<"surf import destroyed!"<<std::endl;
}



I still get the same same access violation reading location errors:

Quote

First-chance exception at 0x7780aa1e (msvcp100d.dll) in test_fstream_use.exe: 0xC0000005: Access violation reading location 0x00000004.
Unhandled exception at 0x7780aa1e (msvcp100d.dll) in test_fstream_use.exe: 0xC0000005: Access violation reading location 0x00000004.


and get the following call stack from the debugger:

Quote

msvcp100d.dll!std::basic_iostream<char,std::char_traits<char> >::~basic_iostream<char,std::char_traits<char> >() Line 982 + 0x2e bytes C++
test_fstream_use.exe!std::basic_fstream<char,std::char_traits<char> >::~basic_fstream<char,std::char_traits<char> >() Line 1303 + 0x20 bytes C++
test_fstream_use.exe!std::basic_fstream<char,std::char_traits<char> >::`vbase destructor'() + 0x2e bytes C++
> test_fstream_use.exe!surf_import::import() Line 25 + 0x12 bytes C++
test_fstream_use.exe!main() Line 10 C++
test_fstream_use.exe!__tmainCRTStartup() Line 555 + 0x19 bytes C
test_fstream_use.exe!mainCRTStartup() Line 371 C


but my console now shows that I don't reach the surf_import destructor this time:

Quote

surf import created!
file is open...
This is in the file: read and got the line


Thank you for the advice given so far. I welcome any more advice to point me in the right direction.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is online

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,062
  • Joined: 25-December 09

Re: Access violation reading location error when class object destructs

Posted 07 November 2012 - 11:43 AM

Have you run this program through your debugger? The debugger should tell you exactly where the problem was detected. Then you can view the variables at the time of the crash. Since you have not posted a complete program that illustrates your problem you will need to debug the program and tell us where the problem is detected.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Access violation reading location error when class object destructs

Posted 07 November 2012 - 11:45 AM

Ah, you're also over here as well.

It's happening still in the destruction of the stream object.
Was This Post Helpful? 0
  • +
  • -

#8 dwillatberwick   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 07-November 12

Re: Access violation reading location error when class object destructs

Posted 07 November 2012 - 11:52 AM

Not sure if there is a delay in replies - but my debugger (VS 2010 Express) indicates it is waiting to return from calls through fstream and iostream objects. I can post all the content the sur_file has on the local variable and the "this" variables of the fstream and iostream objects if that would be useful. Again - the above code as posted is the complete program I am trying to debug.

View PostJackOfAllTrades, on 07 November 2012 - 11:45 AM, said:

Ah, you're also over here as well.

It's happening still in the destruction of the stream object.


Yes - but it seems I am getting more assistance here, while the cplusplus forum congratulates each other on non-solutions. Sorry to be a pain.
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Access violation reading location error when class object destructs

Posted 07 November 2012 - 11:53 AM

Not a problem.

Try removing the .close() call altogether, just let the fstream object go out of scope when leaving the function. I doubt it will work, but where the object is local the .close() call is now unnecessary anyway.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg   User is online

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,062
  • Joined: 25-December 09

Re: Access violation reading location error when class object destructs

Posted 07 November 2012 - 11:55 AM

The above code is not a complete program. A complete program must have a main() that should create instances of your class and try to actually use the class.

If the debugger stops waiting for something you need to put a break point somewhere before that point so you can single step through the problem section viewing the variables as you proceed.

Jim
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Access violation reading location error when class object destructs

Posted 07 November 2012 - 11:56 AM

jimblumberg, my assumption is that main is pretty much as it was in the original post.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg   User is online

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,062
  • Joined: 25-December 09

Re: Access violation reading location error when class object destructs

Posted 07 November 2012 - 12:18 PM

Okay, I added the main() that I missed and the program ran correctly for me. However I do notice:
	surf_import(void);
	~surf_import(void);

Your compiler may be pickier than mine (g++) but your destructor should not have the void. The destructor has no return type and no arguments, not even void.

Jim
Was This Post Helpful? 1
  • +
  • -

#13 dwillatberwick   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 07-November 12

Re: Access violation reading location error when class object destructs

Posted 07 November 2012 - 07:14 PM

After jimblumberg's reply I installed MinGW/g++ and Code::Blocks and this code compiles and runs fine too on my computer. I would prefer to use a Windows compliler for portability to other Windows-based machines, so I will look further into why VS 2010 Express is failing this simple code.
Thanks for your time looking into this.
Was This Post Helpful? 0
  • +
  • -

#14 dwillatberwick   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 07-November 12

Re: Access violation reading location error when class object destructs

Posted 07 November 2012 - 07:49 PM

So I deleted the default property sheets used for VS 2010 Express because I had recently explored the use of gtkmm and restarted VS 2010 Express. I copy pasted the codes into a new project and now the code compiles and runs fines without any access violation reading location errors.

The gtkmm installation (following the procedure on gtkmm) required some different build properties that gtkmm makes available to import. I thought the changes would only be for that project, but it seems to have had over-written the default propert sheets and maybe whatever changes made there was interfering with proper compilation.

I will see if I can duplicate this again by re-importing the gtkmm property sheets, and if it happens again I'll post something to gtkmm.
Was This Post Helpful? 0
  • +
  • -

#15 dwillatberwick   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 07-November 12

Re: Access violation reading location error when class object destructs

Posted 07 November 2012 - 08:19 PM

Apparently the istream incompatibility with gtkmm is/was a known bug:

http://mcdougalljona...m-crash-in.html
and
https://mail.gnome.o...e/msg00013.html

Still confused why the installation procedure for gtkmm changed the default property settings rather than keeping the property changes within the project.
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1