views:

505

answers:

4

Ok I finally found the problem. It was inside the C function(CarbonTuner2) not the objC method. I was creating inside the function an array of the same size as the file size so if the filesize was big it created a really big array and my guess is that when I called another function from there, the local variables were put on the stack which created the EXC_BAD_ACCESS. What I did then is instead of using a variable to declare to size of the array I put the number directly. Then the code didnt even compile. it knew. The error wassomething like: Array size too big. I guess working 20+hours in a row isnt good XD But I am definitly gonna look into tools other than step by step debuggin to figure these ones out. Thanks for your help. Here is the code. If you divide gFileByteCount by 2 you dont get the error anymore:

//  ConverterController.h

# import <Cocoa/Cocoa.h>
# import "Converter.h"

@interface ConverterController : NSObject {

 UInt64 gFileByteCount ;
}

-(IBAction)ProcessFile:(id)sender;
void CarbonTuner2(long numSampsToProcess, long fftFrameSize, long osamp);

@end

//  ConverterController.m
# include "ConverterController.h"

@implementation ConverterController

-(IBAction)ProcessFile:(id)sender{

    UInt32 packets = gTotalPacketCount;//alloc a buffer of memory to hold the data read from disk.

 gFileByteCount=250000;
 long LENGTH=(long)gFileByteCount;
 CarbonTuner2(LENGTH,(long)8192/2, (long)4*2);
}
@end

void CarbonTuner2(long numSampsToProcess, long fftFrameSize, long osamp)
{
 long numFrames = numSampsToProcess / fftFrameSize * osamp;
 float g2DFFTworksp[numFrames+2][2 * fftFrameSize];
 double hello=sin(2.345);
}
A: 

For what it's worth, I don't include math.h in my Cocoa app but have no problem using math functions (in C).

For example, I use atan() and don't get compiler errors, or run time errors.

Can you try this without including math.h at all?

Jeff Hellman
+3  A: 

Objective C is built directly on C, and the C underpinnings can and do work.

For an example of using math.h and parts of standard library from within an Objective C module, see:

http://en.wikibooks.org/wiki/Objective-C_Programming/syntax

There are other examples around.

Some care is needed around passing the variables around; use the C variables for the C and standard library calls; don't mix the C data types and Objective C data types incautiously. You'll usually want a conversion here.

If that is not the case, then please consider posting the code involved, and the error(s) you are receiving.

And with all respect due to Mr Hellman's response, I've hit errors when I don't have the header files included; I prefer to include the headers. But then, I tend to dial the compiler diagnostics up a couple of notches, too.

Stephen Hoffman
I guess my question would be why include math.h at all for calls to sin() or cos() which are well defined in fp.h and easily usable in any Cocoa application? Perhaps there are other functions the original poster needs, but for sin() and cos() I'd just go with the default.
Jeff Hellman
Yes the sin and cos functions do seem to work even though no libraries are included in an objective c file but in a c file you will get "implicit function used" warnings....back to testing..and btw my program got the error even when i use something stupid like: cos(2.0) and also sometimes I wouldnt get crashes when i dont assign the returned value.
yan bellavance
Parts of this code look to have been acquired from...http://developer.apple.com/samplecode/PlayAudioFileLite/listing1.html...though pieces (such as where gTotalPacketCount is set) seem missing from what was posted here...I'd look to start reducing the C and Objective C code until the bug was located or working through the code in the debugger; C heap and stack corruptions (buffer overruns, bad pointers, uninitialized or wrong values) can manifest themselves in some interesting ways....This regardless of whether the header file is present. Or not.
Stephen Hoffman
+3  A: 

Your crash has nothing to do with incompatibilities between C and ObjC. And as previous posters said, you don't need to include math.h.

Run your code under gdb, and see where the crash happens by using backtrace.

Are you sure you're not sending bad arguments to the math functions?

E.g. this causes BAD_ACCESS: double t = cos(*(double *)NULL);

diciu
A: 

First, you should add your code to your question, rather than posting it as an answer, so people can see what you're asking about. Second, you've got all sorts of weird problems with your memory management here - gFileByteCount is used to size a bunch of buffers, but it's set to zero, and doesn't appear to get re-set anywhere.

err = AudioFileReadPackets (fileID, false, &bytesReturned, NULL,0, &packets,(Byte *)rawAudio);

So, at this point, you pass a zero-sized buffer to AudioFileReadPackets, which prompty overruns the heap, corrupting the value of who knows what other variables...

fRawAudio = malloc(gFileByteCount/(BITS/8)*sizeof(fRawAudio));

Here's another, minor error - you want sizeof(*fRawAudio) here, since you're trying to allocate an array of floats, not an array of float pointers. Fortunately, those entities are the same size, so it doesn't matter.

You should probably start with some example code that you know works (SpeakHere?), and modify it. I suspect there are other similar problems in the code yoou posted, but I don't have time to find them right now. At least get the rawAudio buffer appropriately-sized and use the values returned from AudioFileReadPackets appropriately.

Mark Bessey
I think you're missing my point. I am addressing your problem. I'd bet almost any amount of money that you're overrunning a buffer somewhere in your code, and that's the cause of your problem. Since you haven't posted all of the code, telling you that your audio buffers are most likely the wrong size at some point is really all anybody can do to help you.
Mark Bessey
Ok here is what I am going to do....now that I have a finger on the issue I will try to recreate it with a simpler app to show whats happening (I might in the process find my mistake). Do you know though if anything special happens to the stack when you call a C function from within a C function from within an obj-C function?(the problem occurs without even touching the buffer...if the data being pointed is less than a certain quantity i dont get the problem anymore
yan bellavance
Objective-C methods are really just C functions with an additional hidden parameter, which is set to "self". So, there's no reason why calling your code from an Objective-C method would be any different than calling it from another C function. The Objective-C runtime's objc_msgSend function does not create a stack frame, so that's a little odd, but isn't related to your problem. I think recreating the problem with a smaller code sample is the way to go.
Mark Bessey
Ok I will do that later on tonight as soon as im done working on something else
yan bellavance
Hey I finally found the problem. I was declareing an array of the same size as the array being pointed and that was creating the bad access. Since I was sending the size of the input array it would create a big array in that function.
yan bellavance