NSNumber Reference Count Question

I'm working my way through Stephen Kochan's Programming in Objective-C right now, and I've just finished Chapter 17 on memory management. The first exercise is to write a program to determine the effect on reference counts of adding and removing objects in a dictionary object. Anyway, I've got the following code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSNumber *num1, *num2, *num3;
NSString *string1, *string2, *string3;
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity: 3];
num1 = [NSNumber numberWithInt: 1];
num2 = [NSNumber numberWithInt: 2];
num3 = [NSNumber numberWithInt: 3];
string1 = [NSString stringWithString: @"first string"];
string2 = [NSString stringWithString: @"second string"];
string3 = [NSString stringWithString: @"third string"];
printf("\n num1 retain count before dictionary: %x\n", [num1 retainCount]);
printf("string1 retain count before dictionary: %x\n", [string1 retainCount]);
printf(" num2 retain count before dictionary: %x\n", [num2 retainCount]);
printf("string2 retain count before dictionary: %x\n", [string2 retainCount]);
printf(" num3 retain count before dictionary: %x\n", [num3 retainCount]);
printf("string3 retain count before dictionary: %x\n", [string3 retainCount]);
[pool release];
return 0;
When I compile and run it, I get the following:
num1 retain count before dictionary: 2
string1 retain count before dictionary: 1
num2 retain count before dictionary: 2
string2 retain count before dictionary: 1
num3 retain count before dictionary: 2
string3 retain count before dictionary: 1
Can anyone tell me why the NSNumber objects have a reference count of 2 after creation, while the strings only have a count of 1? I expected both types of objects to have a reference count of 1 after initialization.

NSNumber itself retains them. Notice, if you set them all to the same value, they'll all just point to a single object.
It's informative to check the addresses with something like:
printf("%x\n%x\n%x\n, num1, num2, num3);

Similar Messages

  • Increase Registry Reference Count with Labview Installer

    I am trying to create an installer that is using an ActiveX control.  There are instructions on what files need to be installed and registered (See attached pdf).  I think I have the files going to the proper destinations and the ones that require registration I have checked the register COM in the source file settings section.  However I am unsure about two items.
    1.  There is a file (IAT_YUV.ax that needs to be registered, how do I go about doing that?
    2. There is a statement in the instructions as follows:
    All files marked with *) must be registered using regsvr32.exe. It is very important to copy all files to the target computer, before registering these files. Most setup toolkits have an option to automate this task.
    For all files marked with **), you have increase their reference counts in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Curr​entVersion\SharedDLLs. Most setup toolkits have options to perform this step automatically.
    I am not sure how to accomplish this in the Installer?
    Regards
    Terry.
    Attachments:
    IC Image Control Deployment.pdf ‏34 KB

    Hi Terry,
    The answer to your question is quite involved. We have this knowledge base article that you can follow for registering (IAT_YUV.ax and similar files. You will write a LabVIEW executable that uses the System Exec VI to register the ActiveX dll. To include them in the installer, under the Installer Properties»Advanced..., select Run executable after installation and add the above executable. Therefore when the installer is finished your executable that registers the dll will automatically run. While this process is pretty involved that should be all you need to do.  Hope this helps!
    Regards,
    Travis Ann
    Customer Education Product Marketing Manager
    National Instruments

  • String reference count

    Hi,
    is copy a string to another string a real copy or is it just an increment of the reference count to it. For example if used like this:
    private void foo()
         String data = getFooData();
         lastData = data;
         if (someThingTodo) {
              // do something
              lastData = null;
    }If it is a question of reference count, then this code only increment, and eventually decrement it, right ?

    Assigning to a reference type--String, Object, List, MyClas, etc.--never copies the object. It always just causes the reference on the left side to point at the same object that the reference on the right side points to.
    Java doesn't use reference counting for its memory management. But if your question about decrementing is in the vein, "Will the VM automatically detect when an object can no longer be reached and make its memory available for use by other objects?" then then answer is yes, that's taken care of for you.

  • Do Java interfaces have automatic reference counting?

    I'm actually a Delphi developer trying to understand interfaces and I'm curious about the way Java implements them.
    Here is a simple Delphi psuedo-code snippet:
    var
    // interface variable
    Intf : IInterface;
    // object reference variable
    Car : TCar;
    begin
    // instantiate TCar
    Car := TCar.Create;
    // obtain IInterface interface for Car object
    Intf := Car; // the reference count for Car is now 1
    Intf :=Nil;
    // After setting Intf to nil, the reference count is decreased to 0 and Car is automatically freed.
    Car.Free; // EXCEPTION! Car was automatically freed because the only interface reference had a reference count of 0
    What would happen in Java with the above code?
    Does Java automatically reference count all objects with interfaces?
    I know Java reference counts all objects in general so they are automatically destroyed, but I'm trying to understand the difference in interface implementation.
    Thanks for any help!

    Thanks for the replies!
    Sorry about my confusion with Java...
    Does Java support interfaces? If so, can't you have a interface reference variable like I have in my Delphi code?
    Also, if Java does support interfaces, do they have any other purposes aside from creating a list of functions/methods that can be used as a contract to interact with an object?
    Interfaces seem really useful since you can have an IDriveable interface (for example), and any unrelated classes can implement it... (eg. TBoat class, TCar class, etc) even if the classes do not have a common ancestor class.
    However, in Delphi interfaces also automatically reference count so once you create an interface reference you have to be very careful so the interface doesn't automatically free the object it is pointing to...

  • Using reference counter with 6602 board

    I want to control a 6602 card in a Labview application so that I can count events in channels 1-6 depending on a reference counter in channel 0. I've set up channel 0 to count from a fixed value down to 0, and I am using hardware triggering to start all counters. Now I need something like a stop trigger to stop my event counters simultaneously when counter 0 reaches zero.
    I tried to use counter 0 output as a gate for the others, but without success. Unfortunately, the help files for the TIO counter VI's are pretty lousy (nudge, nudge), so I'd be grateful if someone could give me a hint how to solve this problem.
    Sebastian

    Hi Sebastian (whatthehellamithinking),
    Nice name I think your idea about using counter 0 output as the gate signal is the right idea. However, I would make sure to configure a couple things. The first thing I would do is configure counter 0 output for toggle mode. This means when the counter rolls beyond zero it will toggle it's output. I would alsoe configure the output to be configured as high-to-low logic. Then I would connect it to the gate of all the other counters (make sure to configure a gate on each counter). The gate should initially be high on each counter since the output of counter zero should be high. Then when it rolls or reaches terminal count it will toggle the output which will gate each of the other counters.
    Anyway, not sure i
    f you tried it that way but I think it shoud work. Hope that helps. Have a good day.
    Ron

  • "reference count underflow" printing NSDocument using "Save as PDF"

    Hello, I'm getting a "reference count underflow" error trying to do a "Save as PDF" in a NSDocument class using the printOperationWithSettings method. There are no errors sending to the printer or viewing the output in preview. I found the post http://discussions.apple.com/thread.jspa?messageID=8708986&tstart=0 which was posted a year ago showing a 10 line program with the same error. I've got garbage collection turned on.
    Is this still a know problem?
    Thanks

    Oh I do get the "reference count underflow" when compiling and running the above 10-line program.
    If I create a new "Document-based" project and setting "Garbage Collection" to required, add an IBOutlet variable to reference the NSTextField generated, and add the following required method for printing:
    - (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)printSettings error:(NSError **)outError
    NSPrintInfo *tempPrintInfo = [self printInfo];
    if ([printSettings count]) {
    tempPrintInfo = [[tempPrintInfo copy] autorelease];
    [[tempPrintInfo dictionary] addEntriesFromDictionary:printSettings];
    // textField is an IBOutlet variable
    NSPrintOperation *op = [NSPrintOperation printOperationWithView:textField printInfo:tempPrintInfo];
    return op;
    I can print and "Open PDF in Preview" without errors, but get the "reference count underflow" with the "Save as PDF".
    The sample application "SimpleStickies" created in 2009-10-14 and has by default Garbage Collection required, gets the same errors.
    Is there a simple solution to creating a document-based project that allows "Save as PDF" to work.
    This is a common function. I know I'm missing something obvious.
    Thanks!

  • Audio capture filter reference count causing leak

    I have a purplexing problem.
    I have written my own audio and video capture filters and they work fine. I have tested with FMLE, graphedit and various other tools which i wrote myself. When i click "Stop" on the stream or close FMLE, the reference count on the audio capture filter is not release to 0, thus causing a memory leak. The leak is not a big problem, but my filters have to do some important processing in their destructors, which dont get called because its not being released.
    To purplex the problem further, my video capture filter is not having this problem, only my audio.
    I have written several more capture filters that literally do nothing other than register themselves to explore this problem and only the audio filters seem to be leaking. Anyone else seen such a bizare problem ?
    I am using FMLE 3.1.0.8703

    Thank you for your quick reply!
    However I cant manage to find the panic logs in the library/logs/ or any of its directorys. Ive looked for this logs before and my guess is that they dont get created if the panic occurs during boot.

  • Panel Decoration Reference index Question

    I have a front panel that uses native LV controls so I avoid scaling objects with monitor resolution as I have not had the best results with this when using native LV controls and indicators.
    The front panel was laid out on a 1680 X 1050 widescreen monitor.  I want to use it on my laptop that uses 1280 X 1024.  I have the verticle taken care of by ensuring all the controls fit within th everticle area.  But I need help with the horizontal scrolling.
    I am trying to limit the left and right scrolling to not extend beyond the furthest most left decoration and the furthest right decoration.  I used PANEL - DECORATION property nodes to get access to the decoration bounds so I can set the Left, Top and Right Bottom of the FP.BOUNDS,
    But I have not had any luck getting this to limit the horizontal scrolling,  it still goes to far left and to far right.
    Another question is - How do i find out the index of a decoration when there are a lot of decorations on the front panel.
    Thanks
    Tim Crouse
    1:30 Seconds ARRRGHHH!!!! I want my popcorn NOW! Isn't there anything faster than a microwave!

     Tim,
    You can get the reference of a specific decoration by looking at the size and/or color. Using Width, Height, and Color, you should be able to pin down the exact decoration that you want a reference of. The attched picture just looks at Height.
    ...(I'm getting tired of the editor eating my posts)...
    Richard
    Attachments:
    specific decoration ref.gif ‏50 KB

  • Play count question.   I have about 500 songs in my library that I know have played at least once...probably just once.  Although there is a "last played" date listed there is no number in the play count column.   Any reason for this?  thx

    I have about 500 songs in my library that have played one time.   Although a "last date played" date is noted, there is no number in the play count column.  This only happens some times.   Any reason for this, and can it be corrected?  thx

    Thanks, but the play count is not incrementing only on the very first play.  All subsequent plays are fine.  Meaning the play counts for these particular tracks are always one play short.  I double-checked some of the tracks in question and I have not shortened, or stopped them early.

  • Dictionary, Table, Counter Question

    Hi,
    I'm trying to create a special customer table (SE11) with the following fields and have question regarding TYPE etc:
    Fields of table
    CUST_ID    
    CUST_NAME
    CUST_DESC
    1. CUST_ID should work as a counter (increment by 1 every time.) What kind of TYPE should I use ?
    (How do i do the increment stuff...)
    2. CUST_DESC should be able to take approx 5 kb of data and be searchable in WHERE clause. What kind of TYPE should I use ?
    3. Is there any advantages of creating/using Data Element instead of Build-in type ?
    //Martin
    Message was edited by: Martin Andersson

    Hi Martin,
    1. SE11 will help you only to create a table, but not to add any special functionality (as far as i have known) the CUST_ID can be made as counter only by programming. while moving data use a variable to get the previous maximum CUST_ID value from the tabel and increment it before you insert the value into the table.
    2. To give long descriptions declare the CUST_DESC as LCHR or LRAW types, i am not sure whether it will hold 5kb of data. Then searching for a particular word will not be a problem  you can use,
    SEARCH <c> FOR <str> <options>.
    <option> in the SEARCH FOR statement can be any of the following:
    ABBREVIATED
    STARTING AT <n1>
    ENDING AT <n2>
    AND MARK
    can be used to find the required word.
    3. I think this is what you meant -  if a data element is created by a domain, the technical attributes of the domain are inherited by data elements and these domains can be used by any number of data elements, besides you can also create data elements by refering to the existing data elements using 'TYPE'.
    Hope this helps.
    Regards,
    Kathirvel

  • E-Tester Script Programming Interface Reference Guide   - question

    Hello,
    I just came across this question posted as a Comment for this Knowledge Base document http://qazone.empirix.com/entry.jspa?externalID=34&categoryID=18, so I am adding it to the Forums for additional visibility and replies
    Author: Wegener, Posted: Jul 23, 2007 11:47 AM
    To get the current workspace in a Job Scheduler job I used the following code from the reference
    Private Sub ThisJob_JobBegin()
    Dim location
    location = application.currentResult.workspace
    End Sub
    As a result the value of the variable location changed from Empty to "".
    What can be the reason for this failure?

    We had a similar problem. You actually need to do this in job end or script end event. The application.currentResult object does not have any data until the end of a test.
    I hope this is helpful.

  • WSA / CWS Licensing count question.

    Hello,
    I have a question how WSA and CWS are licensed based on the number of users.
    Is it based on concurrent or named users, active sessions based on source IP address or another way? This old post mentions concurrent users but based on a legacy ironport ordering guide. And that document does not mention any licensing methods.
    https://supportforums.cisco.com/message/3949429
    However for CWS i found this post:
    https://supportforums.cisco.com/message/4003674
    For the sizing it mentions: "It will be total number of CWS users, not concurrent users". Sizing is nog licensing, but the CWS datasheet mentions "The Cisco Web Security portfolio uses tiered pricing based on the number of users".
    I believe there are licenses allowing mixed use of CWS and WSA in combination with the free Virtual Applicances. Is this correct and how does this reflect on my question regarding user licensing?

    Hi,
    It's based on the number of named users, however there is no absolute way to count the users so it's a trust thing too and you should be honest about how many users you have.
    Thanks
    Chris

  • Counter Questions Using PCI6602

    I have a couple of questions regarding the operation of the PCI6602. I am programming in VB.NET.
    Can you have multiple tasks that each correspond to one Counter Input Channel being used at the same time (i.e. trying to read a small angle inside another larger angle using encoders)?
    Is there any way to setup a trigger based on a counter count value (i.e. once the count value hits 180 degrees raise a digital bit)?

    Justin,
    You cannot have two tasks running simultaneously that are using the same counter input channel. You can, however, connect the same signal to two of your counter channels and have two tasks monitoring the two different channels.
    As far as triggering off of a counter value. The best that you can do is a software trigger. In DAQmx, in LV 7.1 you can do a timed loop that waits for the event to occur (i.e. counter reaching a value). I am not aware of this implementation in VB, but it is still nothing more than a fancy software trigger. You would need to poll the counter and then run the DIO task when it reaches its value.

  • Sum/Count Question

    Hi: I'm very new to Crystal Reports, and have a question that is probably pretty simple, but has me stumped and at a standstill...
    I've built a report in Excel that determines the number of days between two date columns, then has a series of summary fields that count how many records have 1 day, how many have 2 days, ... 3 days, etc... I'm trying to build something similar in CR that will do the same thing, but I can't find it. I'm thinking it should have something to do with the Count (fld, condfld) function, but think I'm having trouble getting the syntax right. Any help here?
    Thanks!
    Edited by: dthornton on Jan 15, 2010 11:48 AM

    I'm now looking for a way to show that result as a percentage of all records returned
    Sure.  Lets assume you have a formula @DaysDiff which is calculating the days difference between two days.  Not let's say you have a running total RT_OneDaysDiff which is counting your records  but only when @DaysDiff = 1.  If you want to know the percentage of records where Days Diff = 1 compared to the total number of records, you would create a formula as followis:
    if @count(rowidentifier) <> 0
         then @RT_OneDaysDiff / count(rowidentifier)
    Fuskie
    Who would then repeat for each desired number of days difference...

  • Apex_application.g_fxx.count Question (problem?)

    Do the apex_application.g_fxx variables need to be shown on the page (region) in order for apex_application.g_fxx.count to return a non-zero value?
    I'm trying to figure out why I spent a day trying to get something to work, and it seems like this is what it was, though I'm not quite sure.
    In my report region, I have a column that when shown, my process works correctly, where a loop that has the form of:
    FOR i IN 1 .. APEX_APPLICATION.g_f16.COUNT
    LOOP
    This performs the statements within the loop.
    However, if that column is not shown (under Report attributes, unclick the Show box), then the process does not seem to execute the statements inside the loop.
    The only thing I can think of is that when it's marked as not shown, then the COUNT has no value, even though it seems like it should. Does COUNT only return the count of displayable items in the column, so if it's not shown, it's zero?
    It also may be how the report region is defined as well I suppose. It seems like ApEx should understand it okay, but who knows. It's simply a join of two inline views, so I can pre-populate the region with values from another table if this table didn't have any entries.
    This is Apex 2.2.1.00.04. One of these days I'll get caught up enough to upgrade to 3.0.
    Thanks,
    Bill Ferguson

    Hi Patrick,
    I suppose I also should have stated that this is a manually generated report region, using the apex_application.[item_type] syntax, where I have to specify the array (item) number. I just tested with Firebug, and if I change the number of the second column to 22, it's shown as f22 instead of f02.
    g_f16 was the next to the last column, though the one after it is a hidden column (apex_application.hidden).
    My report region is such, that g_f03 andg f16 are the only columns I can verify will actually have some data in it, as I force a value into gf16 if the second half of my union is selecting null values, so I could get around the null sorting problem (NULLS LAST didn't work, even on 10gR2, so I'm doing something else wrong).
    Anyway, I can use the values of the columns, whether they are displayed or not, it's just that apex_application.g_f16.COUNT always returned a zero when the column was hidden, even though it consisted of an array of 5 values. Trying to track it down I was able send the value of the column to a logging table, so I was able to see that I was getting the values, just not the count of values.
    Bill Ferguson

Maybe you are looking for

  • ITunes keeps rejecting my security code on my cc so I cannot buy any tunes.  And it worked fine yesterday!

    HI FOLKS, Itunes keeps rejecting my security code on my debit card, saying it is not valid, even though I just recently used it to purchase on Itunes and it is the number on the back of the card.  ???.  Any one else run into this?  ANy ideas?  Thank

  • TCP/IP Protocol

    Hi Guys, I want some basic information regarding protocols..What is a Protocol? Where is i used? And what are the types of protocols...In the same context please also explain SOAP Protocol.

  • ACE 4710 transparent LB with two Caches and two routers.

    Hello, I have ACE 4710 that load balance two cach flows (bluecoat), i am doing pbr on the routers to send the traffic destined to port 80 to ACE then Cach farm. After that the Cach flow will get the page from the internet via two routers. The return

  • Photo library occupy big storage

    this is not the issue related to iTunes! in the storage usage, there are four items for the Photos&Camera: Photo library (2.1GB), synced from iTunes Library (0.8kb), My photo stream (181mb), Shared Photo Stream(20.4MB). I can not figure out what the

  • Resetting my WRT54GS V.6

    Hi there My wireless router is really playing up this weather. When I try and access 192.168.1.1 its not working, so I presed the reset button at the back of the router for 30 seconds.. and since I did that the power light is flashing continueously..