#### Archived

This topic is now archived and is closed to further replies.

# An reference into an object inside another object?

This topic is 5466 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi This question is a bit weirdm so I'll try to explain it as well as I can. I wrote an code where an object(lets name it C) referes inside another objects(object A) into object(object B). Now why I need to refer to object B inside object A is because I can have only one instance of object B in my programs code(it's hardware releated). Now as it turns out such reference does work, well for a while anyway. I start up the program and start to test it's functionality. Everything goes well, untill after X amount of time the program crashes and the debugger sayes that an NULL reference has been passed blah blah... The weird thing is that when I made an integer component and assigned an value into object B to see if there is really an NULL data beside objects rest of the data. I run the program again same error, enter debbug mode directly to the error and look at this integer value and according to the debbuger the value that I assigned to it is still there. By the way I access object B through object C because there is where I want to use this object B functionality, and what makes it harder to debbug the components in object B is that the components are hardware releated and the debbugger shows me no info regarding their state. Uh... I really don't know how to explain it well. I can go around this, but in a way it would be easier to give an reference to object B inside object A and be able to use the components in object B through an reference in object C. This way I don't have to pass each function an individual reference to the required component in object B. Don't know maybe I have an memmory leak somewhere and during the programs run it overwrites into the same memory address. Ah... I really messed up here and really would like to understand this promblem, so that I won't do the same mistake again. Well if anyone can give some tips and hints thanks, I really need them. Ou yeah the programming language is c++ and the programming tool is VS .NET c++ 2K3 standard.

##### Share on other sites
Let''s see some code and we might be able to help you, mate.

##### Share on other sites
quote:
Original post by Sailorstick
Let''s see some code and we might be able to help you, mate.

Ouh... this is gonna take some time theres a lot of code I need to parse it somehow.

##### Share on other sites
quote:
quote:
Original post by Sailorstick
Let''s see some code and we might be able to help you, mate.

Ouh... this is gonna take some time theres a lot of code I need to parse it somehow.

Well I''ll refer to the code object as A B and C as in my question, maybe it will help to understand which object is which.

This is object B:It''s main idea is to hold the DX9 components that creates the interface between the hardware and the software.#ifndef D3DINTERFACE_H#define D3DINTERFACE_H#include <d3d9.h>		// directx 8 header#include <d3dx9.h>		// d3d render library#include <dinput.h>#include "Common\CommonAlgorithms.h"class DX9INTERFACE{public:	DX9INTERFACE();	~DX9INTERFACE();	LPDIRECT3DDEVICE9			*D3DDEVICE9;	LPDIRECT3D9					D3D;	LPDIRECTINPUT8 				DirectInput;					// Direct Input device with this variable we can control the input data	LPDIRECTINPUTDEVICE8  		DIDeviceKeyboard;				// Keyboard device	LPDIRECTINPUTDEVICE8  		DIDeviceMouse;				// Mouse device	void ReleaseD9XDevices();		int x;};#endif---------------------------------------------------------------Object C:class 3DGraphics{public:void SetD9XInterface(DX9INTERFACE *DX9Interface); // This function sets the reference to object B in object A.void Render(); // render a sceneprivate:DX9INTERFACE *DX9Interface; // Reference to Object B};void 3DGraphics::SetD9XInterface(DX9INTERFACE *DX9Interface){this->DX9Interface = DX9Interface;}---------------------------------------------------------------Object A:Here we create an instance of object B and C. Notice: we give the reference to object C into object B through a function namedI wont put all of the code theres to much. Just the important lines of code.class ProjectEngine{public:void InitDirectX(HWND &g_hWnd, HINSTANCE &hInstance);		// initializing directxprivate:DX9INTERFACE DX9Interface; // Object B3DGraphics GfxD3D;   // OBject C};void ProjectEngine::InitDirectX(HWND &g_hWnd, HINSTANCE &hInstance){this->GfxD3D.SetD9XInterface(&DX9Interface);}----------------------------------------------------------// The connection is done during initialization process and before we start to do anything with the components in object B.// Below is a simple situation where the program crashes after X amount of time(which during this time works perfectly). One of such error occur during th Render function in object C. This error happens inside the Render() function when trying to use one of the components in object B. Notice: the program works fine until X amount of time has passed and crashes. For example the first operation is render function will be to clean the screen and the buffers. When the crash occurs it will be in this line. OR if I would like to retrieve keyboards state as the first operation in Render() function, once again when the crash occurs it will be trying to get the keyboards state.

##### Share on other sites
Hmmmm. Are you initialising all of the members of the DX9INTERFACE class in it''s constructor?
Also, try changing
DX9INTERFACE DX9Interface;
to
DX9INTERFACE* DX9Interface;
and in:
void ProjectEngine::InitDirectX(HWND &g_hWnd, HINSTANCE &hInstance)
{
DX9Interface = new DX9INTERFACE();
this->GfxD3D.SetD9XInterface(DX9Interface);
}

This may do nothing at all. It''s just that I''m not sure how you are supposed to use a constructor when not using pointers.
I thought it was
DX9INTERFACE DX9Interface();
but I might be thinking of Java...

##### Share on other sites
quote:
Original post by Sailorstick
Hmmmm. Are you initialising all of the members of the DX9INTERFACE class in it's constructor?
Also, try changing
DX9INTERFACE DX9Interface;
to
DX9INTERFACE* DX9Interface;
and in:
void ProjectEngine::InitDirectX(HWND &g_hWnd, HINSTANCE &hInstance)
{
DX9Interface = new DX9INTERFACE();
this->GfxD3D.SetD9XInterface(DX9Interface);
}

This may do nothing at all. It's just that I'm not sure how you are supposed to use a constructor when not using pointers.
I thought it was
DX9INTERFACE DX9Interface();
but I might be thinking of Java...

Hmm damn... I think that I forgot to allocate objects B memory with |new| call. Damn... I'll try it. I'll be back

Addition: Yeah I'm setting the components in DX9INTERFACE class to NULL in the classes cunstructor.

[edited by - LionheartAdi on July 3, 2003 6:58:23 AM]

##### Share on other sites
I just realized that it should not be any different do I use
DX9INTERFACE DX9Interface;
or
DX9INTERFACE* DX9Interface;

and then

DX9Interface = new DX9INTERFACE();

If I remember right both do allmost the same thing, but with a little difference. huh... Damn I''m messed up...

##### Share on other sites
Well, try it as it is now, but put a break-point in the DX9INTERFACE constuctor to make sure it is being called. If it isn''t, then try using the pointer and new.

##### Share on other sites
quote:
Original post by Sailorstick
Well, try it as it is now, but put a break-point in the DX9INTERFACE constuctor to make sure it is being called. If it isn''t, then try using the pointer and new.

I think I found my problem. It seems that the code had no problem, the error seemed to be when opening an shader file to be processed. This caused an error during rendering time. I did the error leaving the file loading operation to be called during rendering. Well I think this is the problem, this is what VS .NET showed me after I upgraded the DirectX debug state to a higher leveel. Well have to test on.

• 17
• 11
• 11
• 9
• 49
• ### Forum Statistics

• Total Topics
631394
• Total Posts
2999750
×