bugGNUstep - Bugs: bug #6743, Distinct NSNumbers with the same...

Group
 
 

bug #6743: Distinct NSNumbers with the same value dealloc each other

Submitter:  Invalid User ID <#25793>
Submitted:  Tue 25 Nov 2003 03:06:21 AM UTC
   
 
Category:  Base/Foundation Severity:  3 - Normal
Item Group:  Bug Status:  None
Privacy:  Public Assigned to:  None
Open/Closed:  Closed
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Sun 30 Nov 2003 09:08:07 AM UTC, comment #9: 

Code in cvs modified to raise an exception if a cached small number is deallocated ... more user friendly than crashing on attempting to reference a deallocated object where user code has released a number it doesn't own.

Richard Frith-Macdonald <CaS>
Group Member
Tue 25 Nov 2003 10:36:07 PM UTC, comment #8: 

Aside from my assertion that this is counterintuitive, my face is burning with shame. Thanks for the pointer.

Invalid User ID <#25793>
Tue 25 Nov 2003 09:52:14 PM UTC, comment #7: 

[NSNumber +numberWithLong:] returns an autoreleased object, not an object you own. You might want to read http://www.stepwise.com/Articles/Technical/HoldMe.html .

Alexander Malmberg <alexm>
Group Member
Tue 25 Nov 2003 09:38:43 PM UTC, comment #6: 

I confess I don't understand how the test program fails to own the numbers n1 and n2, because it allocated them. I think given "principle of least surprise" I expect these entities to be independent of each other, just as if they were created using malloc() or if they were some other NSObject subclass. Doing otherwise breaks retain/release balance. I would suggest ignoring a request to dealloc a cached NSNumber if the program must keep track of how many identical NSNumbers it has created (essentially duplicating the cache).

(Insert newbie disclaimer here.)

I would be curious to know how Cocoa handles the test program.

Invalid User ID <#25793>
Tue 25 Nov 2003 11:09:48 AM UTC, comment #5: 

Yes, that would be very good.

David Ayers <ayers>
Group Member
Tue 25 Nov 2003 10:40:53 AM UTC, comment #4: 

What I was thinking of was making the cached numbers members of some new subclass where -dealloc had been overridden to raise an exception.  So there would be no runtime overhead on dealloc of a non-cached number.

Richard Frith-Macdonald <CaS>
Group Member
Tue 25 Nov 2003 10:17:35 AM UTC, comment #3: 

In principal I agree, but it depends on the cost.  I'm not sure how much the hit would be, but expect numbers to be created and destroyed often in GDL2 based applications.  So if each dealloc resulted in a hash look up... well, depending on the implementation, I guess we may need to test it.

David Ayers <ayers>
Group Member
Tue 25 Nov 2003 08:00:09 AM UTC, comment #2: 

I think raising an exception upon an attempt to deallocate a cached NSNumber would be a nice feature.

Richard Frith-Macdonald <CaS>
Group Member
Tue 25 Nov 2003 06:24:42 AM UTC, comment #1: 

There is a bug in the test program ... it releases an object it does not own, so a crash does not seem unreasonable (in the general case releasing objects you don't own is bound to cause crashes).

Is the report a suggestion that the library should be changed to crash elsewhere, or to have code added to watch for this case and raise an exception or something similar, or perhaps to simply ignore the extra release and keep on going?

Richard Frith-Macdonald <CaS>
Group Member
Tue 25 Nov 2003 03:06:21 AM UTC, original submission:  

Distinct NSNumbers with the same value can dealloc each other. The following code snippet crashes gnustep-base 1.7.3 possibly because of NSNumber caching. If I don't call [n1 release] there is no crash. If this is due to caching strategy, then it can make NSNumber very fragile when numbers are expected to persist as long as retained.

#include <Foundation/Foundation.h>

int main (int argc, const char *argv[], const char *env[])
{
  NSNumber* n1 = [NSNumber numberWithLong:1];
  NSNumber* n2 = [NSNumber numberWithLong:1];
  NSLog(@"%@(%d)\n", n1, [n1 retainCount]);
  NSLog(@"%@(%d)\n", n2, [n2 retainCount]);
  [n1 release]; //<--problem here
  // The next line crashes. n2 has gone away!
  NSLog(@"%@(%d)\n", n2, [n2 retainCount]);
  return 0;
}

Thanks for your attention.
Brian 'Moses' Hall
-email is unavailable-

Invalid User ID <#25793>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

CC list is empty

 

There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

Only logged-in users can vote.

 

Follows 1 latest change.

Date Changed by Updated Field Previous Value => Replaced by
2003-11-30 CaS Open/ClosedOpen Closed

Back to the top

Powered by Savane 3.13-4b48.
Corresponding source code