DirectDraw Object as a parameter

Started by
12 comments, last by tifkagh 22 years, 3 months ago
quote:Original post by Magmai Kai Holmlor
this is null or uninitialized

create the object before you use it...

Edited by - Magmai Kai Holmlor on January 1, 2002 4:46:29 PM


Yep, I think Magmai has it! He means the this pointer is null as in you did not create the object that your are calling init() on.

i.e.

GraphicsEngine *graphics = NULL;
graphics->init(directDraw);


The GraphicsEngine object has not been created so when your init() method tries to assign a value to one of the class date members an exception is thrown as the program has tried to access an invalid memory location.

Make sure you create the object before you use it.

i.e.
graphics = new GraphicsEngine()

and make sure you clean it up after you are finished with it:
delete graphics;

At least that seems like the problem, because there's no other reason that I can think of that an assignement would throw an exception.

EDIT:

- Kaijin

Edited by - Kaijin on January 3, 2002 8:43:00 AM
Advertisement
actually

GraphicsEngine *graphics = NULL;
graphics.init(directDraw);

will give you a compiler error

GraphicsEngine *graphics = NULL;
graphics->init(directDraw);

is the way to go, this will cause an exception and should be something like

GraphicsEngine *graphics = new GraphicsEngine;
graphics->init(directDraw);
delete GraphicsEngine; // when done with the object



Edited by - kwizatz on January 3, 2002 2:15:19 AM
GraphicsEngine* Graphics = NULL;
Graphics->Init(...)
should cause the exception before it ever goes inside the Init function though, shouldn''t it? YOu can''t dereference a NULL pointer!

-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
NOTE: This is just my theory so don''t hold me to this

The pointer isn''t deferenced until it tries to access the data. Remember objects each have their own copy of the data but not of the functions so nothing needs to be dereferenced when a method is called.

e.g.
  class Example {  public: void method() {    i = 10;  }  private: int i;}  


When you call method() it is implicitly passed a this point so the method is really:

        void method(Example *this) {  this->i = 10;}  


So a problem won''t occur until it tries to dereference this.

The compiler doesn''t need a pointer to find the method only to locate the data. I suspect that this will be different for virtual methods.


- Kaijin

This topic is closed to new replies.

Advertisement