Is it a bug? Or is it my fault?

This is a discussion on Is it a bug? Or is it my fault? within the C++ Programming forums, part of the General Programming Boards category; In Code::Blocks, somewhere in my code, I declared an ostringstream object. But that gives me an segmentation error. Even only ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    66

    Is it a bug? Or is it my fault?

    In Code::Blocks, somewhere in my code, I declared an ostringstream object.
    But that gives me an segmentation error.

    Even only declaring it would rise up an error.

    Is this a bug in Code::Blocks or is it my fault?

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    You've probably got an bug somewhere else in your code, and it's manifested itself in the instantiation of the ostringstream object.

    You're gonna need to post some code to get more specific than that.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    66
    Code:
    #include "cGameInstance.h"
    
    cGameInstance cGameInstance::a_game;
    
    cGameInstance::cGameInstance()
    {
    	Surf_Card       = NULL;
    	Surf_Background = NULL;
    	Surf_Blank      = NULL;
    	Surf_Border     = NULL;
    
    	StartTime = 0;
    
    	Board_Cols = 0;
    	Board_Rows = 0;
    	PairsLeft  = 0;
    
    	ClickState = OPEN_CARD1;
    	CardsOpen[OPEN_CARD1] = NULL;
    	CardsOpen[OPEN_CARD2] = NULL;
    }
    
    void cGameInstance::SetMode(std::string& CardSet, int GameMode)
    {
    	CardSetFile.assign(CardSet);
    
    	switch(GameMode)
    	{
    		case FOURFOUR   : Board_Cols = 4;Board_Rows = 4;break;
    		case FOURFIVE   : Board_Cols = 4;Board_Rows = 5;break;
    		case SIXFIVE    : Board_Cols = 6;Board_Rows = 5;break;
    		case SIXSIX     : Board_Cols = 6;Board_Rows = 6;break;
    		case SIXSEVEN   : Board_Cols = 6;Board_Rows = 7;break;
    		case SIXEIGHT   : Board_Cols = 6;Board_Rows = 8;break;
    		case SEVENSIX   : Board_Cols = 7;Board_Rows = 6;break;
    		case SEVENEIGHT : Board_Cols = 7;Board_Rows = 8;break;
    		case EIGHTSEVEN : Board_Cols = 8;Board_Rows = 7;break;
    		case EIGHTEIGHT : Board_Cols = 8;Board_Rows = 8;break;
    	}
    }
    
    bool cGameInstance::OnInit()
    {
    	if((Surf_Background = CSurface::OnLoad("gfx/game.jpg")) == NULL)
    	{
    		MessageBox(GetActiveWindow(), IMG_GetError(), "SDL Resource Load Failure", MB_OK | MB_ICONEXCLAMATION);
    		return false;
    	}
    
    	if((Surf_Border = CSurface::OnLoad("gfx/border.png")) == NULL)
    	{
    		MessageBox(GetActiveWindow(), IMG_GetError(), "SDL Resource Load Failure", MB_OK | MB_ICONEXCLAMATION);
    		return false;
    	}
    
    	if((Surf_Blank = CSurface::OnLoad("gfx/card.png")) == NULL)
    	{
    		MessageBox(GetActiveWindow(), IMG_GetError(), "SDL Resource Load Failure", MB_OK | MB_ICONEXCLAMATION);
    		return false;
    	}
    
    	if((Surf_Card = CSurface::OnLoad(CardSetFile.c_str())) == NULL)
    	{
    		MessageBox(GetActiveWindow(), IMG_GetError(), "SDL Resource Load Failure", MB_OK | MB_ICONEXCLAMATION);
    		return false;
    	}
    
    	GenerateGameCards();
    	ResetCards();
    
    	return true;
    }
    
    void cGameInstance::ResetGame()
    {
    	CardList.clear();
    	GenerateGameCards();
    	ResetCards();
    }
    
    void cGameInstance::GenerateGameCards()
    {
    	srand(SDL_GetTicks());
    
    	PairsLeft = (Board_Cols * Board_Rows) >> 1;
    
    	unsigned Cards   = Board_Cols * Board_Rows;
    	unsigned *Digits = new unsigned[Cards];
    
    	memset(Digits, 0, sizeof(unsigned) * Cards);
    
    	for(unsigned i = 0;i < PairsLeft;++i)
    	{
    		unsigned Number;
    		unsigned Index;
    
    		bool done;
    
    		do
    		{
    			done = true;
    			Number = (rand() &#37; MAX_CARDS) + 1;
    
    			for(Index = 0;Index < Cards;++Index)
    			{
    				if(Digits[Index] == Number)
    				{
    					done = false;
    					break;
    				}
    			}
    		}
    		while(!done);
    
    		do Index = rand() % Cards;while(Digits[Index] != 0);
    		Digits[Index] = Number;
    
    		do Index = rand() % Cards;while(Digits[Index] != 0);
    		Digits[Index] = Number;
    	}
    
    
    	for(unsigned i = 0;i < Cards;++i)
    	{
    		cCard newCard(Digits[i] - 1, Surf_Card);
    		CardList.push_back(newCard);
    	}
    
    	delete [] Digits;
    
    	unsigned spaceX = (BOARD_WIDTH  - (Board_Cols * IMAGE_W)) / (Board_Cols + 1);
    	unsigned spaceY = (BOARD_HEIGHT - (Board_Rows * IMAGE_H)) / (Board_Rows + 1);
    
    	for(unsigned i = 0;i < Cards;++i)
    	{
    		CardList[i].whereX = ((i % Board_Cols) * IMAGE_W) + (((i % Board_Cols) + 1) * spaceX) + ((SCREEN_WIDTH  - BOARD_WIDTH ) / 2);
    		CardList[i].whereY = ((i / Board_Cols) * IMAGE_H) + (((i / Board_Cols) + 1) * spaceY) + ((SCREEN_HEIGHT - BOARD_HEIGHT) / 2);
    	}
    
    	StartTime = SDL_GetTicks();
    }
    
    void cGameInstance::OnCleanUp()
    {
    	CardList.clear();
    
    	CardsOpen[OPEN_CARD1] = NULL;
    	CardsOpen[OPEN_CARD2] = NULL;
    
    	SDL_FreeSurface(Surf_Background);
    	Surf_Background = NULL;
    
    	SDL_FreeSurface(Surf_Border);
    	Surf_Border = NULL;
    
    	SDL_FreeSurface(Surf_Blank);
    	Surf_Blank  = NULL;
    
    	SDL_FreeSurface(Surf_Card);
    	Surf_Card   = NULL;
    }
    
    void cGameInstance::OnPause()
    {
    }
    
    void cGameInstance::OnResume()
    {
    }
    
    void cGameInstance::ResetCards()
    {
    	for(unsigned i = 0;i < CardList.size();++i)
    	{
    		CardList[i].Closed = !CardList[i].Found;
    	}
    
    	CardsOpen[OPEN_CARD1] = NULL;
    	CardsOpen[OPEN_CARD2] = NULL;
    	ClickState = OPEN_CARD1;
    }
    
    void cGameInstance::OnEvent(cGameEngine *Memory_Game)
    {
    	SDL_Event event;
    
    	if(SDL_WaitEvent(&event))
    	{
    		switch(event.type)
    		{
    			case SDL_QUIT : Memory_Game -> Quit();break;
    			case SDL_KEYDOWN :
    			{
    				switch(event.key.keysym.sym)
    				{
    				    case SDLK_ESCAPE : Memory_Game -> PopState();break;
    					default          : break;
    				}
    			}
    			break;
    			case SDL_MOUSEMOTION :
    			{
    			    for(unsigned i = 0;i < CardList.size();++i)
    				{
    					CardList[i].OnMouseMove(event.motion.x, event.motion.y);
    				}
    
    				break;
    			}
    			case SDL_MOUSEBUTTONDOWN :
    			{
    				switch(event.button.button)
    				{
    					case SDL_BUTTON_LEFT :
                        {
    						if(ClickState == RESET_CARD)
    							ResetCards();
    
    						else if(ClickState == OPEN_CARD1 || ClickState == OPEN_CARD2)
    						{
    							for(unsigned i = 0;i < CardList.size();++i)
    							{
    								CardsOpen[ClickState] = CardList[i].OnLeftButtonDown();
    
    								if(CardsOpen[ClickState] != NULL)
    								{
    									ClickState++;
    									break;
                                    }
    							}
    						}
    						break;
    					}
                        default: break;
    				}
    				break;
    			}
    			default : break;
    		}
    	}
    
    	if(PairsLeft == 0)
    	{
    		std::ostringstream Message;
    		float Time = (SDL_GetTicks() - StartTime) / 1000.0f;
    		Message << "Well Done! Your Time Was " << Time << " seconds.\nDo You Want To Play Again?";
    		if(MessageBox(GetActiveWindow(), Message.str().c_str(), "Well Done!", MB_YESNO | MB_ICONINFORMATION) == IDYES)
    			ResetGame();
    
    		else Memory_Game -> PopState();
    	}
    }
    
    void cGameInstance::OnLoop()
    {
        std::ostringstream WindowCaption;
    	WindowCaption << "MEMORY_GAME - Pairs Left : " << PairsLeft;
    	SDL_WM_SetCaption(WindowCaption.str().c_str(), NULL);
    
        if(CardsOpen[OPEN_CARD1] == NULL || CardsOpen[OPEN_CARD2] == NULL)
    		return;
    
    	if(CardsOpen[OPEN_CARD1] -> Type == CardsOpen[OPEN_CARD2] -> Type)
    	{
    		CardsOpen[OPEN_CARD1] -> Found   = CardsOpen[OPEN_CARD2] -> Found   = true;
    		CardsOpen[OPEN_CARD1] -> OnHover = CardsOpen[OPEN_CARD2] -> OnHover = false;
    
    		ResetCards();
    		PairsLeft--;
    	}
    }
    
    void cGameInstance::OnRender(SDL_Surface *Surf_Display)
    {
    	if(Surf_Display == NULL || Surf_Background == NULL)
    		return;
    
    	CSurface::OnDraw(Surf_Display, Surf_Background, 0, 0);
    
    	for(unsigned i = 0;i < CardList.size();++i)
    	{
    		CardList[i].OnRender(Surf_Display, Surf_Border, Surf_Blank);
    	}
    }
    
    cGameInstance* cGameInstance::Instance()
    {
    	return &a_game;
    }
    This code below, which is a part of the code above, also has a declaration of ostringstream, but doesn't raise up any error.

    Code:
    	if(PairsLeft == 0)
    	{
    		std::ostringstream Message;
    		float Time = (SDL_GetTicks() - StartTime) / 1000.0f;
    		Message << "Well Done! Your Time Was " << Time << " seconds.\nDo You Want To Play Again?";
    		if(MessageBox(GetActiveWindow(), Message.str().c_str(), "Well Done!", MB_YESNO | MB_ICONINFORMATION) == IDYES)
    			ResetGame();
    
    		else Memory_Game -> PopState();
    	}
    While the declaration of WindowCaption below does raise an error, even if I remove the next two lines.

    Code:
    void cGameInstance::OnLoop()
    {
        std::ostringstream WindowCaption;
    	WindowCaption << "MEMORY_GAME - Pairs Left : " << PairsLeft;
    	SDL_WM_SetCaption(WindowCaption.str().c_str(), NULL);
    
        if(CardsOpen[OPEN_CARD1] == NULL || CardsOpen[OPEN_CARD2] == NULL)
    		return;
    
    	if(CardsOpen[OPEN_CARD1] -> Type == CardsOpen[OPEN_CARD2] -> Type)
    	{
    		CardsOpen[OPEN_CARD1] -> Found   = CardsOpen[OPEN_CARD2] -> Found   = true;
    		CardsOpen[OPEN_CARD1] -> OnHover = CardsOpen[OPEN_CARD2] -> OnHover = false;
    
    		ResetCards();
    		PairsLeft--;
    	}
    }
    And also, this only happens in Code::Blocks, not in MinGW Developer Studio
    Last edited by ThLstN; 08-24-2008 at 10:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 09:27 PM
  2. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 08:52 AM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

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