Solved

Annoying:  Access Violation / Null Reference

Posted on 2004-03-31
7
733 Views
Last Modified: 2007-12-19
Hi Experts,
I have a C# application that calls a DLL written by me in C++.
GPGUI.exe calls gpmain() in simplegpdll.dll with the following line:

if (simplegpdll.gpmain(...,...,...) == 1) {... return true;}


Most of the time it runs with no problems - however, sometimes (and I can't spot a pattern), it crashes with the following error:

   First-chance exception at 0x77f5b2ab in GPGUI.exe: 0xC0000005: Access violation reading location 0x00000034.
   An unhandled exception of type 'System.NullReferenceException' occurred in simplegpdll.dll

   Additional information: Object reference not set to an instance of an object.

The DLL reads a text file, and writes three (once each per operation).  All reads and writes look like this:
FILE *solstream;
solstream=fopen("solution.csv","w+");
fprintf(solstream,"Gen,Best Program\n%d,%d\n", generation, best_prog);
fclose(solstream);
fcloseall();

Any suggestions on what's at 0x00...34 (or how to find out), and also what is throwing the Null Ref?

Thanks in advance, J
0
Comment
Question by:Jazzle
  • 4
  • 3
7 Comments
 
LVL 12

Accepted Solution

by:
vascov earned 100 total points
ID: 10728298
Hi Jazzle,

Compile your C++ DLL with debuging info. That may help you showing more info when an AV occurs.

Regarding your code, check to see that solstream is NOT null after opening it. You might be experiencing issues with file sharing, or something like that.
The NullRef might be a consequence of the other error... (the access violation)

hth

Vasco
0
 
LVL 1

Author Comment

by:Jazzle
ID: 10728376
I have compiled it with the debuging info (it was anyway) but unfortunately it doesn't give me any extra info. :(
also, I know that solstream can't be null because of the way it's created.

I did wonder about file sharing, but i have found found that it gives a different error if the file is in use.

thanks for looking.

J


PS - 'hth'?
0
 
LVL 12

Assisted Solution

by:vascov
vascov earned 100 total points
ID: 10730184
Launch your application using the debugger.

Please, do check if solstream is NOT null anyway. It wont hurt, and it might surprise you the situations that can occur. (i'm not saying with 100% sure that the problem is in this area, but since this is the only code you're showing i definetely recommend for you to test it...)

That exception can also be happening in some marshalling between managed / unmanaged.

hope this helps (hth),

Vasco
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:Jazzle
ID: 10731085
Okay, so I have now found that the exception is caused by the fprintf() command - so i suppose it's refering to the file i want to write to.  all the files written to are closed afterwards - so could it be a matter of threads confusing each other? (I haven't specifed any thread usage)

************* Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at fprintf(_iobuf* , SByte* )
   at gpmain(Int32 POPULATION_SIZ.....
0
 
LVL 1

Author Comment

by:Jazzle
ID: 10731683
You were right it seems the fopen() command is sometimes returning NULL - but I can't see why - any ideas anyone?

J
0
 
LVL 12

Assisted Solution

by:vascov
vascov earned 100 total points
ID: 10731756
Use GetLastError()

VV

0
 
LVL 1

Author Comment

by:Jazzle
ID: 10731905
thanks - will look into GetLastError().
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

20 Experts available now in Live!

Get 1:1 Help Now