Click here to Skip to main content
15,887,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had the following code that worked fine on Windows, but would crash on Linux with a segmentation fault.

{
char memory[1024];
Object(memory);
}


Object would use the memory for its operations. I have two theories about why the crash happened:

1) Destructor call order is different in Linux and Windows, on Windows Object destructor is called before making changes to "memory".
2) Linux marks the "memory" space in some manner that doesn't allow Object destructor to work properly.

I have already fixed the problem with new and delete, but would like to know why this problem manifested itself on one platform and not the other. Used compilers are C++ (GCC 4.2.3 x86) for Linux and your friendly neighborhood VS compiler 6.0. for Windows.
Posted

C++ destructor call order is specified by the C++ standard. That will not be operating system specific.
 
Share this answer
 
Comments
AthosU 29-Oct-10 4:03am    
Thanks for the answer, I'd still like to find out the reason for this. That certainly rules out one option.
Dalek Dave 29-Oct-10 4:28am    
Good call.
Order of destruction isn't up for grabs by compiler writers - it's set by the standard. If you have two objects a and b, declared as:

A a;
B b;


then the destructor of b is always going to be called first. So if b depends on a it will always be destroyed before its dependent object.

So what you've got to ask yourself is... what dodgy practice are you doing that will kill the process? I'd be more inclined to say that VC++6 is likely to be the culprit - it's an antique compiler that isn't particularly good at doing standard things and lets you get away with all sorts of rubbish that a relatively modern compiler (like gcc 4.x) won't.

So...

- crank up the warnings on both compilers to /W4 /WX
- get a new version of VC++ - download the express edition and try compiling with that
- start writing unit tests to find out where you class goes wrong and under what circumstances. e.g. test the behaviour of the class when passed a randomly filled array of characters, a zero pointer, a pointer to an array of all zeros.

I can't be anymore specific than that as you haven't described what "Object" does except for taking a block of characters for some nefarious end.

Cheers,

Ash
 
Share this answer
 
Comments
AthosU 29-Oct-10 7:00am    
'Object' takes other parameters as well, but I simplified it for the sake of making the question clear. And this is what caused the problems so other stuff is irrelevant. Thank you for the good answer.
This was in fact caused by the stack being too small.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900