NSLog Changes Value

Marjock
Unregistered
 
Post: #1
Okay, is there any reason at all that would cause NSLog to change one of the values stored in my variable?

Code is as follows:

Code:
-(void)drawFrontGrid
{    
    
    NSLog(@"Hallo");
    
    frontLineCount = [frontLines floatValue];

    NSLog(@"%f",frontLineCount);

}


When it's exactly the code above NSLog prints out Hallo and 0.0 repeatedly.

However, when I delete or comment out the NSLog(@"Hallo"); it prints out -50.0 repeatedly. Not 0.0 like you'd expect, but -50.0.

I just can't think of any reason at all why this should be the case. I don't like to immediately blame XCode when something goes wrong, but this definitely feels that way =/

Cheers,
Mark
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #2
This is not an xcode problem, this is most likely a stack smasher or some over overwriting problem.

See what happens if you do this:
Code:
-(void)drawFrontGrid
{    
    
    NSLog(@"Hallo");
    
    frontLineCount = [frontLines floatValue];
    NSLog(@"member var: %f",frontLineCount);

    NSLog(@"direct: %f",[frontLines floatValue]);
}

I assume frontLineCount is a member variable of your class?

There's a good chance, also, that frontLines hasn't been retained and is giving you garbage.
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #3
TomorrowPlusX Wrote:There's a good chance, also, that frontLines hasn't been retained and is giving you garbage.

It's possible, but wouldn't that just crash the application?

"When you dream, there are no rules..."
Quote this message in a reply
Marjock
Unregistered
 
Post: #4
Code:
I assume frontLineCount is a member variable of your class?

There's a good chance, also, that frontLines hasn't been retained and is giving you garbage.

I declare frontLineCount like so:

staticfloat frontLineCount;

inside my class, so I think that means yes, it is.

Should I be retaining frontLines or should Cocoa be doing that for me? I've definitely gotten values from a textfield before without me having to retain anything but I guess that was a simpler example.

Anyway, when I ran your function I got some very interesting results.

When I run it with the "Hallo" NSLog there it prints 0.0 for both. However, when I run it without the "Hallo" NSLog it prints -50.0 for the member variable and a very long number for direct. I'm sure the very long number isn't important, but just to give a feel for how long we're talking:

Code:
2005-10-28 15:49:03.506 MapMaker[1398] member var: -50.000000
2005-10-28 15:49:03.507 MapMaker[1398] direct: 25625606973858109357852279576246345871697204153368949783554081122486329394064783​79214073003168732372580392664747007492438156513483014118877236831756255682887680​00.000000


I don't know if that sheds any light on the problem?

Cheers,
Mark
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #5
Try retaining frontLines, don't forget to release it when you don't need it anymore or you'll be leaking memory Smile

"When you dream, there are no rules..."
Quote this message in a reply
Marjock
Unregistered
 
Post: #6
Sadly, this:

Code:
-(void)drawFrontGrid
{      
    [frontLines retain];
    //NSLog(@"Hallo");
    
    frontLineCount = [frontLines floatValue];
    NSLog(@"member var: %f",frontLineCount);
    
    NSLog(@"direct: %f",[frontLines floatValue]);
    
    [frontLines release];
}


Just produced the same result as before. (I also tried having the retain message in my -(void)awakeFromNib and the release message in my -(void)dealloc - still no luck)

There's a high chance I'm still doing something wrong with retaining it. Memory Management is really something I need to get a better understanding of =/

-Mark
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #7
Could you post a zip of the project so we can take a look?
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #8
Marjock Wrote:There's a high chance I'm still doing something wrong with retaining it. Memory Management is really something I need to get a better understanding of =/

-Mark

Yes, retain it when you create the object, ie

frontLines = [[NSNumber number] retain];

Assuming frontLines is an NSNumber, change if not.

"When you dream, there are no rules..."
Quote this message in a reply
Marjock
Unregistered
 
Post: #9
Gah, sorry, I completely failed to make it clear that frontLines is an outlet. (connected to an NSTextField).

Does the same thing still apply?

-Mark

Edited to change the mistyped world "NSTextView" to "NSTextField".
Quote this message in a reply
Member
Posts: 114
Joined: 2005.03
Post: #10
Are you sure it's a NSTextView? That doesn't have floatValue. Or do you use an NSTextField (which does have this function)?

Apart from that, as far as I know, there is no need to retain anything that is an outlet connected in Interface Builder. At least, I never had any problems without retaining.

Does changing the type of frontLineCount to normal float change anything?
Quote this message in a reply
Marjock
Unregistered
 
Post: #11
Sorry, I did mean NSTextField, the post has now been edited.

And no, changing the static float to float doesn't change anything.

Thanks,
Mark
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #12
is this a multithreaded app?
Quote this message in a reply
Marjock
Unregistered
 
Post: #13
It's not multithreaded to the extent of my knowledge, heh.

I'm trying to upload the project but my only means of doing so is Geocities which (surprise, surprise) is being uncooperative. However hopefully I'll get that sorted soon.

Cheers,
Mark
Quote this message in a reply
Marjock
Unregistered
 
Post: #14
Okay, I've figured out Virgin's freespace, so the project should be able to be downloaded from:

http://freespace.virgin.net/neil.thomson...roject.zip

Everything that I've quoted in this thread has been from Surroundings.m but obviously feel free to search through the rest of it.

Cheers heaps,
Mark
Quote this message in a reply
Marjock
Unregistered
 
Post: #15
Heh, sorry to triple post, but:

I've just ran my program through malloc, and it identifies a memory leak. However, I have no idea what its actually telling me or how I'd go about fixing it.

Address:
0x2ce7b0

Size:
20

Zone:
DefaultMallocZone

I don't know if that's helpful or not. But either way, would somebody mind telling me what malloc debug is actually telling me?

Thank you all,
Mark
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  A Little Problem with NSLog (and long time no see) Justin Brimm 3 3,396 Oct 17, 2005 06:42 PM
Last Post: Steven