Thread: debug error

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    debug error

    I am running a simple program dealing with an Animal class. I have the program working (no errors and no warnings); however, when I run the program I get the following error:
    ------------------------------------------------------------------
    debug error

    program: D:\temdebug\temp.exe

    damage: after normal block (#42) at 0x007E0580

    press retry to debug the application
    ------------------------------------------------------------------

    When I press retry instaed of abort, the program terminates from an illegal operation.

    ------------------------------------------------------------------

    When I attempt to debug the error, it opens a file in:

    \vc98\crt\src\dbgheap.c

    more specifically, it points to a line in the file that states:

    (byte *) pbdata(phead))
    ------------------------------------------------------------------

    Can anyone explain what this means and why it happens. I am attaching only the main file (the program has 3 files) because I do not know how to attach all 3.

    Thanks for any help that you can provide. I am not a C++ guru, so please explain without getting fancy.

    Thanks again,
    Alan

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    Sorry but I can't really help you without knowing what you are doing in Animal.cpp + hpp. Since you end up in dbgheap.c I think you have a pointer error, probably just using a void ptr or dealocating memory twice. That usually what causes those errors. Can point you to the specific error you've done without more info though. But you can always step-debug trough your program until you get to the crash, then you know what line holds the error.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Animal.cpp file for debug error

    here is the animal.cpp file

    Will attach the animal.hpp file on the next post.

    If you know how to attach more than one file with just a single post let me know.

    Thanks,
    Alan

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    animal.hpp file for debug error

    Could not upload the header file because it is an .hpp file and not just a .h file. so here it is:


    #ifndef ANIMAL_HPP
    #define ANIMAL_HPP

    // sets constants
    //const int ANIMAL_NAME = 25;
    const int ANIMAL_SOUND = 15;
    const int AKC_NUMBER = 15;

    class Animal
    {
    private:
    int animalAge;
    char *animalName;//[ANIMAL_NAME];
    char animalSound[ANIMAL_SOUND];
    float animalWeight;
    int len;
    static int numStrings;
    public:
    Animal(); // default constructor
    // defined constructor
    Animal(int a, const char *name, const char *sound, double d);
    Animal(const char *name); // for type conversions
    ~Animal(); // deconstructor
    void speak();
    int getAge();
    char* getName();
    char* getSound();
    double getWeight();

    void setAge(int a);
    void setName();
    void setSound(const char *sound);
    void setWeight(double f);
    void show() const;
    friend ostream& operator << (ostream &left, const Animal &right);

    };
    #endif

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Where the bug is

    The program crashes as it exits the callme1() function. I am not sure why. A similar example works.
    Alan

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Your call to callme1() will result in your Animal destructor being called for the first time. I assume this is where your char *animalName gets de-allocated, therefore this is probably where your error is.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Destructor called

    That is a logical conclusion, but if that is the problem, what is the solution?

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >what is the solution?

    Double checking your destructor to make sure it deletes what you think it's deleting. Unless you post your class implementation that's about all I can suggest.

  9. #9
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    I didn't spot your 2nd upload, see PM.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >Animal::Animal()
    >{
    
    >    animalAge = 0;
    
    >    len = 7;
    //Need room for the string terminator.  Should be:
        len = 8;
    >	animalName = new char[7];
    //Should be:
    	animalName = new char[8];
    >	strcpy(animalName, "No Name");
    	
    >    strcpy(animalSound, "No Sound");
    >    animalSound[15] = '\0';
    //Don't need above line, but if it's there should be:
        animalSound[14] = '\0';
    
    >    animalWeight = 0.0;
    
    >    numStrings++;  // adds 1 to object count
    >    cout << numStrings << ": \"" << animalName
    >         << "\" default object created\n";
    >}
    >// 2) Defined Constructor
    >Animal::Animal(int a, const char *name, const char *sound, >double d)  
    >{
    >    animalAge = a;
    
    >    len = strlen(name);
    >    animalName = new char[len + 1];
    >	strcpy(animalName, name);
    	
    >    strncpy(animalSound, sound, 15);
    >    animalSound[15] = '\0';
    //Either change to 14 or use:
        strcpy(animalSound, sound);
    //And leave out the next line.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Thanks

    Thanks to all who helped in this little problem. Thanks especially to swoopy, the suggestion to change the length of len (from 7 to 8) to allow for the '\0' worked. The program works fine now. I have also taken the other suggestions and will make appropriate changes.

    Thanks again,
    Your help is always appreciated

    Alan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  5. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM