Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using COM object in MDI - Memory collapse?

Status
Not open for further replies.

tofuTnT

Technical User
Jul 17, 2003
67
US
I created a MDI and added two child forms. Each of the child form had a private member,
Code:
private MODI.Document modiDoc;
when I clicked on a button, a BackgroundWorker will start and use the modiDoc to do some processing.

Everything worked fine if I had only child form. However, if opened two and ran them simultaneously, I would get an exception thrown from the COM object.
Code:
Error Code: -959966917
Error Message: bad shapepacks
And later, the main form would throw another exception
Code:
(-2147417851): RPC_E_SERVERFAULT (The server threw an exception.)
My guess was that only one instance of the modiDoc was created instead of two for each of the child forms. While two process tried to use the same object to do different things, it just collapsed.

Anyone has any idea how to fix the problem?

Thanks!
tofuTnT
 
It sounds like the COM object is single-threaded, so when you accessed it twice from different threads, it had problems.

Possible solutions:
1. Rewrite COM object to use the MTA
2. Queue up access to the COM object so that subsequent requests have to wait for the current request to finish

Another possibility is the COM object is trying to write to the screen from your worker thread, which is a big no-no. The answer here is to make sure all screen updates (changing the value of a textbox, label, grid, etc) go through the BeginInvoke pattern.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
It sounds like the COM object is single-threaded, so when you accessed it twice from different threads, it had problems.
That is what I don't understand about C#. I created one instance of the COM object in each of the BackgroundWorker and not expecting them to interference with each other...
Besides, the COM object doesn't update screen at all, even though the BackgroundWorker does.

I gotta look up to see what MTA does and how it works.

Thanks,
tofuTnT
 
You should read about threading models of the COM/DCOM objects before saying that you do not understand C#. They are usually not written in C#...


Most likely, ChipH is right. Also, problems can arise from free threading model COM objects which access a certain resource (i.e. database) and which expect the resource to be free at that time, etc...
 
I have been searching and reading about the threading models and come to realize that I need to force the COM object to use MTA. I couldn't rewrite the COM, because it is a component from Microsoft Office 2003.
I tried to change [STAThead] to [MTAThread] and it didn't help.
So my question now becomes how I can force the COM object to use MTA when I initialized it in the BackgroundWorker.

Thanks,
tofuTnT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top