Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Creating a Handle from an existing memory



If you don't need a Handle for some other reason, such as an API that expects to
get a Handle, and want to do refcounting, taking advantage of the operating
system's means of doing it, take a look at CFData.h and CFBase.h for CFDataRef,
CFMutableDataRef, and CFAllocatorRef along with their various methods of
creation.


Quoting Yaron Tadmor <email@hidden>:

A simple no would do the trick. There's no need to disrespect, especially when you might be wrong.

If you look at various packages supplying memory management, some allow
you to pass your own memory and even your own deallocation method. For
example, boost:share_ptr. By default, when reference counting drops to
0, it calls c++ delete on the object. But you could very well pass a
deleter object which does something else.


So it's perfectly fine to assume such a MemoryToHandle function can be
available, marking that handle as a "no delete" handle. Meaning that
when it's released, the memory is not to be deleted, and deletion should
be handled by the user. Carbon has quite a few examples of such things.


And as a test, I tried using PtrToHand and PtrToXHand, passing my c++
allocated memory instead of a real Ptr allocated by the system. Guess
what.... It worked!!! Releasing the handle, did not try to delete the
memory, as I would expect.


So, yes, I've been programming long enough to know such questions are
certainly in place.




-----Original Message-----
From: Laurence Harris [mailto:email@hidden]
Sent: Monday, June 11, 2007 7:48 PM
To: Yaron Tadmor
Cc: carbon-dev
Subject: Re: Creating a Handle from an existing memory


On Jun 11, 2007, at 1:03 PM, Yaron Tadmor wrote:

> Hi,
>
> Is there a way to create a handle from an already existing memory
> area? For example, can I do something like this:
>
> Char *myP = new char[100];
>
> Handle myH = MemoryToHandle (myP, 100);
No. There never has been. If there were, it would be in MacMemory.h. If you anticipate needing the functionality of a Handle you should
allocate the memory that way initially.


Besides, if you did something like the above it wouldn't work anyway. The memory in your example was allocated with a C++ new allocation. You then pass it off to a Memory Manager data type, which would
ultimately release it with free instead of your C++ delete. It needs
to be released with:


delete [] myP;

and that's not going to happen inside DisposeHandle.

You've been programming for a long time. Why are you asking such
questions?

Larry

>
> I saw there is a PtrToHand and PtrToXHand, but they use copying. >
> Thanx
>
> Yaron Tadmor

_______________________________________________
Do not post admin requests to the list. They will be ignored. Carbon-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/email@hidden


This email sent to email@hidden



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden
References: 
 >RE: Creating a Handle from an existing memory (From: "Yaron Tadmor" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.