Thread: Strange core dump, help please

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    California
    Posts
    19

    Strange core dump, help please

    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 :-)

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    California
    Posts
    19
    Crap, sorry to waste your time! I got it.

    It seems that not having a newline character in a print statement before a core dump causes that print statement not to show up. Strange. I learned something today. As a result, I thought the bug was in the completely wrong place, where I declared that FILE object. Lame.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Smattacus View Post
    Crap, sorry to waste your time! I got it.

    It seems that not having a newline character in a print statement before a core dump causes that print statement not to show up. Strange. I learned something today. As a result, I thought the bug was in the completely wrong place, where I declared that FILE object. Lame.
    Of course, those two facts are relatively unrelated - it's simply that a newline forces anything in a printf to be flushed to the output, and if you core-dump, the application will stop immediately without further notice, so the "not yet printed" stuff will stay where it is held in preparation to be printed when a newline turns up.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    California
    Posts
    19
    Thank you for the clarification! I understand why it was doing that now.

    Debugging this particular problem has become much easier.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you are printing strings with no newline, you CAN do fflush(stdout) to flush the buffer at any given time, even if there is no newline. Or, if you really want to avoid buffering, you can use fprintf(stderr, ...), or use setvbuf(stdin, ...) with the _IONBF mode option.

    http://linux.die.net/man/3/setvbuf

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Look at the stack trace, which function did you call last?

    > while ((c1 = getc(sex_fl)) != ((int) '\n'))
    Maybe this one?

    What could go wrong with that?
    Well you could call it with sex_fl == NULL, which would suggest you failed to open the file.

    Mmm, looking back in the code, there's no check to see if you opened the file or not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    California
    Posts
    19
    Yes, you are correct.

    I had a function which was called outside of this module which I was using to expand environment variables, which was adding an extraneous "/" to the beginning of the filename.

    The main thing that was tripping me up here was that not putting a newline character on my second print statement,

    Code:
    	
            printf("Test ptfPhotometry_compute_photometry starts.\n");//TODO: Delete.
    
    	FILE * sex_fl = NULL;
    
    	printf("Test again - line 30");//TODO Delete.
    
            etc...core dump.
    was causing me to look at the wrong spot for the bug! It looked like I was getting a core dump when I initialized my file pointer, which made no sense.

    Thank you for your help guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. having a core dump
    By justins in forum C Programming
    Replies: 6
    Last Post: 05-21-2008, 12:00 PM
  2. STL vector core dump
    By creativeinspira in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2007, 04:49 PM
  3. Strange core dump
    By jcarouth in forum C Programming
    Replies: 9
    Last Post: 09-13-2005, 12:42 PM
  4. Core Dump in While()
    By KrepNatas in forum C Programming
    Replies: 5
    Last Post: 05-17-2005, 11:15 AM
  5. core dump
    By kermit in forum Linux Programming
    Replies: 0
    Last Post: 08-03-2004, 06:25 PM