Worker Thread and MDI app - Release Build dies but Debug Build OK 
Author Message
 Worker Thread and MDI app - Release Build dies but Debug Build OK

I have a situation where the my MDI app throws an exception when running
the Release Build only,

specifically: Unhandled Exception in app,exe 0xC0000005: Access

that occurrs when I "select" an item from a populated CTreeView view.  I
experience no indication of problems when running the DEBUG version of
the program, and I have tried changing my only Release Linking
optimizaiton (Maximize Speed) to No Optimizations, with no change in

the high-level description of the problem is as follows:

MDI app, where each document has a single childframe support THREE VIEWS
via nested splitters.  pane0=TreeView, pane1=CScrollView,
pane2=ListView..  The TreeView view tells the other views what to
display based on the selected node in its TreeCtrl (calls view methods
by using pointers to other views centrally stored in the document).  the
document maintains a pointer to a dataset Object that has a dataset
composed of a couple of user-defined tree-type collections as well as a
large MAP (using STL) of pointers to data objects...  All views
ultimately refer to the dataset objects by accessing this particular
pointer in the document (let's call it m_pUpdater).

what worked:
from within the main thread, letting the document build a valid dataset
off of its m_pUpdater pointer from its OnNewDocument or OnOpenDocument
events, then calling a method in it's associated TreeView class to
populate that view.  the treeview, in turn, called methods in the other
two views telling them what to paint/draw based on the selected item in
its treectrl collection (which would be the root item at the onset).

the problem:
the m_pUpdater pointer in my document is a pointer to an object that is
populated with data by making requests to a DCE server on a network
host...  the dataset is quite large, and takes some time to load across
the network...  since we support multiple documents, i did not want to
cripple my application's support of an existing document by hogging all
resources for loading another document - and i certainly did not want to
cripple the UI of the entire system while my program sat waiting on the
DCE data-load process to finish...   so:

the "solution" that caused my current problem:
i tried to create a worker thread that would, on request from my
document class, create a pointer to a "new" Updater object, register it
with DCE and all that, refreshing its dataset... then notify the
document that it was finished.  Once the document was notifed, it would
set its Updater object pointer to that of the thread's Updater object
pointer, then set the thread's pointer to NULL.. leaving the thread
alive but doing a WaitForSingleObject(StartTheUpdate-Event, INFINITE)...
for when the user initiates a "refresh" of the document's dataset.

i can step through the process while in Debug mode, and see no errors or
conditions that would cause errors... my views wait happily until they
are told to populate themselves (either by the document, or by the
navigator TreeView view), and once the document is signaled to look for
a proper dataset, it tells the treeview to populate itself, and things
move on as expected... ie. i can select items from the tree, and it
calls methods in the other views by accessing view-pointers in the
document...  all is well...

in release mode, the thread executes, and the document gets a dataset,
and it succesfully tells the TreeView to populate itself based on the
document's newly aquired dataset - but when i "select" a treeitem, the
program blows up...  apparently at this line from within the treeview's
OnSelChange event:
  CTreeItem *pSelected =
(pDoc is determined InLine during release execution, very common
method..  m_pUpdater is the pointer to the doc's dataset, and m_pLVMTree
is a tree-type collection maintained within the dataset.  FindTreeItem
returns a CTreeItem based on the supplied hNode, which is just a long
that I keep in the CTreeCtrl itemData variable to help tie the
TreeCtrl's collection back to our internal "tree-type collection".....)

that's pretty much the explanation without supplying the actual code for
the worker thread, the global function used in the worker thread, or the
structure used when passing data into the worker thread...   the
notification process is pretty simple: the thread starts up on creation,
and listens for events set or reset from within the document class that
tell it to StartUpdate or AbortUpdate, etc... , then Posts a message to
a document-supplied View pointer when it's finished.  The view, in turn,
calls a method in the document, which takes a look at the info structure
passed to the thread, and gets to the dataset from a pointer there...

any ideas on this?  i am using no synchronization or locking mechanisms
to control access to the document, but i don't think that really applies
in this situation....  in this example, the treeview uses the same
pDoc->m_pUpdater->m_pLVMRoot object when populating its control, which
is does succesfully, so i am at a loss as to why i have this exception
in the OnSelChanged event....   very frustrating...

i've posted a message under a similar title in the MFC.DocView newsgroup
that has some code for the thread, info class, and global function, if
it would be helpfull.

/as always, thanks for taking the time to hear me out.

Michael Peters
Systems Integrator
Stack Computer, Inc.
Newport Beach, CA

Mon, 31 Jul 2000 03:00:00 GMT  
 Worker Thread and MDI app - Release Build dies but Debug Build OK

Fri, 19 Jun 1992 00:00:00 GMT  
 Worker Thread and MDI app - Release Build dies but Debug Build OK

was: Worker Thread in MDI App: Release Build Dies

I have narrowed down the problem some what.  here is a "simpler"
description of the problem:

any of my view classes, created in the "main" thread from a document
template, seem to "lose" the ability to see their document if i use
their m_pHwnd value in a ::PostMessage call from a worker thread.

short hx of the problem:
i have an MDI application with a single document template.  within the
OnNewDocument -or- OnOpenDocument functions I start a worker thread that
builds a data collection from a network DCE source.  i pass the thread a
parameter structure that  includes an m_hWnd from the document's 1st
view to use when communicating back to the main thread.. (use
pos=GetFirstView, GetNextView(pos) to return a CView*, then access the
m_pHwnd variable).

life goes on as expected.. document-template views are created, albeit
blank, within a splitter frame.

the worker thread informs the document that it has retrieved a complete
data collection by doing a ::PostMessage() to the hWnd I pass to it in
the parameter structure.  that view, in turn, intercepts my user-defined
msg, and calls a member-function in its document (using GetDocument())
to "tell" the document that the data is ready.  the document does some
manipulation of the data, then updates the views to display the newly
aquired data...

the view that intercepted the user-message from the worker thread always
throws an exception in Release Build when responding to
"user-interaction"... in other words, it displays data from the document
properly by using a GetDocument() pointer to reach the document's
data... it does NOT get a proper  handle from GetDocument() moments
later when user interaction, such as clicking a TreeView item -causing
an OnSelChanging event, or right-mouse clicking -causing an
OnContextMenu event occurrs.

by creating debug info in my release build, and enabling profiling, i
have been able to observe that when the exception occurrs, i am able to
see values for worker-thread variables i happened to have listed in my
watch window... and pDoc, or GetDocument() was non-existant at that
point (explaining the NULL return value, and the exception)...

WHY would I suddenly be back in the context of the Worker Thread when i
interact with the views that i watch properly paint themselves based on
data they just accessed by using a properly obtained pointer to  their
document only moments before... ?

/very confused

Mon, 07 Aug 2000 03:00:00 GMT  
 [ 3 post ] 

 Relevant Pages 

1. Debug build breaks, Release build ok

2. Debug build breaks, Release build ok.

3. Worker Thread in MDI App... Release Build Dies.

4. Debug build ok, release - unresolved external _main

5. Release build OK / Debug version crashes /ODBC /dbcore.cpp

6. STRCPY does not work in debug, Ok in release build

7. Debug build fine, release crashes unless we link with debug Multi-Threaded library

8. Release build OK / Debug version crashes /ODBC /dbcore.cpp

9. Debug Build vs. Release Build

10. Debug Build works Release build doesn't?

11. Protection Violation in Release Build but not in Debug Build

12. Debug build works correctly but release build doesnt (sometimes)


Powered by phpBB® Forum Software