views:

294

answers:

3

Hi, in my application I am getting an exception: MyApp(38431,0xa004ffa0) malloc: *** error for object 0xa08be760: pointer being freed was not allocated

Is there a way for XCode to show me where this pointer is? How can I find where the problem is?

I know that I can type info line*[address] and it may show me where the line is. But now it doesn't work for me.

How can I get a number of line or name of a variable where exception is being thrown?

Thank you in advance, Ilya

+2  A: 

You're calling free() without calling malloc(). Look for uninitialized pointers. If you're using a debugger (gdb) you should be able to type "bt" for "backtrace" and this will show you the stack at the time the error occurs.

jeffamaphone
He's probably not calling malloc if he's using Objective-C. He's probably releasing at the wrong time.
apphacker
Yeah; I'm not an Obj C guru. Just going off the exception. Either way, most likely an uninitialized pointer if it's non-null but bad. Could be double free too, I suppose but that should be hard to do in Obj C, right?
jeffamaphone
+2  A: 

in gdb try

bt

to get more information about when the error happens. Attempting to free non allocated memory is a common bug and gdb+xcode provides many tools to help you figure out where the problem is.

apphacker
Could you please tell me what are the tools and how to use them? Probably you can point me an article or a book that has the information?This is really a problem for me - I have to spend hours finding where an error is in my code.
Ilya
+1  A: 

You should be able to put a breakpoint a malloc_error this will cause your program to break when the message in question is printed.

You can also use the following method, to find the point of allocation from the address:

  1. Set MallocStackLogging, MallocStackLoggingNoCompact environments to 1.
  2. Run the program and once it prints out the line use malloc_history from shell to find out the stack trace of the allocation: malloc_history <pid> <addr>.
mfazekas