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 Robert · Dec 07, 2009 at 07:29 AM · iphoneplugin

Objective-C Plugin issue

I am using the Unity Plugin feature to transfer data from Obj-C to Unity. After much blind falling about, I eventually settled on the method outlined below.

Obj-C side:

char* MakeStringCopy (const char* string) { if (string == NULL) return NULL; char* res = (char*)malloc(strlen(string) + 1); strcpy(res, string); return res; }

struct PuzzleEngineData { const char* puzzleData; bool canRatePuzzle; int usedHints; };

extern "C" { void _LoadPuzzleData ( struct PuzzleData *puzzleData ) { // of course, these values actually come from data // stored elsewhere, but the gist is the same puzzleData->puzzleData = MakeStringCopy([@"puzzle string" UTF8String]); puzzleData->canRatePuzzle = YES; puzzleData->usedHints = 0; } }

C# side:

public struct PuzzleData { public string puzzleData; public bool canRatePuzzle; public int usedHints; }

[DllImport ("__Internal")] private static extern void _LoadPuzzleData ( ref PuzzleData puzzleData );

public static PuzzleData LoadPuzzleData () { PuzzleData data = new PuzzleData(); if ( Application.platform == RuntimePlatform.IPhonePlayer ) { LoadPuzzleData( ref data ); } return data; }

It all works - The data gets into Unity just fine. Right after the C method finishes executing, however, I am getting the following output in the console when running the app on-device in Debug mode:

PuzzleGame(2940,0x383f72d8) malloc: *** error for object 0xd35510: 
Non-aligned pointer being freed (2)
*** set a breakpoint in malloc_error_break to debug
PuzzleGame(2940,0x383f72d8) malloc: *** error for object 0x62727a0: 
Non-aligned pointer being freed (2)
*** set a breakpoint in malloc_error_break to debug
PuzzleGame(2940,0x383f72d8) malloc: *** error for object 0xd355b0: 
Non-aligned pointer being freed (2)
*** set a breakpoint in malloc_error_break to debug

Research tells me that this error comes up when free() is called on something that hasn't been malloc'ed. I haven't the faintest clue about how to go about using malloc correctly to solve this problem in my particular instance; all the strings are malloc'ed, and the struct is created C# side.

Help? :)

===== EDIT =====

Turns out that making the struct's strings non const and actually using MakeStringCopy() for every instance of a string fixes it! Part misunderstanding and part pebcak (problem-exists-between-keyboard-and-chair)!

Thanks jonas echterhoff for taking the time to help me.

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
3
Best Answer

Answer by jonas-echterhoff · Dec 07, 2009 at 10:57 AM

While I'm not sure what exactly is happening here, I'm pretty sure you cannot simply cast a C++ string to a C# string in a struct, as they are represented different internally. However, IIRC, you should be able to do that by passing the C++ string as a return value. This is because the mono runtime detects the case of having strings as a return value and performs an automatic cast from C++ to C# strings in that case. At least that is how I remember it, probably worth experimenting with anyways.

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Marshal.PtrToStructure in iPhone makes error. 1 Answer

using native code plugin for unity iPhone 1 Answer

can mono touch be used to make unity iPhone plugins 0 Answers

do plugins run on iphone? 1 Answer

Specific steps to set up a plugin for iphone in XCode 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