Thanks everybody for your comments!

I'll go through them all in detail tonight. I'm still a bit unsure of myself when dealing with raw C in my Obj-C code (came by way of Java, not C), so this is all a bit of a learning curve... I did actually throw the execution of this particular chunk of code into its own thread this morning, so it's entirely possible that the "thread race" situation is the going to be the answer. The problem is that I've always had NSLogs in this particular code, so there's never been an opportunity to see it running without them. Funny catch-22, really; if I pop in an NSLog to see what's going on, it works fine. When I take out the NSLog, it goes whacky again!

Whatever it is, I certainly have a much better chance of finding it now.
I'll post the solution when I find it, just to close the thread for future users.

cheers,

J.


On 18-Mar-09, at 4:00 PM, Greg Parker wrote:


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     gpar...@apple.com     Runtime Wrangler



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to