473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

program hanging returning from a constructor

I am writing a program for the Palm OS in c++. I have already posted
this to a palm development forum but I'm desperate for a fix, so I hope
this cross-post isn't offensive.

My program is hanging, like it's in an infinite loop, while returning
from constructing an object. Through the debugger, I've found that it
is hanging on the returning call to the constructor. I put a breakpoint
on the closing bracket of the constructor, and it goes past that point
fine. It hangs on the next step, on the returning line, like this

mg = MyGame();

The variable 'mg' is declared in a file of global variables (as type
MyGame) and referenced through an extern reference.

It appears to work fine the first time the mg object is constructed.
Then I re-use the mg reference when I am resuming a saved game. I don't
have a destructor for the MyGame() object, but I never have. I don't do
any memory allocation within the object.

None of this is new, it's been working fine for months. I haven't made
changes to any of this code. I've made changes to other parts of the
program, but nothing in the MyGame object.

Any ideas? Any help would be greatly appreciated. Thanks.

Aug 4 '05 #1
8 3034
> mg = MyGame();

A temporary is getting assigned. The temporary is destroyed immediately
after the execution of the above statement. Depending on what you have
in your class, this _may_ lead to problems. Why don't you do this
instead...

mg = new MyGame;

Regards,
Srini

Aug 4 '05 #2
This could be due to a stack corruption in the constructor function.

When the constructor returns, the return address is pulled from the
stack. If the return address has been corrupted, the function would
crash on return.

The following artciles might also give you some ideas:
http://www.eventhelix.com/RealtimeMa...re_crashes.htm
http://www.eventhelix.com/RealtimeMa..._crashes_2.htm

--
EventStudio 2.5 - http://www.EventHelix.com/EventStudio
Generate Sequence Diagrams in PDF and Word EMF from plain text input

Aug 4 '05 #3
Do you have any operator= method in MyGame class check if it is being
called and if it has any locks.

Aug 4 '05 #4
Srini wrote:
mg = MyGame();


A temporary is getting assigned. The temporary is destroyed
immediately after the execution of the above statement. Depending on
what you have in your class, this _may_ lead to problems. Why don't
you do this instead...

mg = new MyGame;


Expressions 'MyGame()' and 'new MyGame' have different types. In most
cases if the former compiles, the latter won't.
Aug 4 '05 #5
> Expressions 'MyGame()' and 'new MyGame' have different types. In most
cases if the former compiles, the latter won't.


Oh yeah. new returns pointer to the object whereas MyGame() would be a
temporary object. Thanks Victor.

Aug 4 '05 #6
Thanks much for the replies. I found the problem, and it was occurring
else where. I don't know why it appeared to hang coming out of the
constructor. But when I was debugging it again, it got past that
point.

But the replies have me intrigued. Is my code :

mg = myGame();

still incorrect in some way, or have the potential for problems? It
turns out it does work after all, but if there is a better way I'd like
to know.

I tried

mg = new myGame;

and the compiler gave an error of 'illegal operand'.

Aug 4 '05 #7
brifo...@gmail.com wrote:

But the replies have me intrigued. Is my code :

mg = myGame();

still incorrect in some way, or have the potential for problems? It
turns out it does work after all, but if there is a better way I'd like
to know.


Your code will create a new myGame object, by calling the
default constructor. Then, it will assign that object to
'mg' by calling mg's operator= function. Then, the new
object will be destroyed since it is no longer needed.

If you have not written an operator= function for myGame
explicitly, the compiler will generate one for you. But if
myGame has any pointers or other resource handles, then this
compiler-generated operator may not do what you need.
If you post your definition of myGame then we can tell you
if you need to write an operator= function or not.

If you want to avoid this copy operation, you could add a
function to myGame that's designed to clear out an existing
object, eg:
void myGame::clear()
{
member1 = 0;
member2 = "";
// etc.
}

Aug 4 '05 #8
If you feel pretty advanced you could just check the code on another
machine.
But that's a real longshot.
If your code has been truly stable in the past and no changes have been
made in the code where you are seeing the problem:
Try lookin at the assembly language and make some sense out of it.
For example, does the call address for MyGame() actually look
right?
If not, then some corruption occurred.
If so, does it look like the MyGame() code at that address?
The first thing that code of that sort would do, after
calling wrapper functions, is push registers onto stacks. But maybe you
know all of that.
Sometimes libraries are dynamically loaded and
unloaded. That kind of stuff could theoretically become corrupted, but
one would think only inside the OS level.
A true assembly language genious would discover the problem quickly.

As for C++ woof you could try looking for a memory leak. Copy
Constructors are good for that. Somehow your constructor may be
overwriting stack space a bit higher than it. So the return address is
screwed up(on the stack), and thus the address of the line of code will
not actually match what you or your debugger thinks it is. Uh-Oh, a
good debugger would not do this. This is probably all a bad idea.

-Tim

Aug 5 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: JKop | last post by:
unsigned int CheesePlain(void) { unsigned int const chalk = 42; return chalk; } unsigned int& CheeseRef(void) {
4
by: Siemel Naran | last post by:
Hi. I have found one advantage of returning values through the argument list. It's that we have to store the return value. But when we return by value, we may forgot to store the return value. ...
1
by: Tony Johansson | last post by:
Hello!! Assume you have the following AccountForStudent class class AccountForStudent { public: AccountForStudent (); AccountForStudent (long, double); ~AccountForStudent ;
0
by: joeted | last post by:
Hi, I am using system.diagnostic.process with the intention of running a program to communicate with a unix box: The program is "PuTTY", running from a batch script. I know it works because...
19
by: mohammaditraders | last post by:
a program which consists of a class named Student, the class should consists of three data members Name, Ob_marks, Total_marks and two member functions Cal_percentage() which calculate the...
4
by: mike3 | last post by:
Hi. I seem to have made some progress on finding that bug in my program. I deactivated everything in the bignum package that was used except for the returning of BigFloat objects. I even...
14
by: wshaer | last post by:
Hi all, I have an assignment and I need some help with it. This is the assignment // Research reports are often required to conform to a given standard such as APA or MLA. These standards...
3
by: Microsoft | last post by:
Hi I have a c# program that continually runs 24/7 and performs a variety of tasks based on a timer. There is one routine that hangs every Saturday morning without fail. If I restart the...
4
by: JDS | last post by:
I have an application that interfaces with a USB device using the .Net serial port. The code works fine, displaying live data on the screen; that is until the USB lead is pulled out from the PC...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.