Problem with threads and new in C++

A bit off topic.-----------------------------

Hi,

I found a strange bug, when I was programming my game with SDL. I join at
the end of the message a basic example that reproduce it. The problem is
that the “this” pointer of the object “m” has wrong value during the call to
"m->test" in the method “Juju :: foo()”. When I put a breakpoint in the
"test" method, then run and do a backtrace I can see that the "this"
pointer is corrupted. Usually its value is 0x1. If I don’t link with the
pthread library, it doesn’t happens. But I must link with pthread since I am
using SDL. I also find that if I replace the new by a malloc, the problem
doesn’t occur. The value is also correct if i declare a Mask and not a “Mask
*” in the class Juju, but i don’t want to do this.

This problem happen on a slackware 8.1 with gcc 2.9.5 (I don’t know the
version of the pthread)
I have tested the code below on a Red Hat 8 (psyche) with gcc-3.2 and
pthread 0.10, and it works.

Does anybody has already encounter such an error ? Is there special
compilation option ? I would like to fix this, otherwise my game won’t work
on all linux box. I also hope that I musn’t force people to compile with a
special version of the pthread library or gcc.

I will try with gcc-3.2 ans my current pthread tonight.

thanks

Julien


#include <SDL.h>

class Mask {
public :
Mask (unsigned int width=32, unsigned int height=32);
~Mask ();
bool test();

private:
unsigned char * _mask;
};

class Juju {
public:
Juju();
void foo();
private:
Mask * m;
};

Mask :: Mask (unsigned int width, unsigned int height)
{
_mask = new unsigned char[10000];
}
Mask :: ~Mask() {}

bool
Mask :: test()
{
return 0;
}

Juju:: Juju()
{
m = new Mask(32,32);
}

void
Juju:: foo()
{
m->test();
}

int main(int argc, char** argv)
{
Juju juju;
juju.foo();
}

I found a strange bug, when I was programming my game with SDL. I join at
the end of the message a basic example that reproduce it. The problem is
that the “this” pointer of the object “m” has wrong value during the call to
"m->test" in the method “Juju :: foo()”. When I put a breakpoint in the
"test" method, then run and do a backtrace I can see that the "this"
pointer is corrupted. Usually its value is 0x1. If I don’t link with the
pthread library, it doesn’t happens. But I must link with pthread since I am
using SDL. I also find that if I replace the new by a malloc, the problem
doesn’t occur. The value is also correct if i declare a Mask and not a “Mask
*” in the class Juju, but i don’t want to do this.

You have memory corruption somewhere in your program.
It sounds like something is writing over the end of an array.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

You have memory corruption somewhere in your program.
It sounds like something is writing over the end of an array.

Yes, I was thinking of such a problem, but in the code I sent I don’t access
to an array. Basically, I just create an object and in this object create an
array, but I don’t touch to this array. So I can’t corrupt hte memory there.
In fact, if I create just an “unsigned char” (with new) and not an array,
the problem also occur.

If you overload the constructor for Mask to allow for Mask() and Mask(w,h), do you still have the problem?

Rather than Juju having Mask as a member, you might want to try having Juju inherite Mask. The relationship of Juju and Mask might get a little sketchy with juju creating m with no constructor being used and juju’s constructor creating another Mask object and assigning it to m. Perhaps the different results you’ve seen in the corruption is the order of object creation and such that could also relate to threading. You might want to try adjusting this relationship so either a 0 argument constructor exists for Mask, or have Juju inherite Mask as an object.

  • Joby
    http://jobybednar.com
    Give someone a program - frustrate them for a day.
    Teach them how to program - frustrate them for a lifetime.> ----- Original Message -----

From: slouken@devolution.com (Sam Lantinga)
Reply-To: sdl at libsdl.org
Date: Thu, 30 Jan 2003 07:31:10 -0800

I found a strange bug, when I was programming my game with SDL. I join at
the end of the message a basic example that reproduce it. The problem is
that the “this” pointer of the object “m” has wrong value during the call to
"m->test" in the method “Juju :: foo()”. When I put a breakpoint in the
"test" method, then run and do a backtrace I can see that the "this"
pointer is corrupted. Usually its value is 0x1. If I don’t link with the
pthread library, it doesn’t happens. But I must link with pthread since I am
using SDL. I also find that if I replace the new by a malloc, the problem
doesn’t occur. The value is also correct if i declare a Mask and not a “Mask
*” in the class Juju, but i don’t want to do this.

You have memory corruption somewhere in your program.
It sounds like something is writing over the end of an array.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Thanks for all people that replied to me. I will try to find the bug, with
these informations.

In fact, I have tried yesterday with gcc 3.2 but I want it to compile with
2.95.3

Julien