Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by dashesy · Dec 26, 2012 at 05:50 PM · iospluginobjective-c

UnitySendMessage and NSString allocation

What would be right way of sending a custom NSString from objective-c to Unity?

I can do this, and the message is successfully received:

 NSString * aString = [[NSString alloc] initWithString: @"a string"];
 UnitySendMessage("MyObject", "Method", [aString UTF8String]);

But I am wondering how the memory allocation works across the message passing, I guess I have to do allocation (most examples do), but is the UnitySendMessage synchronous. I just want to make sure I have neither a memory leak, nor a dangling pointer problem.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by $$anonymous$$ · Dec 27, 2012 at 11:54 AM

Hey there Dashesy :)

I've been interested in knowing the details of this myself as I've also been using UnitySendMessage extensively in plugin development.

First of all, from the original documentation it is stated that UnitySendMessage is not synchronous, that is, the call will actually probably be received the next frame: Official documentation

From what I was able to gather from around the web when it comes to unmanaged->managed calls between Objective-C and Mono using UnitySendMessage, it will copy any data which you send using UnitySendMessage. The source of this information comes from this thread: Original Source

I've been working accordingly to this for a while now, that is, right after I send any allocated memory from Objective-C to Mono, I deallocate it right away just as you would with any other alloc/release pair in traditional Objective-C code to avoid memory leaks. Actually I've mostly simplified the problem by just making use of autorelease objects, and it have never imposed any problems. You should be safe doing this :)

Hope this helps!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dashesy · Dec 27, 2012 at 06:41 PM 0
Share

Thanks, yes that was most helpful and it makes perfect sense. I am going to rely on the auto pool, and let the variable go out of scope after UnitySend$$anonymous$$essage for memory to be reclaimed.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Open Web page embedded in unity app using native UIVIEW 0 Answers

How to pass a Swift string into Unity iOS game? 0 Answers

Alternate to UnitySendMessage in iOS native 1 Answer

iOS Objective C plugin linker error 2 Answers

Unity iPhone plugin - what data type to use? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges