Re: Access Violation using CCriticalSection in DLL
I do not normally mix debug and release components, but I was trying to find
the source of the problem by using a debugger on at least the DLL
components.. My latest discoveries:
1) when I run the S/W in debug mode (all components) , it works.
2) When I run the same S/W, except compiled for release, it fails.
3) when I run the same program. compiled for release, on win2K, it works. I
am not recompiling this program on Win2K, I am simply copying the same
program from win XP and executing it. I have tried using the same copy of
MFC42.dll in both cases in case this was a Problem (it wasn't).
This S/W is many years old, and it has suddenly stopped running. Are there
changes made to WInXP that may be a factor??
The questions I am trying to get answered are:
1) There is obviously a difference between the way a debug compile and a
release compile work on Win XP. What is this difference?
2) Why does the release version work on Win2K and not on Win XP?
"Joseph M. Newcomer" <newcomer@flounder.com> wrote in message
news:prij13p4k3rorcre462hts3prscq858bp4@4ax.com...
See below...
On Sat, 7 Apr 2007 11:12:18 -0600, "Brian Westcott" <bwestcott@shaw.ca>
wrote:
Sorry for the mangled description. The logger is actually a class that I
instantiate outside of the scope of my main program. CCriticalSection is
declared within the logger class. If I comment out calls to the logger in
the DLL, then the logger works OK once I return from the DLL, so lifetime
does not appear to be an issue.
I also am testing this by sending a very short message to the logger from
the DLL to ensure that I am not overrunning the Logger's buffer.
I have compiled evrything, including the DLL, with debug mode and
everything works when I run a debug version. When I execute the debug mode
without starting the debugger, it also works.
If I run the release mode of the main program with the debug version of
the
DLL,
*****
You can stop right there. this won't work, and any problems you encounter
should be
expected. You cannot mix-and-match debug and release components like
this.
****
then the program fails with an access violation in the call to Lock().
I get a beak at the point in the dbugger and the stack trace is:
NTDLL!7c918fea()
NTDLL!7c90104b()
Logger::EnterLogCriticalSection(), which is declared as:
void EnterLogCriticalSection() {ccs.Lock();}
ccs is the instantiated CCriticalSection in the Logger class.
If I take a look at ccs, it appears normal, except that
ccs.m_sect.OwningThread = NULL;
Is there any other info I can give that might help?
"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:v5hf13hpiv3vckd6mcengdv9rvib06p9qp@4ax.com...
On Sat, 7 Apr 2007 09:27:56 -0600, "Brian Westcott" <bwestcott@shaw.ca>
wrote:
I have a log program that uses a CRiticalSection to synchronize writes
to
the log file. It works OK in most of my programs. When I Load a DLL
(That
I
wrote) , I pass the address of the log program by reference to the DLL.
When
I call the log program from within the DLL, I get an access violation
when
the log program calls CCriticalSection::Lock after formatting the data
to
be
logged. When I compile and test all programs in Debug mode, the log
program
works OK.
I am declaring the logger outside of the scope of my main program so
presumably that makes it global?
Can anyone tell me what is going on?
I think you must be using the word "program" when you mean "function",
and
this makes your message a little confusing. Where are you declaring the
CCriticalSection? If its lifetime has ended by the time the DLL calls
it,
you will have problems. That said, if the rest of your program can use
it
successfully at the same time the DLL has problems, that would rule out
a
lifetime issue. After verifying the CCriticalSection should still be
valid
at the time the DLL uses it, and double-checking that "formatting the
data"
isn't causing any buffer overflows, I'd compile the release mode version
with debug info and see if I could spot anything wrong under the
debugger.
--
Doug Harrison
Visual C++ MVP
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm