Valhalla Legends Forums Archive | C/C++ Programming | C++ OOP Problem

AuthorMessageTime
Mephisto
I finally finished all of my interfaces for my bot and definitions for those interfaces and put it all together. In _tmain(...) it creates an object of BotConnection which calls a member function void *Connect();. For some reason, this causes three runtime errors. The first two errors are: The instruction at <memory address> referenced memory at <memory address>. The memory could not be read., and the third one is a debug error involving an assertion failure. For some reason, this only occurs when I call this member function. I tried just for testing calling functions like Disconnect, Reconnect, and used other objects such as BotLoad and called things like LoadConfig, etc. and everything worked fine. It just seems to be this function, and there's nothing wierd about it. I even tried debugging the program and the error occurs at the function call, NOT in the function...Help?

[code]int _tmain(int argc, _TCHAR* argv[])
{
   BotConnection *startBot = new BotConnection;
   startBot->Connect();
   return 0;
}[/code]
August 13, 2004, 6:06 PM
Eibro
Break in with the debugger when you get one of these runtime errors. Have a look at the stack trace-- it should be pretty obvious where the problem lies.
August 13, 2004, 6:31 PM
Mephisto
After I did some more debugging I actually figured out that it was occuring when BotConnection was being initialized. When I step into the declaration newop.cpp displays as expected containing the following code:

[code]// newop operator new(size_t) for Microsoft C++
#include <cstdlib>
#include <new>

_C_LIB_DECL
int __cdecl _callnewh(size_t size) _THROW1(_STD bad_alloc);
_END_C_LIB_DECL

void *__cdecl operator new(size_t size) _THROW1(_STD bad_alloc)
   {   // try to allocate size bytes
   void *p;
   while ((p = malloc(size)) == 0)
      if (_callnewh(size) == 0)
         _STD _Nomemory();
   return (p);
   }

/*
* Copyright (c) 1992-2002 by P.J. Plauger. ALL RIGHTS RESERVED.
* Consult your license regarding permissions and restrictions.
V3.13:0009 */[/code]

I debugged this code while it executed, when it finishes (and before it gets to the function call in WindowsBot.cpp in _tmain(...) it has to break. And the file that shows up is strlen.asm and it occurs on this line in the file:

[code]main_loop:
mov eax,dword ptr [ecx] ; read 4 bytes (this line)[/code]

I'm sort of confused by this, but perhaps people with more experience with this type of thing aren't. I also tried looking at the constructors and such in BotConnection and everything is fine. I even tried commenting them out to see what would happen and the same error occurs.
August 13, 2004, 7:57 PM
St0rm.iD
[code]
which calls a member function void *Connect();.
[/code]

Looks to me like it should be void Connect();

Should also delete the pointer at the end.

Should also put () after new BotConnection.
August 13, 2004, 10:23 PM
Eibro
If new is failing, check the classes constructor.
August 13, 2004, 10:52 PM
Mephisto
[quote author=Eibro[yL] link=board=30;threadid=8150;start=0#msg75400 date=1092437555]
If new is failing, check the classes constructor.
[/quote]

I did, and like I said I even tried commenting out and the same thing is happening. I also did what $torm suggested and to no avail...

Eibro, I could send you the project on MSN if you'd like (I don't want to publicly post it) and when you have time and you want to look at it, see if you can find the problem. :)
August 13, 2004, 11:43 PM
K
Are you chaining constructors? ie, calling a different constructor from within the first constructor, perhaps with default values?
August 14, 2004, 4:09 AM
Mephisto
Sort of figured it out, pinpointed it to a problem with the constructors when one of the objects multiply derives. It seems that when the object BotConnection is created it calls the constructor of BotLoad and so on up the heirarchy. BotLoad dervies from BotFunctions and Botdata, each have constructors, and it seems like the constructor in BotFunctions is being called twice, and on the second call it causes that error. I commented out the code in the BotFunctions constructor and just made it a default constructor, and the same thing continued to happen. Anyways, Eibro is going to help me debug it later, we'll see if that can fix it up. :)
August 14, 2004, 4:21 AM
Sargera
Problem fixed. To think how many problems you can solve by paying attention to compiler warnings, and not just errors. :)
August 15, 2004, 4:20 AM
Skywing
[quote author=Sargera link=board=30;threadid=8150;start=0#msg75553 date=1092543626]
Problem fixed. To think how many problems you can solve by paying attention to compiler warnings, and not just errors. :)
[/quote]
An easy way to make sure you do that is to build with /W4 /WX.

[quote]
/W<n> set warning level (default n=1)
/WX treat warnings as errors
[/quote]
August 15, 2004, 3:08 PM
Sargera
[quote author=Skywing link=board=30;threadid=8150;start=0#msg75591 date=1092582503]
[quote author=Sargera link=board=30;threadid=8150;start=0#msg75553 date=1092543626]
Problem fixed. To think how many problems you can solve by paying attention to compiler warnings, and not just errors. :)
[/quote]
An easy way to make sure you do that is to build with /W4 /WX.

[quote]
/W<n> set warning level (default n=1)
/WX treat warnings as errors
[/quote]
[/quote]

Thanks, this should not only help avoid more runtime errors but bad coding as well. :)
August 15, 2004, 6:00 PM

Search