Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding c++ object in the NSMutableArray

I have MeetingUser C++ class; new_meetingUser is its object.

I want to add it to the NSMutableArray meetingUsersList, which I did using NSValue. However, I can't make out how to handle the memory of the object and the ownership of NSMutableArray.

MeetingUser *new_meetingUser = new MeetingUser(userName, dispName );
[meetingUsersList addObject:[NSValue valueWithPointer:new_meetingUser]];
like image 999
Saraswati Avatar asked Jun 07 '12 05:06

Saraswati


1 Answers

You can add the C++ object to the array like that, but the array will not take ownership of the object. You will have to ensure that the object is not deleted while it's in the array, otherwise it will become a dangling pointer. Then you have to delete the object manually later, when you know it is safe to do so.

If you want the array to take ownership of the object, then you will have to use a CFMutableArray. CFMutableArray allows you to specify functions that called when objects are removed from the array, or when the array is deallocated. In your case, you want to set the CFMutableArray to call delete on the objects.

like image 177
Tom Dalling Avatar answered Sep 23 '22 01:09

Tom Dalling