I have a peculiar problem with the Task Scheduler interfaces and I would be very happy if someone could help.
A part of my application reads the items from the Task Scheduler. Under XP I use the Task Scheduler v1.0 interfaces from a unit from JEDI Library. Under Vista and Win7 I used the TaskSchd.pas unit found in this article. It contains the definitions for the new 2.0 interfaces. I use Delphi XE2, Update 4.
The problem is that I get an Access Violation when I call the Connect method of the ITaskService interface. The error message is "First chance exception at $0055C73C. Exception class $C0000005 with message 'access violation at 0x0055c73c: read of address 0x0000000b'."
The code I use is:
procedure TSomeClass.TaskScheduler(...);
var
TS : ITaskService;
TaskFolder : ITaskFolder;
begin
TS := CoTaskScheduler.Create();
TS.Connect('', '', '', '');
......
end;
The code is in a separate unit in a class descendant from TObject. This is enough to cause AV, even without doing anything else with the interface. The AV happens when the function exits though, not at the Connect line, so it might be related to the releasing of the object.
I have ran some tests, but could not find where the problem is. Here is what is strange:
- The AV happens only in Release builds, in Debug it works properly
- If I turn off Optimization in Project Options > Compiling then again the code works. If I turn it on in Debug build, the code stops working.
- If I place this in some method in the main form, it works again.
- I have called CoInitialize, but it does not have any effect
- I tried to duplicate it in a blank new app, but I could not, so it is somehow related to another unit or class in the application
It is probably something very simple that I am missing about interfaces, but I have not been able to find it. The code otherwise works, I can get and display the list of tasks. The problem comes when the Compiling > Optimizations option is turned on, which is the default for the Release build.
One solution would be to just turn off the Optimizations, but I would rather find the cause of the problem.
Edit: I have included additional info about the AV, CoInitialize, Delphi and the ITaskService header file.