Trying to pass an SDL_Surface to an object constructor

I thought I understood pointers, but I must have missed something.

I’ve got my main program with an ‘SDL_Surface* screen’ which successfully
paints to the screen. Now I’ve made a class called Painter, with a surface
pointer in it and the constructor takes a pointer as an argument, the idea being
that I can pass the screen pointer to it so that it has a pointer to it. Doing
this compiles, but gives a seg fault when the Painter class tries to use this
pointer. The original pointer works so I can only assume that my assignment
hasn’t worked as expected.

The screen is declared in the main program like this

SDL_Surface *screen;

and I create the Painter like this

Painter p(screen);

This is how the Painter constructor assigns the pointer to its pointer *s.

Painter::Painter(SDL_Surface *screen)
{
s = screen;
}

Can anybody see where I’ve got it wrong? Thanks–
Nick H

What are you doing with the original pointer?

Are you releasing it, by chance?

Nick wrote:> I thought I understood pointers, but I must have missed something.

I’ve got my main program with an ‘SDL_Surface* screen’ which successfully
paints to the screen. Now I’ve made a class called Painter, with a surface
pointer in it and the constructor takes a pointer as an argument, the idea being
that I can pass the screen pointer to it so that it has a pointer to it. Doing
this compiles, but gives a seg fault when the Painter class tries to use this
pointer. The original pointer works so I can only assume that my assignment
hasn’t worked as expected.

The screen is declared in the main program like this

SDL_Surface *screen;

and I create the Painter like this

Painter p(screen);

This is how the Painter constructor assigns the pointer to its pointer *s.

Painter::Painter(SDL_Surface *screen)
{
s = screen;
}

Can anybody see where I’ve got it wrong? Thanks

Nick wrote:

Doing
this compiles, but gives a seg fault when the Painter class tries to use this
pointer.

Paulo Pinto <pjmlp progtools.org> writes:

What are you doing with the original pointer?

Are you releasing it, by chance?

No… is that good or bad? Should the result not just be that screen and s both
point to the same memory location?

Nick wrote:

Nick wrote:

Doing
this compiles, but gives a seg fault when the Painter class tries to use this
pointer.

Paulo Pinto <pjmlp progtools.org> writes:

What are you doing with the original pointer?

Are you releasing it, by chance?

No… is that good or bad? Should the result not just be that screen and s both
point to the same memory location?

My guess is that Painter::~Painter contains a SDL_FreeSurface(s), right ?

Stephane

Stephane Marchesin <stephane.marchesin wanadoo.fr> writes:

Nick wrote:

Doing this compiles, but gives a seg fault when
the Painter class tries to use this
pointer.

My guess is that Painter::~Painter contains a SDL_FreeSurface(s),
right ?

Stephane

No, I haven’t put anything in the destructor as yet, but
that shouldn’t matter because I’m not destroying the Painter,
it doesn’t make a difference if I do that.

I don’t release the original pointer, I just pass it to the
constructor of this Painter class, which assigns it to a
pointer in the class, which should hopefully make it point
to the same surface? Is that correct?

Thanks

Nick

Stephane Marchesin <stephane.marchesin wanadoo.fr> writes:

Nick wrote:

Doing this compiles, but gives a seg fault when
the Painter class tries to use this
pointer.

My guess is that Painter::~Painter contains a SDL_FreeSurface(s),
right ?

Stephane

No, I haven’t put anything in the destructor as yet, but
that shouldn’t matter because I’m not destroying the Painter,
it doesn’t make a difference if I do that.

I don’t release the original pointer, I just pass it to the
constructor of this Painter class, which assigns it to a
pointer in the class, which should hopefully make it point
to the same surface? Is that correct?

As far as I can tell what you are doing is perfectly OK. The most likely
explanation for the problem is some sort of memory corruption. Either
the second pointer is being overwritten of the surface structure is
being overwritten.

There are a lot of libraries that will help you find the problem
(electric fence comes to mind, there are many others) or you can find it
the old fashioned way. Just make a copy of the pointer and the structure
and just before you use either compare the pointer and structure you are
using to the copy (put the test in a subroutine or macro so you can use
it all over the place). That will tell you when the corruption occurs
and you can then fix it.

	Bob PendletonOn Tue, 2004-08-17 at 11:28, Nick wrote:

Thanks

Nick


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

±-------------------------------------+

Nick wrote:

Stephane Marchesin <stephane.marchesin wanadoo.fr> writes:

Nick wrote:

Doing this compiles, but gives a seg fault when
the Painter class tries to use this
pointer.

My guess is that Painter::~Painter contains a SDL_FreeSurface(s),
right ?

Stephane

No, I haven’t put anything in the destructor as yet, but
that shouldn’t matter because I’m not destroying the Painter,
it doesn’t make a difference if I do that.

Wrong. For example, if you pass the Painter object as a parameter to a
function, the default copy constructor is called, and a copy of your
object is passed to the function.
The function executes and at the end the copy of object is destroyed.
But remember that the original instance of the object already points to
the same SDL_Surface, which is now destroyed. So any subsequent access
to the SDL_Surface from the original Painter object now accesses freed
memory.
So calling SDL_FreeSurface(s); or not does make a difference, indeed.

Since we don’t know what you do in the rest of your program, we can only
guess (as usual a full, compilable and preferabily short source code
showing your problem will help others help you).

Stephane

Stephane Marchesin wrote:

So calling SDL_FreeSurface(s); or not does make a difference, indeed.

I don’t think it would in this case.
In my own experiments, I found that SDL_FreeSurface() on the display surface is ignored, and I think we can assume that a surface called screen is the display surface in general.

Which brings me onto my suggestion for Nick: Do you call SDL_SetVideoMode() at all after the pointer is copied?
This creates a new surface, and frees the old one, which would explain why your old pointer becomes invalid.

Just what are you trying to do with the pointer, by the way?

Chris E.
-------------- next part --------------
A non-text attachment was scrubbed…
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20040817/f954e5a8/attachment.pgp

I don’t believe it.

I was getting all worked up about pointers, but I’d stupidly declared the
Painter object at the top, before the surface had even been created. So the
argument passing was fine, but then when the surface was actually created it was
given a memory location.

The only question now is how to create the Painter object after the surface has
been created, while still giving it scope outside of the initialization method?
I wanted to avoid making a setSurface() method so that the Painter could do
stuff specific to the surface, but I may have to throw that dream away!

Apologies for my insistent “no it’s right” nonsense, I should have noticed :slight_smile:

thanks

Nick

Nick <csucew dcs.warwick.ac.uk> writes:

The only question now is how to create the Painter object after the surface has
been created, while still giving it scope outside of the initialization method?
I wanted to avoid making a setSurface() method so that the Painter could do
stuff specific to the surface, but I may have to throw that dream away!

I just called the constructor again after setting the video mode, works fine now.

Let this be a lesson to others who don’t pay attention to what they’re doing :slight_smile:

such are the perils of programming :wink:

Nick wrote:>I don’t believe it.

I was getting all worked up about pointers, but I’d stupidly declared the
Painter object at the top, before the surface had even been created. So the
argument passing was fine, but then when the surface was actually created it was
given a memory location.

The only question now is how to create the Painter object after the surface has
been created, while still giving it scope outside of the initialization method?
I wanted to avoid making a setSurface() method so that the Painter could do
stuff specific to the surface, but I may have to throw that dream away!

Apologies for my insistent “no it’s right” nonsense, I should have noticed :slight_smile:

thanks

Nick


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

I’d say: such are the perils of programming in C++ :stuck_out_tongue_winking_eye:

18.8.2004 kello 01:46, grembo kirjoitti:> such are the perils of programming :wink: