Changed (breaking) COM object handling in trunk lazarus / 21-General ---------------------------------------------------------------------- CCRDude · 2021-02-19 11:21 Platform: Windows, x86 and x64 I have an external library that uses COM objects and calls CoInitializeEx within. My own code calls CoInitializeEx for my own use as well. This worked fine until I updated my trunk version of Laz/FPC two weeks ago (the old one was months old). Ever since then, one of these library functions fail in creating a COM object (which leads to unlucky further handling within the library). I know that calling CoInitializeEx from a library is not a good option, but I can't change this. I have used fpcupdeluxe to install a bunch of alternatives from stable to fixes to trunk to verify this is new in trunk compared to the others. Has anyone experienced similar things? Does anyone what has changed in regards to this topic in the past months? ---------------------------------------------------------------------- PascalDragon · 2021-02-19 15:02 Quote from: CCRDude on February 19, 2021, 12:21:20 pmI know that calling CoInitializeEx from a library is not a good option, but I can't change this. Of course it's a valid choice. If your code needs to do COM calls (especially if the calling binary has no clue that you do) then you must initialize COM in your library. Quote from: CCRDude on February 19, 2021, 12:21:20 pmHas anyone experienced similar things? Does anyone what has changed in regards to this topic in the past months? Do you have any small code that shows this behaviour? Otherwise it will be really hard to comment on this... ---------------------------------------------------------------------- CCRDude · 2021-02-19 16:54 Quote from: PascalDragon on February 19, 2021, 04:02:07 pmOf course it's a valid choice. If your code needs to do COM calls (especially if the calling binary has no clue that you do) then you must initialize COM in your library.Well, there's a bunch of reports that this can cause issues (regardless of language). So it might be valid (and MSDN says so), but not necessarily good. From my point of view, if I get a library to implement, getting the information that I need to initialize COM is as much required as exported function documentation. Quote from: CCRDude on February 19, 2021, 12:21:20 pmDo you have any small code that shows this behaviour? Otherwise it will be really hard to comment on this... A minimum program can demonstrate it (that's how I located the issue), the problem is that the third party library is not freely available. I'll see if I can reference to it. As for the valid action, here's a reason why it may not be valid: I learned that the third party module uses COINIT_APARTMENTTHREADED, while I use COINIT_MULTITHREADED. Two parts of code that expect initialization with different concurrency models. ---------------------------------------------------------------------- PascalDragon · 2021-02-21 15:14 Quote from: CCRDude on February 19, 2021, 05:54:51 pmQuote from: PascalDragon on February 19, 2021, 04:02:07 pmOf course it's a valid choice. If your code needs to do COM calls (especially if the calling binary has no clue that you do) then you must initialize COM in your library.Well, there's a bunch of reports that this can cause issues (regardless of language). So it might be valid (and MSDN says so), but not necessarily good. From my point of view, if I get a library to implement, getting the information that I need to initialize COM is as much required as exported function documentation. It shouldn't matter if it's done inside a library or not. From Windows' point of view it's just a bunch of nested CoInitialize(Ex)/CoUnitialize calls, so it should be the same if you're doing it inside the same binary. Quote from: CCRDude on February 19, 2021, 05:54:51 pmQuote from: CCRDude on February 19, 2021, 12:21:20 pmDo you have any small code that shows this behaviour? Otherwise it will be really hard to comment on this... A minimum program can demonstrate it (that's how I located the issue), the problem is that the third party library is not freely available. I'll see if I can reference to it. As for the valid action, here's a reason why it may not be valid: I learned that the third party module uses COINIT_APARTMENTTHREADED, while I use COINIT_MULTITHREADED. Two parts of code that expect initialization with different concurrency models. The MSDN explicitely states that you can not mix threading models inside the same thread. So you either need to use the same model or you need to initialize from different threads (and make sure that you don't have cross thread usage of COM objects of one thread in another thread). ---------------------------------------------------------------------- CCRDude · 2021-02-22 07:58 Quote from: PascalDragon on February 21, 2021, 04:14:02 pmSo you either need to use the same model or you need to initialize from different threads (and make sure that you don't have cross thread usage of COM objects of one thread in another thread). That indeed sounds like "unnecessary" overhead, but the way to go to keep that third party module out of everything. Thanks for this useful suggestion