Hi guys,
I'm pretty stumped on this one, as it's behaving kind of weird. I am running this module as part of a larger program that is doing some data analysis. However, when I run the code, I get a Bus Error right after it prints the first diagnostic print statement I put in (Line 24). When I GDB it I get the following stack trace and error message:
Code:
Test ptfPhotometry_compute_photometry starts.
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000004
0x9002d795 in getc ()
(gdb) where
#0 0x9002d795 in getc ()
#1 0x00005ff6 in ptfPhotometry_compute_photometry ()
#2 0x00001d6f in main ()
Here is my code, painstakingly drawn for your ease of viewing! :-p
Code:
/*
* ptfPhotometry_compute_photometry.c
*
* Created on: Sep 23, 2008
* Author: Sean Mattingly
*
* The main computation module.
*/
#include "ptfPhotometry.h"
#include "ptfPhotometry_defs.h"
#include "fitsio.h"
#include "wcslib.h"
#define INPUT_BUFFER_SIZE 2000
void ptfPhotometry_read_LDAC(char *, int *, PT_Flags *PT_Flag,
PT_ImageData *PT_SEx_Data);
void ptfPhotometry_read_ASCII(char *, PT_State *PT_Stat,
struct wcsprm *WCS_Params);
void ptfPhotometry_compute_photometry(PT_Filenames *PT_Fnames,
PT_Pathnames *PT_Pnames, PT_State *PT_Stat, PT_Flags *PT_Flag) {
printf("Test ptfPhotometry_compute_photometry starts.\n");//TODO: Delete.
FILE * sex_fl = NULL;
printf("Test again - line 30");//TODO Delete.
FILE * astref_fl = NULL;
FILE * output_fl = NULL;
int SEx_image_read = 0;
int ast_image_read = 0;
int out_image_written = 0;
PT_ImageData PT_SEx_Data;
PT_ImageData PT_Ast_Data;
PT_ImageData Out_Data;
int SEx_end_reached = FALSE;
int Ast_end_reached = FALSE;
int Out_end_reached = FALSE;
char input_SEx_fn[INPUT_BUFFER_SIZE];
char input_ast_fn[INPUT_BUFFER_SIZE];
char input_scampsol[INPUT_BUFFER_SIZE];
char out_fn[INPUT_BUFFER_SIZE];
int ldac_strlen;
int scamp_strlen;
int temp = 0;
int fits_strlen;
char c1;
struct wcsprm *WCS_Params;
printf("ptfPhotometry_compute_photometry.c...");
if (PT_Stat->Verbose)
printf(
"ptfPhotometry_compute_photometry: Opening files and reading for input.");
sex_fl = fopen(PT_Fnames->Filename_SEx_Catalog, "r");
astref_fl = fopen(PT_Fnames->Filename_Ast_Refs, "r");
output_fl = fopen(PT_Fnames->Filename_Output, "r");
while (TRUE) {
/**
* Read in the next filename from each file.
*/
sprintf(input_SEx_fn, "%s", "");
while ((c1 = getc(sex_fl)) != ((int) '\n')) {
if (c1 == EOF) {
SEx_end_reached = TRUE;
break;
}
if (c1 != ((int) (' ')))
sprintf(input_SEx_fn, "%s%c", input_SEx_fn, (char) c1);
c1 = getc(sex_fl);
}
sprintf(input_ast_fn, "%s", "");
while ((c1 = getc(astref_fl)) != ((int) '\n')) {
if (c1 == EOF) {
Ast_end_reached = TRUE;
break;
}
if (c1 != ((int) (' ')))
sprintf(input_ast_fn, "%s%c", input_ast_fn, (char) c1);
c1 = getc(astref_fl);
}
sprintf(out_fn, "%s", "");
while ((c1 = getc(output_fl)) != ((int) '\n')) {
if (c1 == EOF) {
Out_end_reached = TRUE;
break;
}
if (c1 != ((int) (' ')))
sprintf(out_fn, "%s%c", out_fn, (char) c1);
c1 = getc(output_fl);
}
/**
* Check if the end of any of the file lists has been reached.
*/
if (SEx_end_reached == TRUE || Ast_end_reached == TRUE
|| Out_end_reached == TRUE) {
if (PT_Stat->Verbose)
printf("End of filelist reached. Halting computations.");
printf("Note: This stops at the end of the shortest file.");
break;
}
/**
* Read the LDAC images of the Astref catalog and the SExtractor'd catalog.
*/
ptfPhotometry_read_LDAC(input_SEx_fn, &SEx_image_read, PT_Flag,
&PT_SEx_Data);
ptfPhotometry_read_LDAC(input_ast_fn, &ast_image_read, PT_Flag,
&PT_Ast_Data);
/**
* Obtain the ASCII filename from the current LDAC name.
* //TODO: Make a note for end user
* about the fact that LDACS and ASCIIs must be in the same directory.
*/
ldac_strlen = strlen(input_SEx_fn);
scamp_strlen = strlen(PT_Stat->ASCII_EXTENSION);
fits_strlen = strlen(FITS_EXTENSION);
strcpy(input_scampsol, input_SEx_fn);
input_scampsol[ldac_strlen - fits_strlen - 1] = '\0';
strcat(input_scampsol, PT_Stat->ASCII_EXTENSION);
/**
* Initialize the WCSLIB wcsprm struct.
* See WCSLIB documentation for details.
*/
WCS_Params->flag = -1;
if ((temp = wcsini(TRUE, 2, WCS_Params)) != 0) {
if (temp == 1)
printf("ptfPhotometry_compute: NULL wcsprm passed to wcsini.");
if (temp == 2)
printf("ptfPhotometry_compute: No available memory.");
}
WCS_Params->naxis = 2;
WCS_Params->flag = 0;
/*
* Obtain the WCS necessary data from the ascii files, to convert SEx's images to RA / Dec in the correct positions.
*/
ptfPhotometry_read_ASCII(input_scampsol, PT_Stat, WCS_Params);
/*
* Now that we have the necessary image data, convert to RA / DEC using WCS libs.
*/
}
}
From looking at my stack trace, I believe it's possible that perhaps there is a bad file pointer or something, due to getc() erroring, but I have not seen anything that is actually indicative of such. I am compiling this with the optimization option enabled, if that matters at all.
Thank you so much in advance for any help you may give me! If it's something obvious then...d'oh!
PS. This code is still not finished, I am just making sure it works while still putting it together. Any other pointers on it would be very much appreciated :-)