Archived

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

Recommended Posts

I''ve been over this problem for hours and it simply breaks every rule of logic I throw at it. Here''s a simplified code sample, in orther words with all code before and after removed .

class CSlawModel
{
private:
char *name;
public:
__inline const char *GetName()
{
return name;
}
};

class CSlawModel
{
public:
CSlawObject *Objects;
CSlawObject *GetObject()
{
return Objects;
}
};

void SomeFunc(CSlawModel *Model)
{
CSlawObject *Object1=Model->Objects;
CSlawObject *Object2=Model->GetObject();
/*at this point Object1=the correct hex address, checked by debugging where as Object2=0x100*/
const char *String=Object1->GetName();
/*this call fails and in debug mode when it steps into the function GetName the ''this'' pointer is set to 0x100*/
}

 
Can anyone tell me WTF is happening. I should say that the way I''ve represnted the code sample is the EXACT way the code is in my actual however since sending the actual code would require sending most of the engine I''m writing which is very big I figured I''d just send this. From what I can tell my compiler is stuffing up, maybe it''s time to upgrade.

Share this post


Link to post
Share on other sites
a.) Your code example is flawed: both classes have the same name.
b.) You don''t show allocation or pointer assignment, so we don''t know if that''s the problem.
c.) You don''t describe the problem (error code, compiler message, application behavior).

Ergo, you haven''t said anything. The first step in getting help is describing your problem correctly and adequately.

Share this post


Link to post
Share on other sites
Also using a subject more descriptive than ''wtf''.

Why do I have to swear in my head when I read about your problem? I don''t use language like that myself and it isn''t really appropriate that it is used here. Of course you can if you want to but it would be inappropriate. And inappropriate behaviour makes me think you need some extra help. Something stronger than retail therapy.

Anyway, your code problem is that both classes have the same name.

Share this post


Link to post
Share on other sites
i think the first class should be called CSlawObject and its a typo, and his problem is then quite visible when reading the comments..

now you should just describe what you mean by "this call fails", and what your compiler/debugger tells you what fails. because they normally explain quite detailed what the problem is.

is it possible that your CSlawObject* Objects in the CSlawModel class never really gets initialised, meaning it does not have objects in? because it looks like thats what you try to do. you return a pointer to objects, wich does possibly not point to anything useful. then you want to call GetName() of such an object, wich results in an error. this pointer gets passed, but still points to an invalid object, then it tries to access CSlawObject::name to return that => accesses the invalid this pointer and voilà, blam, crash...

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
quote:
Original post by davepermen
i think the first class should be called CSlawObject and its a typo, and his problem is then quite visible when reading the comments..

quote:
Original post by davepermen
now you should just describe what you mean by "this call fails", and what your compiler/debugger tells you what fails. because they normally explain quite detailed what the problem is.

Aren''t those contradictory statements - right after one another, no less? Help the kid learn to think, not be sloppy and have other people do all his reasoning for him. Yes, it''s fairly obvious what the class names should be, but that still doesn''t give us enough information to determine what "fails" and why.

Your hypothesis-laden follow-up paragraph illustrates exactly my point: we''re left guessing, which is a waste of our time as the problem isn''t interesting enough to warrant that kind of examination and discussion.

-1, Needlessly standing up for the so-called "downtrodden"

Share this post


Link to post
Share on other sites
RamboBones: if the code is exactly as you say (except for the typo) then you there are a couple of places that you may have problems. You have public data. So anything could modify it. You have no constructors so the data isn''t initialised when the class is made. It could be set somewhere else but we wouldn''t know because of public data.

Make the data private. Make constructors. That''ll get you moving in the right direction.

Share this post


Link to post
Share on other sites
RamboBones wrote:


    
CSlawObject *Object1=Model->Objects;
CSlawObject *Object2=Model->GetObject();
/*at this point Object1=the correct hex address, checked by debugging where as Object2=0x100*/
const char *String=Object1->GetName();
/*this call fails and in debug mode when it steps into the function GetName the 'this' pointer is set to 0x100*/



If this is truly representative of the real problem code, then the most likely explanation is that the debugger is confusing Object1 with some other variable. This does happen with some debugging tools, trust me.

It may be worth checking the value of Model->Objects in the debugger, but that may also be reported incorrectly. To know for sure, output the value of Object1 is some reliable way, eg.



  
union { CSlawObject* obj; int i; } value;
value.obj = Object1;

FILE* const file = fopen("debugLog.txt", "wt");

char buffer [256];
sprintf(buffer, "Object1: %d\n", value.i);

fputs(buffer, file);
fclose(file);


Good luck.

[edited by - Osc on March 7, 2003 8:56:02 AM]

Share this post


Link to post
Share on other sites
Okay, I would like to appologize for the double naming, really, that was a bad type on me. Osc pointed out the true problem and to answer his question, in the debugger the value for Objects is set to say 0x086543, nice number to , and then Object1 will get set that and Object2 will get something else, but if you look at the inline you''ll see it should return the same thing yet it doesn''t. I actually looked at the produced assembler code and say that it set eax, the return value, with ''mov eax, [eax+14h]'', now after checking out the addresses in the debugger I found that eax+14h was NOT the same as the actual address of Objects. What I''m trying to say is that something in the inline function GetObject is just plain stuffing up. I personally think that it''s just a bug in my compiler, I''m going to try and change the function to noninline and see if that works, thanks for your help anyway guys, not that I think there really was much you could do.

By the way, when faced with a problem such as this that is just completely illogical I was getting very angry which explains the ''wtf'' in the title, sorry guys.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
based on the code shown, and in addition to class name being a duplicate, I see the following problems;
1. the function GetObject is defined to return CSlawObject but you are returning a pointer to the object.
2. the Objects is not allocated.

Share this post


Link to post
Share on other sites
I thouched it because I had a bmp loader and the sizes of BMP headers can''t be 8 bit aligned otherwise it loads all wrong. So I did the lazy way out and changed struct member aligining. I''ve now fixed so that I manually put the sizes in.

Share this post


Link to post
Share on other sites
Strange, i''ve never had that problem, ever, no matter what types i use in the structures. It would seem your compiler is seriously borked if it gives you that sort of trouble.

--
MFC is sorta like the swedish police... It''''s full of crap, and nothing can communicate with anything else.

Share this post


Link to post
Share on other sites