Re: NSLog fixes output, but why?
Re: NSLog fixes output, but why?
- Subject: Re: NSLog fixes output, but why?
- From: Greg Parker <email@hidden>
- Date: Wed, 18 Mar 2009 16:00:15 -0700
On Mar 18, 2009, at 3:05 PM, James Maxwell wrote:
I've got a really frustrating, and really silly problem.
I have some fairly complex machine learning code I'm working on.
I've noticed inconsistent output from a particular method. I'm doing
some fairly nasty array and matrix stuff, which is all done in C,
and I pass the arrays around wrapped in NSData objects. What's
really strange is that, in this particular method, if I place an
NSLog after calling and unwrapping a particular 2D matrix, then the
output is as expected. If I remove the NSLog, the output is
incorrect (or at least, unexpected). The system is pretty complex,
and involves 4 different classes, so I won't post it here, and
obviously I don't expect any magical help. But does anybody have any
experience with something this flaky? Whereby literally adding a
single NSLog (and thus obviously slowing things down a fair bit)
makes the method run correctly?... It's just bizarre, to me.
Maybe somebody here has worked their way into a similar corner?
Obviously I've done something stupid somewhere, but I don't really
know how to go about finding it. It was weird enough to realize that
the NSLog could make or break my output - tracking down the actual
reason why is just baffling... Everything being done in these
classes is basically procedural stuff, so it doesn't seem like
timing should dramatically influence the output. But I am doing
*lots* of iterations over this data. Does that suggest anything?
Bugs that appear or disappear when you change things like logging
statements are one of the following.
* If your program is multithreaded, you may have a thread race. The
logging statement then changes the thread timing so it works (or not).
* You may have an uninitialized variable. The logging statement
changes the value that the variable eventually gets.
* You may have a memory error (malloc/free or retain/release). The
logging statement allocates memory internally and changes the results
of the memory error.
* You may have tripped over a compiler error. This almost never
happens; don't blame the compiler unless you know that the generated
assembly code doesn't match your C code.
Solutions include:
* Turn on as many compiler warning flags as you can. Then fix the
warnings. Do this in your Release build, because gcc doesn't generate
"uninitialized variable" warnings in Debug.
* Run your program with NSZombie and/or Guard Malloc. http://developer.apple.com/technotes/tn2004/tn2124.html
* Run the Clang Static Analyzer on your code. http://clang.llvm.org/StaticAnalysis.html
* Run Valgrind on your program. http://www.sealiesoftware.com/valgrind/
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden