BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

ofstream access violation

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Fraser Ross
Guest





PostPosted: Thu Dec 21, 2006 9:08 pm    Post subject: ofstream access violation Reply with quote



I have a function in a DLL module that I've reduced to simply creating a
std::ofstream object with the default constructor. When the calling
module shuts down there is an access violation. I can't get rid of the
access violation if the DLL creates a ofstream. It is at the last line
of the Windows Delphi file.

There have been many messages about similar problems with ofstream over
a few years. Is there any bug fixes for Turbo Explorer?

When I make a catch handler body empty the integrated debugger acts like
it isn't there. Thats not a big problem but it is strange.

Fraser.
Back to top
Darko Miletic
Guest





PostPosted: Thu Dec 21, 2006 11:24 pm    Post subject: Re: ofstream access violation Reply with quote



Fraser Ross wrote:
Quote:
I have a function in a DLL module that I've reduced to simply creating a
std::ofstream object with the default constructor. When the calling
module shuts down there is an access violation. I can't get rid of the
access violation if the DLL creates a ofstream. It is at the last line
of the Windows Delphi file.

You need to post some code and explan what it is that you do - but exactly.

Darko
Back to top
Fraser Ross
Guest





PostPosted: Fri Dec 22, 2006 9:59 pm    Post subject: Re: ofstream access violation Reply with quote



As Remy said not long ago the DLL will be mapped into my EXEs address
space because I use LoadLibrary. That is why I get an access violation
in the EXE. Calling the default constructor of std::ofstream is only
ordinary code that I would not expect a problem with. I have tried
using the free store instead of the stack which made no difference.

Fraser.
Back to top
Fraser Ross
Guest





PostPosted: Tue Jan 02, 2007 8:37 pm    Post subject: Re: ofstream access violation Reply with quote

"Darko Miletic"
Quote:
Fraser Ross wrote:
I have a function in a DLL module that I've reduced to simply
creating a
std::ofstream object with the default constructor. When the calling
module shuts down there is an access violation. I can't get rid of
the
access violation if the DLL creates a ofstream. It is at the last
line
of the Windows Delphi file.

You need to post some code and explan what it is that you do - but
exactly.

Darko

It was not a problem with code. I am not seeing the problem now and I
think it is because I am not using Dynamic RTL with my EXE project, my
DLL project and 2 LIB projects that are linked. The LIB projects I
particularly suspect need to have the use of Dynamic RTL in sync. with
those they are linked with.

Fraser.
Back to top
Eike Petersen
Guest





PostPosted: Thu Jan 04, 2007 7:20 am    Post subject: ofstream access violation (TeamB to the rescue?) Reply with quote

Hi,

a long time ago I ran into a problem with the use of "atexit" in conjunction
with dynamically loading *and* freeing a DLL at runtime.

Recently I ran into a similar problem - again in conjunction with dynamically
loading and freeing a DLL. Just like in your case after using the streaming
facilities of the standard library and freeing the DLL at runtime, the program
crashes at program termination *after* leaving WinMain().

I know there is an error with "atexit" in the Borland implementation of the
C-Library which still is not fixed (see my old thread). It's quite possible the
current problems have a similar cause.

Old thread:

http://groups.google.de/group/borland.public.cppbuilder.language/browse_frm/thread/460b7e2b16137dc/0e5f32b8e1aeb028?lnk=st&q=&rnum=1&hl=de#0e5f32b8e1aeb028

It seems that some programmer at Borland tried to do something about the
problem, but after inspecting the source code of the RTL I'm under the
impression he didn't get to finish his job.

In source "startupd.c" you will find several references to a mysterious variable
called "_multidll". I didn't quite figure out how exactly the mechanism is
supposed to work, but I'm quite sure it doesn't work.

In line 159 the code reads something like this:

*** SNIP ***
if (_multidll && (share__dll_table = _create_shmem()) != NULL &&
share__dll_table->table[share__dll_table->ntables] !=
(MODULE_DATA *)-1)
{
share__dll_table->table[share__dll_table->ntables++] = mod_table;
}
else
{

_multidll = 0; /* set a flag that will force the
termination routine to call all of
the exit procs.
*/

/* Call all of the _INIT_ functions.
*/
_init_exit_proc(&_dll_table, 0);
}

*** SNAP ***


To my very surprise the implementation of the function '_create_shmem()' looks
like this:

*** SNIP **

MULTI_INIT *_create_shmem(void)
{
return NULL;
}

*** SNAP ***

When the program shuts down the following code from source 'startupd.c' starting
at line 229 will execute:

*** SNIP
/* The following loop calls the DLL's exit procedures.
* This is also done by _cleanup(), if the application exits
* with exit(); in that case, we won't do anything here, because
* all the exit procedures will be marked as "called". But if the
* application exits some other way, we must call the exit
* procedures here.
*/
if (!_multidll)
{

/* First call all entries in the atexit table for this DLL.
*/
if (__pCallAtExitProcs)
__pCallAtExitProcs();

/* Then call all of the _EXIT_ functions.
*/
_init_exit_proc(&_dll_table, 1);

#if defined(__MT__)
#if 0 /* Adding these two lines fixes a rare CG RTL memory leak.
It's commented out since it seems to adversly affect
other parts of the stream locking system.
*/
_cleanup_handle_locks();
_cleanup_stream_locks();
#endif /* 0 */
#endif /* __MT__ */

}
*** SNAP ***

As you can see the magic variable '_multidll' seems to be used here again -
however it will always be zero because '_create_shmem' will always return NULL
and therefore '_multidll' will always be reset to zero in the else branch of the
initialization quoted above.

Maybe I'm all on the wrong track, but something is definitely odd here. Maybe
the code for destroying certain static objects *within the RTL* gets called
twice? I'm not sure. The last time I spent over 8 hours debugging the RTL, but I
had to turn my attention back to my real project.

Best Regards
Eike Petersen

Fraser Ross wrote:
Quote:
I have a function in a DLL module that I've reduced to simply creating a
std::ofstream object with the default constructor. When the calling
module shuts down there is an access violation. I can't get rid of the
access violation if the DLL creates a ofstream. It is at the last line
of the Windows Delphi file.

There have been many messages about similar problems with ofstream over
a few years. Is there any bug fixes for Turbo Explorer?

When I make a catch handler body empty the integrated debugger acts like
it isn't there. Thats not a big problem but it is strange.

Fraser.
Back to top
David Dean [CodeGear]
Guest





PostPosted: Thu Jan 04, 2007 9:10 am    Post subject: Re: ofstream access violation (TeamB to the rescue?) Reply with quote

In article <459c5654$1 (AT) newsgroups (DOT) borland.com>,
Eike Petersen <epetersen-spam-protection (AT) gmx (DOT) net> wrote:

Quote:
I know there is an error with "atexit" in the Borland implementation of the
C-Library which still is not fixed (see my old thread). It's quite possible
the
current problems have a similar cause.

I'll do some digging to see if this original report was ever tracked
in the internal system, but it was long before I became a CodeGear
employee. These reports looks like they cover the issue:

<http://qc.codegear.com/wc/qcmain.aspx?d=35585>
<http://qc.codegear.com/wc/qcmain.aspx?d=3859>

I'd like to track the new issue you note. Would you please enter a
report into QC about this issue?

--
-David Dean
CodeGear C++ QA Engineer
Back to top
Fraser Ross
Guest





PostPosted: Thu Jan 04, 2007 10:38 pm    Post subject: Re: ofstream access violation Reply with quote

Does anyone agree that the of the dynamic RTL must be on or off
identically with all my projects? With my DLL project which has 2 LIB
projects linked I would expect its necessary there. When I compile my
EXE and DLL in debug mode it might be necessary. When I compile those
in release mode it is probably not necessary.

Fraser.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.