Cryptic debug error

This is a discussion on Cryptic debug error within the C++ Programming forums, part of the General Programming Boards category; I am using codeblocks and I am getting this error: Program received signal (SIGTRAP) Trace/breakpoint trap Previous frame inner to ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    89

    Cryptic debug error

    I am using codeblocks and I am getting this error:

    Program received signal (SIGTRAP)
    Trace/breakpoint trap
    Previous frame inner to this frame (corrupt stack?)
    error


    What does it mean? I have searched google and thus far have gotten zilch?

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    Here is the function where the error occurs

    Code:
    #include "CLoadLevel.h"
    
    void Pause();
    
    CLoadLevel::CLoadLevel()
    {
    };
    
    CLoadLevel::~CLoadLevel()
    {
    };
    
    void CLoadLevel::LoadLevel(CCharacter* enemy, CItem* weapon, char* filename)
    {
        std::cout << "Enter Function Load Level\n" << std::endl;
        Pause();
    
        std::ifstream in(filename, std::ios::binary);
    
        std::cout << "Reading in enemycount\n" << std:: endl;
        Pause();
        in.read((char*)&enemycount, sizeof(int));
        std::cout << "enemycount = " << enemycount << std::endl << std::endl;
    
        enemy[enemycount];
    
        for(int count = 0; count < enemycount; count++)
        {
            std::cout << "\nReading in enemy strength\n" << std::endl;
            int strength = 0;
            in.read((char*)&strength, sizeof(int));
            std::cout << "Strength read = " << strength << std::endl << std::endl;
            Pause();
            enemy[count].SetStrength(strength);
    
            int dexterity = 0;
            in.read((char*)&dexterity, sizeof(int));
            enemy[count].SetDexterity(dexterity);
    
            int intelligence = 0;
            in.read((char*)&intelligence, sizeof(int));
            enemy[count].SetIntelligence(intelligence);
    
            int endurance = 0;
            in.read((char*)&endurance, sizeof(int));
            enemy[count].SetEndurance(endurance);
    
            int hp = 0;
            in.read((char*)&hp, sizeof(int));
            enemy[count].SetHP(hp);
    
            int maxhp = 0;
            in.read((char*)&maxhp, sizeof(int));
            enemy[count].SetMaxHP(maxhp);
    
            int mp = 0;
            in.read((char*)&mp, sizeof(int));
            enemy[count].SetMP(mp);
    
            int maxmp = 0;
            in.read((char*)&maxmp, sizeof(int));
            enemy[count].SetMaxMP(maxmp);
    
            int gold = 0;
            in.read((char*)&gold, sizeof(int));
            enemy[count].SetGold(gold);
    
            std::cout << "XP" << std::endl;
            int xp = 0;
            in.read((char*)&xp, sizeof(int));
            enemy[count].SetXP(xp);
        }
    
        in.read((char*)&weaponcount, sizeof(int));
    
        weapon[weaponcount];
    
        for (int count = 0; count < weaponcount; count++)
        {
            int mindamage = 0;
            in.read((char*)&mindamage, sizeof(int));
            weapon[count].SetMinAmount(mindamage);
            std::cout << "minDamage " << mindamage << std::endl;
    
            int maxdamage = 0;
            in.read((char*)&maxdamage, sizeof(int));
            weapon[count].SetMaxAmount(maxdamage);
    
            int price = 0;
            in.read((char*)&price, sizeof(int));
            weapon[count].SetPrice(price);
            std::cout << "Price " << price << std::endl;
        }
        std::cout << "Closing file\n";
        in.close();
        std::cout << "File Closed.\n";
    };
    In debug mode the program gets all the way to std::cout << "File Closed\n";
    before it crashes and I get my SIGTRAP error. Is it maybe cuz I ran out of memory...my program is on a 3.5" floppy.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > enemy[enemycount];
    What's this supposed to do?
    To me, it just looks like an out of bound memory reference.

    Is enemy even allocated any space when you call the load function, and if so, how much?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    > enemy[enemycount];
    What's this supposed to do?
    To me, it just looks like an out of bound memory reference.

    Is enemy even allocated any space when you call the load function, and if so, how much?
    I read in the variable enemycount here
    Code:
    std::cout << "Reading in enemycount\n" << std:: endl;
        Pause();
        in.read((char*)&enemycount, sizeof(int));
        std::cout << "enemycount = " << enemycount << std::endl << std::endl;

    Tho now I just noticed that I forgot to declare enemycount...Cant believe I missed that.
    Shouldn't that have given me a compile error?

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    Code:
    int main()
    {
        CEnemy* newenemy = new CEnemy[3];
        CWeapon* neww = new CWeapon[3];
        CLoadLevel load;
    
        load.LoadLevel(newenemy, neww, "LevelTest.dat");
    
        std::cout << "Exited LoadLevel Function\n" << std::endl;
    
    
        delete [] newenemy;
        delete [] neww;
    
    
        Pause();
    
        return 0;
    }
    I see my problem...I inialized my object arrays to hold only three objects
    Last edited by eaane74; 11-19-2007 at 10:31 AM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You should use vectors instead of dynamic arrays. They are easier, safer and more flexible. If you did, then you could use push_back to add to the vector instead of worrying about what size to create the dynamic arrays before calling the function.

    >> I just noticed that I forgot to declare enemycount... Shouldn't that have given me a compile error?
    Perhaps it is a member variable of CLoadLevel?

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    >> I just noticed that I forgot to declare enemycount... Shouldn't that have given me a compile error?
    Perhaps it is a member variable of CLoadLevel?
    You are right.

    Ill try using vectors...should've done it before.

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    THANKS Daved...It seems to work fine.

    Now I know why you guys stress using vectors so much over dynamic arrays!

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > CEnemy* newenemy = new CEnemy[3];
    Well if enemycount is >= 3, then the code is broken as it trashes memory you don't own.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    > CEnemy* newenemy = new CEnemy[3];
    Well if enemycount is >= 3, then the code is broken as it trashes memory you don't own
    That was poor coding on my part. I dont think I will be using arrays anymore

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, 02:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03: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. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21