Obj-C keeping memory addresses the same on next debug - objective-c

I'm not sure if there is a name for this technique, but I remember doing something like this a long time ago in C++. I would like to breakpoint and observe a specific object of which there are hundreds in my program. It would be nice if you can tell the compiler to use a reserved space of memory so that I can run once, pull out a memory address, then run again with the guaruntee of having the objects allocated to the same address in memory so that I can see what happens to this specific object the next time around.
At the moment I am just assigning a 'debug id' which gets incremented with each allocation but thought there might be a cleaner way of doing it. I'm sure I've done this before with Vis Studio / C++...

You can set a conditional breakpoint in Xcode so that it will only break into the debugger if a certain condition is satisfied.
To do this, set the breakpoint normally and then right-click on it and select Edit Breakpoint.
Locate the breakpoint in the Breakpoints window and double click the "Condition" column. You can then enter an expression, something like:
(BOOL)[[yourObject name] isEqualToString:#"foo"]
This will break only when the name property of yourObject is foo.
Note that you need to cast the result of the expression to a boolean, otherwise gdb doesn't know how to deal with the result of the expression. You also can't use dot notation syntax, you must use the full square bracket syntax.

Related

Pharo: customizing smart characters

The checkbox "Smart Characters" in "Code Completion" section of Settings Browser does (at least) two things:
1) It doubles some characters when typed: ', ", (, [, {
2) It enables that I can select a piece of code, press ( (i.e. Shift+9), and the selected code becomes surrounded by parentheses: (). I also can remove parentheses by pressing ( again. I also can do this with [] by pressing [ and with {} by pressing {, i.e. Shift+[.
I do not like the first of these things so I want to disable it, but I like the second thing and want to keep it. How can I achieve this? Turning off the checkbox will disable both.
P.S. I know that when the checkbox is off, adding/removing parentheses works by Cmd+Shift+9 (which is less convenient than Shift+9) and that Cmd+[ works for [], although I do not know any working shortcut for adding/removing {} when the checkbox is off.
The setting is called "Smart Characters", which should give you a clue as to where to look. Open a Finder, type in smartCharacters, and hit Enter. You should see some partial matches as well as an exact match for NECController and NECPreferences class (and the former just calls the latter). If you investigate the classes involved a bit, you'll see that smartCharacters stores a boolean, and that smartCharactersMapping returns a dictionary mapping some characters to their "counterparts", i.e. $[ to $] and so on. Now look at senders of smartCharactersMapping, and you'll see where it's being called from.
The caller you're most likely interested in would be NECController>>smartCharacterWithEvent. So put a breakpoint in that very ugly method to see what it does. You don't care about the first two cases (the editor having a selection and there not being a smart mapping), since you want to prevent the second matching character from being inserted. So the interesting bit for you is this bit:
self newSmartCharacterInsertionStringForLeft: char right: opposite
The method only has one implementor and that one sender, so it should be safe to comment out the original method and just return the "left" character, i.e.:
newSmartCharacterInsertionStringForLeft: left right: right
^String with: left
In other words, instead of creating a string with the left and right characters, and possibly spaces between them, just return a new string with the single character you typed. Might not be the most elegant way of solving this, but it should work, and should show you how to solve similar problems in the future.
(Ideally, you'll find a better solution, post it here as an alternate answer, and contribute it to the Pharo codebase - Pharo is open source, after all.)

VBA Overflow Error 6 -- simply when terminating a class

I am trying to debug code that typically functions properly and has been in production for a while, but has some errors associated with particular case runs.
I have a class called "Guarantee", and a variable / object, "myDem", which is of that class. The class has an associated function called "NumberOfGuaranteeDays". The first image given below shows a Long variable called "numRows" being assigned to this function call on "myDem". When this assigment is made -- and, therefore, the "NumberOfGuaranteeDays" function is called -- I receive an Overflow error.
After the function call "NumberOfGuaranteeDays" is run, which is a fairly complex call with many sub-functions itself, then class attempts to terminate itself, and return the value (the number of days, which is an integer... in this case it's 32561). It is during this termination step and the assignment of "numRows" to the value 32561, when the error occurs.
Here I simply demonstrate that the very next step within the code, if I step through it, is where the error message is returned back to me.
Finally I wanted to provide "proof" that the value assigned to "numRows" is 32561, of type Integer, which can acceptably be assigned to a Long. Note in the far right in the watch part of the window, the value "res" is the value which is returned from the "NumberOfGuaranteeDays" call, which is then assigned to the variable "numRows".
As far as I can tell, there are only 2 possibilities for why a crash can be occurring:
There is an error in the attempt to terminate the class. I don't understand how this could lead to an "overflow" error, though.
There is an error in the assignment of the value calculated from "NumberOfGuaranteeDays" to the variable "numRows". This sort of assignment could potentially have an overflow, but not in this case. The return from "NumberOfGuaranteeDays", which is the "res" integer set at 32561, is assigned to "numRows", which is a Long.
So, since neither of these possibilities that I can imagine make sense, I figure there must be another possibility I cannot see. Thank you all in advance for the help!
I opted to put in cut & pasted images instead of writing as code because so much of what's critical to understanding the steps is seeing "proof" of where I am in the debugging stages. If actual code snippets would help, let me know.
Thank you!
Mike

GW Basic default variable initialization

I'm working with legacy code and ran across something that I haven't been able to explain after several days of looking up tutorials and handbooks for GW Basic: a variable (P9%) is used in a comparison on line 530 (IF P9% <> 0) before the code would reach its definition on line 860. It's not a complex piece of code, only ~1200 lines total, so I am confident that I haven't missed any goto or gosub or anything that would reach 860 earlier than this comparison.
I am curious as to how this has been effecting the program as it runs. Most of my experience is with c++ where this sort of thing wouldn't compile, and if it did an unassigned variable could potentially contain anything that would fit, but I have no idea what kind of default assignment is given to a variable in Basic.
It has been many years since I wrote much in gwbasic!
If I remember correctly the variable is assigned a zero value in that case. Gwbasic (and Qbasic I think) assigns a default value to all variables when first referenced, this is usually zero or the empty string for a string variable.
Interestingly when creating an array using the DIM statement, all the items in the array are also initialised this way.
Even with this mechanism it is usually better to initialise a variable, just to be clear what is happening.
Many programmers of the era writing for gwbasic omitted as much as they could to minimise the amount of memory used by the program instructions so they had more for other stuff. So that may be why it's not initialised.

Expression in Xcode 4.3.2 Debug area not evaluated

I'm doing something wrong. I have added an expression, I can see the expression with the "E" symbol in the Debug area, but the expression is not being evaluated, its value is not displayed there (it is in scope at that time).
When I use the debugger (lldb) directly, it works well.
Xcode 4.3.2.
What should I do?
Thanks
You are trying to evaluate a boolean and print it as an object.
You want to use print [self isEditing] or print (BOOL)[self isEditing], depending upon whether the debugger complains that it doesn't know the type of the member or not.
The po command prints an object description, not an arbitrary value, and should only be used when the result of the expression on the right is an object, such as po self.
The same problem occurs in the expression editor. If you use the expression [self isEditing], the debugger won't understand it. However, if you use (BOOL)[self isEditing], it will display correctly.
try adding the expression as self.isEditing, without the square brackets. Works here

How to print chained variables using GDB console?

How to print C type variables at GDB console in xcode4? I'm able to print variables using p var, also able to print variables like p myObj.property but unable to print variables that that are on 3rd level depth. For example using p objName.pointerToOtherObject.someProperty does not work. The GDB dislays "There is no member named someProperty." message but it is there for surely. I'm using the 4.02 version of xcode4 but still it sucks when it comes to displaying properties, variables and etc from debug area. I mean it is unable to show the content or arrays and dictionaries, also, sometimes, it is not displaying the values of vars when I move the mouse over that var, in that case I need to click and move the mouse somewhere else and then move the mouse over again, then it works. Maybe I'm missing some hints but those small problems sometimes annoys me :) For object printing I' using po.
The solution would be to use method messaging syntax instead of property syntax:
p [[SomeObj pointerToOtherObject] someProperty]