Welcome! Log In Create A New Profile

Advanced

CreateThread() crashs my Wii

Posted by YoshiParty 
CreateThread() crashs my Wii
November 23, 2008 04:32PM
Hi,

does anybody know why the following code snippet always crashs my Wii, when executing it?

boot.h

struct boot_struct
{
public:
boot_struct( void );
~boot_struct( void );

private:
static void *BootScreenThread( void *arg );

static GameWindow *gwd;
static bool booting;
static lwp_t *pBootScreenHandle;
static Image *pBootScreenImage;
static Sprite *pBootScreenSprite;
};

boot.cpp

#include "boot.h"

GameWindow *boot_struct::gwd             = NULL;
bool boot_struct::booting                        = false;
lwp_t *boot_struct::pBootScreenHandle   = NULL;
Image *boot_struct::pBootScreenImage   = NULL;
Sprite *boot_struct::pBootScreenSprite    = NULL;

void* boot_struct::BootScreenThread( void *arg )
{
    pBootScreenImage = new Image;
    pBootScreenSprite = new Sprite;

    pBootScreenImage->LoadImage( BootScreen );
    pBootScreenSprite->SetImage( pBootScreenImage );
    pBootScreenSprite->SetStretchWidth( 1.0f/640.0f*width );
    pBootScreenSprite->SetStretchHeight( 1.0f/480.0f*height );

    while( booting )
    {
        WPAD_ScanPads();
		u32 pressed = WPAD_ButtonsDown(0);

		pBootScreenSprite->Draw();
		gwd->Flush();

		if( pressed & WPAD_BUTTON_HOME ) exit(0);
		VIDEO_WaitVSync();
    }

    delete pBootScreenImage;
    pBootScreenImage = NULL;
    delete pBootScreenSprite;
    pBootScreenSprite = NULL;

    return NULL;
}

int boot_struct::ShowBootScreen( void )
{
    booting = true;
    gwd->InitVideo();
    gwd->SetBackground( (GXColor){100, 100, 255} );
    if( LWP_CreateThread( pBootScreenHandle, BootScreenThread, NULL, NULL, 0, 80 ) < 0 ) return 0;

    return 1;
}

int boot_struct::HideBootScreen( void )
{
    booting = false;
    LWP_SuspendThread( *pBootScreenHandle );
	gwd->StopVideo();
    return 1;
}


The compiler doesn't complain, but my Wii seems have a problem with the memory-.-
My aim was it to show a booting screen in a thread while loading other things...

Hope you understand some parts of my GermanEnglish combinations
Re: CreateThread() crashs my Wii
November 23, 2008 04:58PM
Simple Reason: Your using a struct instead of a class. I don't know very much about the differences between the two, but since you're working in C++ maybe that's the reason.

Beyond that (and that's a big maybe), I don't see any glaring errors in your code.

EDIT:Declare your pointers to NULL before you delete them. When constructing a new image use the following syntax:

Image *Image1 = new Image();//You were missing the ()



Edited 2 time(s). Last edit at 11/23/2008 05:03PM by Arikado.
Re: CreateThread() crashs my Wii
November 23, 2008 05:40PM
Uhm, structs are just like classes, excepts that their members are public by default.
Re: CreateThread() crashs my Wii
November 23, 2008 07:39PM
Classes are also allowed code, structures are mearly for representing data
Re: CreateThread() crashs my Wii
November 23, 2008 07:41PM
Sorry, but everything in this post is wrong.

Quote
Arikado
Simple Reason: Your using a struct instead of a class. I don't know very much about the differences between the two, but since you're working in C++ maybe that's the reason.

There is no difference except that members of structs are public by default and members of classes are private. The compiler will catch this anyway.

Quote

EDIT:Declare your pointers to NULL before you delete them.

I don't know what this means. Obviously you can't say "ptr = NULL; delete ptr;" because that will be a memory leak. If you mean CHECK that the ptr is NULL before deleting, there's no need because "delete NULL" is safe (it just does nothing). If you mean to set your pointers to NULL *after* you delete them... uh, it's already doing that.

Quote

When constructing a new image use the following syntax:

Image *Image1 = new Image();//You were missing the ()

No, don't do that. It's one of C++'s weird quirks: a constructor without parameters doesn't take brackets. It doesn't matter so much in a new statement because the "new" makes it unambiguous, but it's best to get in the habit of leaving off the brackets.

The important case is that "Image image1;" is an image created with the default constructor, but "Image image1();" declares a functin named image1 which returns an Image. (See [www.parashift.com] - the C++ FAQ has lots of useful things to know.)

Quote
whodares
Classes are also allowed code, structures are mearly for representing data

Not true at all.



Edited 1 time(s). Last edit at 11/23/2008 07:42PM by JoeNotCharles.
Re: CreateThread() crashs my Wii
November 24, 2008 01:31PM
Hi,

declare your pBootScreenHandle just as "static lwp_t pBootScreenHandle" and initialize it with LWP_THREAD_NULL.
And then call LWP_CreateThread(&pBootScreenHandle,........). This function expects a pointer to a lwp_t handle.
While you already declare it as a pointer, initialized to NULL, LWP_CreateThread will dereferrence this pointer and thus is accessing an invalid memory location. BUMM it's crashing. LWP_CreateThread doesn't malloc the handle for you.
The other option is to allocate the memory for this handle on your own in ShowBootScreen before the call to LWP_CreateThread. This way you can leave the rest of the code as it is.

regards
shagkur
Re: CreateThread() crashs my Wii
November 24, 2008 03:16PM
Quote
JoeNotCharles
Quote
whodares
Classes are also allowed code, structures are mearly for representing data
Not true at all.

Really? Got any examples? I'm interested in seeing them

Edit: Ahh, apparently, it's a C++ thing to have code in structures.



Edited 1 time(s). Last edit at 11/24/2008 03:21PM by whodares.
Re: CreateThread() crashs my Wii
November 26, 2008 03:16PM
Quote
shagkur
Hi,

declare your pBootScreenHandle just as "static lwp_t pBootScreenHandle" and initialize it with LWP_THREAD_NULL.
And then call LWP_CreateThread(&pBootScreenHandle,........). This function expects a pointer to a lwp_t handle.
While you already declare it as a pointer, initialized to NULL, LWP_CreateThread will dereferrence this pointer and thus is accessing an invalid memory location. BUMM it's crashing. LWP_CreateThread doesn't malloc the handle for you.
The other option is to allocate the memory for this handle on your own in ShowBootScreen before the call to LWP_CreateThread. This way you can leave the rest of the code as it is.

regards
shagkur


:)
thx much for your help


edit:
damn...it still crashs :(

I tried both of your possible ways...

1)
boot.h

struct boot_struct
{
public:
boot_struct( void );
~boot_struct( void );

private:
static void *BootScreenThread( void *arg );

static GameWindow *gwd;
static bool booting;
static lwp_t pBootScreenHandle;
static Image *pBootScreenImage;
static Sprite *pBootScreenSprite;
};

boot.cpp

#include "boot.h"

GameWindow *boot_struct::gwd             = NULL;
bool boot_struct::booting                        = false;
lwp_t boot_struct::pBootScreenHandle    = LWP_THREAD_NULL;
Image *boot_struct::pBootScreenImage   = NULL;
Sprite *boot_struct::pBootScreenSprite    = NULL;

void* boot_struct::BootScreenThread( void *arg )
{
    pBootScreenImage = new Image;
    pBootScreenSprite = new Sprite;

    pBootScreenImage->LoadImage( BootScreen );
    pBootScreenSprite->SetImage( pBootScreenImage );
    pBootScreenSprite->SetStretchWidth( 1.0f/640.0f*width );
    pBootScreenSprite->SetStretchHeight( 1.0f/480.0f*height );

    while( booting )
    {
        WPAD_ScanPads();
		u32 pressed = WPAD_ButtonsDown(0);

		pBootScreenSprite->Draw();
		gwd->Flush();

		if( pressed & WPAD_BUTTON_HOME ) exit(0);
		VIDEO_WaitVSync();
    }

    delete pBootScreenImage;
    pBootScreenImage = NULL;
    delete pBootScreenSprite;
    pBootScreenSprite = NULL;

    return NULL;
}

int boot_struct::ShowBootScreen( void )
{
    booting = true;
    gwd->InitVideo();
    gwd->SetBackground( (GXColor){100, 100, 255} );
    if( LWP_CreateThread( &pBootScreenHandle, BootScreenThread, NULL, NULL, 0, 80 ) < 0 ) return 0;

    return 1;
}

int boot_struct::HideBootScreen( void )
{
    booting = false;
    LWP_SuspendThread( pBootScreenHandle );
	gwd->StopVideo();
    return 1;
}


2)boot.h

struct boot_struct
{
public:
boot_struct( void );
~boot_struct( void );

private:
static void *BootScreenThread( void *arg );

static GameWindow *gwd;
static bool booting;
static lwp_t *pBootScreenHandle;
static Image *pBootScreenImage;
static Sprite *pBootScreenSprite;
};

boot.cpp

#include "boot.h"

GameWindow *boot_struct::gwd             = NULL;
bool boot_struct::booting                        = false;
lwp_t *boot_struct::pBootScreenHandle   = NULL;
Image *boot_struct::pBootScreenImage   = NULL;
Sprite *boot_struct::pBootScreenSprite    = NULL;

void* boot_struct::BootScreenThread( void *arg )
{
    pBootScreenImage = new Image;
    pBootScreenSprite = new Sprite;

    pBootScreenImage->LoadImage( BootScreen );
    pBootScreenSprite->SetImage( pBootScreenImage );
    pBootScreenSprite->SetStretchWidth( 1.0f/640.0f*width );
    pBootScreenSprite->SetStretchHeight( 1.0f/480.0f*height );

    while( booting )
    {
        WPAD_ScanPads();
		u32 pressed = WPAD_ButtonsDown(0);

		pBootScreenSprite->Draw();
		gwd->Flush();

		if( pressed & WPAD_BUTTON_HOME ) exit(0);
		VIDEO_WaitVSync();
    }

    delete pBootScreenImage;
    pBootScreenImage = NULL;
    delete pBootScreenSprite;
    pBootScreenSprite = NULL;

    return NULL;
}

int boot_struct::ShowBootScreen( void )
{
    booting = true;
    pBootScreenHandle = (lwp_t*)malloc( 1024 ) //sizeof() didn't work for BootScreenThread...
    gwd->InitVideo();
    gwd->SetBackground( (GXColor){100, 100, 255} );
    if( LWP_CreateThread( pBootScreenHandle, BootScreenThread, NULL, NULL, 0, 80 ) < 0 ) return 0;

    return 1;
}

int boot_struct::HideBootScreen( void )
{
    booting = false;
    LWP_SuspendThread( *pBootScreenHandle );
	gwd->StopVideo();
    delete pBootScreenHandle;
    pBootScreenHandle = NULL;
    return 1;
}



Edited 2 time(s). Last edit at 11/26/2008 03:52PM by YoshiParty.
Re: CreateThread() crashs my Wii
December 02, 2008 01:08PM
Where exactly the crash occurs? Try to locate the function (and line) where it happens and it's easier for yourself or anybody following this forum to see what's going wrong. If you don't have USB Gecko, debug prints to console and waiting afterwards works jolly good as well.
Sorry, only registered users may post in this forum.

Click here to login