   Access Violations?
  So I'm trying to code a console for my project and I've run into this access violation-First-chance exception at 0x00411aff in Space Project.exe: 0xC0000005: Access violation writing location 0x00000018.Unhandled exception at 0x00411aff in Space Project.exe: 0xC0000005: Access violation writing location 0x00000018.This is the last call in the stack-Space Project.exe!CConsole::Initialize(IDirect3DDevice9 * pDevice=0x00161240, IDirect3DSurface9 * pTargetSurface=0x001609c0)  Line 92 + 0x6 bytes[/Source]and this is where it stops in the Initialize function-HRESULT CConsole::Initialize (LPDIRECT3DDEVICE9 pDevice, LPDIRECT3DSURFACE9 pTargetSurface ){if( !pDevice )return E_FAIL;if( !pTargetSurface )return E_FAIL;HRESULT r = 0;=&gt;m_pDevice = pDevice;   ...}I been researching and trying to figure out what's wrong and I've come to the conclusion that the this pointer is not initialized because in the Autos tab of the debugger it lists all the member vars of the console class like this:m_pDevice=???    |CXX0030: Error: Expression could not be evaluated.How would I go about fixing this? I'm stumped. In the constructor I have the member variables initialized to 0 or Null. I was going to call p_Console->Initialized( g_pD3DDevice, bbuffer )  with in the main().[Edited by - Gonefate on September 6, 2009 4:31:09 PM]
  The key item from the error message is thisAccess violation writing location 0x00000018.This tells me that you've got a null pointer. It's probably a pointer to a structure or an object. The exception is likely thrown when the code attempts to dereference a member at offset 0x00000018. That 24 bytes, so you're looking at the 6 member of the structure or class. If you've traced the exception to this assignment=>m_pDevice = pDevice;If m_pDevice is found at an offset 24 bytes into CConsole, it might be that the instance of CConsole used to make the call is invalid. How can you invoke the method of an invalid object? It might be that methods are class based rather than instance based.   ;  First, you're right it is a class.What could cause the object of the class to be invalid?This is what I coded:CConsole * g_pConsole;a global CConsole type object pointer. Is that not correct in some way?just for future reference, how do you get 24 bytes from 0x00000018?I wasn't sure what you meant by:"It might be that methods are class based rather than instance based."EDITOH WOW I just realized my mistake. It was so simple. The implementation of the class and declaration of the g_p was fine. I just forgot to create an object to assign the address to the pointer. Sorry for taking up your time.[Edited by - Gonefate on September 6, 2009 4:22:04 PM];Quote:Original post by GonefateFirst, you're right it is a class.What could cause the object of the class to be invalid?This is what I coded:CConsole * g_pConsole;a global CConsole type object pointer. Is that not correct in some way?Aren't you supposed to use operator new when instancing a pointer to an object? Doesn't thisCConsole * g_pConsole;merely set aside memory for the pointer but not memory for the object pointed to? Quote:Original post by Gonefatejust for future reference, how do you get 24 bytes from 0x00000018?0x18 is hexadecimal equivalent to 24 in decimal. 0x10 + 0x8 = 16 + 8 = 24The calculator that comes with windows will also convert from hexadecimal to decimal. A pointer points to a location in the address space of a process. In 32 bit windows that address space is 4 Gb. The largest number that can be expressed in 32 bits is 4+ billion (2^32 = 4294967296). A pointer holds an address in the address space. For example, 0x00411aff (or 4,266,751) is the address of the 4266751 byte in that address space. 0x00411afe is the address of the 4266750 byte. 0x00411afd is the address of the 4266749 byte. 0x00411afc is the address of the 4266748 byte -- and so on. A byte is the smallest addressable piece of memory. The address space of a process is virtual. It's not actually backed up with 4 Gb of ram or hard drive. The upper half of that 4 gb is off limits to user mode programs. Most of the rest is empty. 0x00000018 is 24 bytes away from 0x00000000. A pointer set to 0x00000000 is known as a "null pointer". Quote:Original post by GonefateI wasn't sure what you meant by:"It might be that methods are class based rather than instance based."CConsole * g_pConsole; This declares a pointer to an object of the CConsole class. Although there may not be an instance of that class, there is a definition of that class that includes the code for the methods of that class. The compiler has access to that definition. During compilation it converts the invocation of g_pConsole->Initialize into a call to the address where the code for that method is found. When the program runs, it doesn't barf on g_pConsole->Initialize because it has an address where that code can be found. It barfs on m_pDevice = pDevice; inside of Initialize because it can't find a valid address where g_pConsole->m_pDevice can be found. Class methods belong to the class. Each instance of a class shares them with the other instances of that class. Two different objects of the same class don't each get their own copy of the code that goes into the methods of the class. There is one section of code for the methods of the class and objects of that class share it. What is unique to each object is it's particular data. There are exceptions to the class model I described here but that's the basics. ;Quote:Original post by GonefateFirst, you're right it is a class.What could cause the object of the class to be invalid?This is what I coded:CConsole * g_pConsole;a global CConsole type object pointer. Is that not correct in some way?Try this: Draw an arrow on a piece of paper. Now go fetch the wristwatch it is pointing at.   ;  Yeah, I get it now. Thanks LessBread. Good detailed explanation. I felt like a complete moron when I realized I didn't create an object in the first place, just the pointer.I have another question while I'm at and I'd rather just keeping posting the same thread.I'm trying to load a bitmap named Bob_loblaw.bmp using (HBITMAP)LoadImage( NULL, PathName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION );However, I can only get the image to load using "C:\\Bob_loblaw.bmp" as the pathway. I want to organize future images in subfolders and such like:C harddrive->Office folder->Backroom folder->Bob_loblaw.bmpI thought I would put something like, "C:\\Office\\Backroom\\Bob_loblaw.bmp" However that does not work. What is the format and syntax for providing a pathway to a file in C++.   ;  A C++ purist will tell you that the language says nothing about files and paths. HOWTO: How To Use LoadImage() to Read a BMP FileSee also LoadImage "If the function fails, the return value is NULL. To get extended error information, call  GetLastError." See Retrieving the Last-Error Code for info on how to convert error codes into meaningful information.   