2

I made a C program to get an image offline and save it in a file using fopen/fread/fwrite and libcurl. Everything compiles perfectly fine and I can run it perfectly fine by double clicking the program. But, when I try and set a task on my computer using task scheduler to run it every 10 minutes, the program opens and when it tries to save the file I get the error:

exception::handle: Exception: STATUS_ACCESS_VIOLATION open_stackdumpfile: Dumping stack trace to garden.exe.stackdump

I am running Windows Vista, and attempting to use the native Windows Scheduled Tasks feature. I have checked the box marked "Run with highest privileges" in the task properties.

kapa
  • 77,694
  • 21
  • 158
  • 175
jeremy
  • 9,965
  • 4
  • 39
  • 59
  • 5
    The program is faulting with an access violation. Information about this error has been dumped to garden.exe.stackdump. – David Heffernan Jul 06 '11 at 18:15
  • 1
    There is a possibility (maybe even probability) that you have a different environment under the task scheduler from when you run it at the command line. You probably have some code that doesn't check a return from some system call but proceeds to use what is actually an invalid reference - maybe a null pointer - that was the result of a function you assumed would 'always succeed'. – Jonathan Leffler Jul 06 '11 at 18:19
  • There is no garden.exe.stackdump – jeremy Jul 06 '11 at 18:24

1 Answers1

5

My guess is that whatever is running your program is doing so from a directory to which you don't have write permissions. The fact that there is no "garden.exe.stackdump" file gives that as a clue.

When you save it to a file, are you saving it as fopen("myfile","w") or are you using a fully qualified name? For example, let's say that the file you want to save is called "foobar.png" and you want to save it to the directory you named below, you'd have something like:

    char fname[256] = "foobar.png";
    char directory[256] = "C:/Users/Joel/Desktop/garden/snaps";
    char path[256];

    memset(path, 0, sizeof(path));
    strcpy(path, directory);
    strcat(path, "/");
    strcat(path, fname);

    if ((fp = fopen(path, "w")) == NULL) {
        fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
        exit(1);
    }

    fwrite(yourdata, yourdata_size, 1, fp);

Since your program also seems to dump errors to a file as well, you might do well to chdir("/home/myname") at the start of your program so that any ".stackdump" files get placed where you have access.

The other thing you might want to take into account is that your task scheduler may be running your script as nobody or some other permissions-deprived account. If that's the case, you'll want to use a full path in fopen and chdir to a globally writable area (such as /tmp) or a working directory with open permissions. For example:

mkdir /home/myname/scratch
chmod a+rwx /home/myname/scratch
chmod a+x   /home/myname

(You set the execute bit on your home directory so that the permission-less program can get access to its subdirectory even though it can't read anything in it.)

unpythonic
  • 4,020
  • 19
  • 20
  • I'm simply using fopen("myfile", "w"). I selected the checkbox in the task scheduler "Run with highest permission settings." I want to write to /Users/Joel/Desktop/garden/snaps, is that not possible with the task scheduler? – jeremy Jul 06 '11 at 19:46
  • @Nile - Of course you can write there. However, if you're writing via `fopen/fwrite`, then you need to give that full path to that location. Otherwise, it's just going to try to drop it from whichever directory is current for the task scheduler. – unpythonic Jul 06 '11 at 20:10
  • @Nile - It might also help you you specify which task scheduler you're using. I'm assuming you're running on linux. – unpythonic Jul 06 '11 at 20:11
  • 1
    I'm running it on Windows Vista. – jeremy Jul 06 '11 at 20:13
  • So if I wanted it to save to C:/Users/Joel/Desktop/garden/snaps, what would the fopen look like? – jeremy Jul 06 '11 at 20:18
  • @Nile - added to the text above with a expanded example – unpythonic Jul 06 '11 at 20:39
  • Wow that's great, but now I'm getting that Failed to open file error. – jeremy Jul 06 '11 at 21:05
  • *help* is there an easy solution? – jeremy Jul 06 '11 at 21:41
  • I'm not sure because I deleted the %s's and the path and strerror(errno) because the compiler was telling me that errno is undeclared. – jeremy Jul 06 '11 at 21:47
  • `#include ` for `errno` and `#include ` for `strerror`. You need to start reading the man pages since there is a ton of help out there for this. (e.g. in google type: `man errno`) – unpythonic Jul 06 '11 at 21:50
  • "Failed to open file snaps/1309989761.jpg: No such file or directory" – jeremy Jul 06 '11 at 22:03
  • And this works when running from the command line or by "clicking the icon"? Are you sure that C:/Users/Joel/Desktop/garden/snaps exists? `fopen` will not create the directories for you. – unpythonic Jul 06 '11 at 22:03
  • My bad. The second `strcpy` should have been a `strcat`. Fixed above. – unpythonic Jul 06 '11 at 22:06
  • I'm positive that it works when running it from the command line or by clicking on the executable. /snaps does exist and I'm fully aware of all fopen's capabilities. (I'm actually doing it in C:/cygwin/home/Jeremy first, then when I have it figured out, I'll change the file locations in the code and move the program) – jeremy Jul 06 '11 at 22:07
  • @Nile - Good to hear. Enjoy. – unpythonic Jul 06 '11 at 22:42