Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7012079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:12:34+00:00 2026-05-27T22:12:34+00:00

I was wondering is it safe to return a fixed pointer from one method

  • 0

I was wondering is it safe to return a fixed pointer from one method to another method – does the pinned object still stay fixed? e.g.

struct TestData
{
    public int value;
}

public class Class1
{
    private TestData data; 

    public unsafe TestData* GetDataPtr()
    {
        fixed (TestData* ptr = &data)
        {
            // IS THIS SAFE?
            return ptr; 
        }
    }
}

public class Class2
{
    public unsafe void Test()
    {
        Class1 x = new Class1();

        TestData* ptr = x.GetDataPtr(); // is this still fixed?
        ptr->value = 2; 
    }
}

The reason i’m asking is using something in this style I’ve been getting AccessViolation exceptions. But since I changed it to e.g. set value direct from Class1 i haven’t seen the issue occur.

EDIT:
the reason i thought it may be fixed is if from outside the “TestData* ptr = x.GetDataPtr()” you try to put fixed( ) you get “you cannot take the address of an already fixed expression”. .. i get it now though it’s speaking about my var “ptr” in “Test()” already being fixed..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:12:34+00:00Added an answer on May 27, 2026 at 10:12 pm

    Returning from the method ends the scope of fixed, hence the pointer is no longer fixed once you return. It is safe to pass fixed pointers up the chain, e.g.

    fixed(TestData* ptr = &data) {
        MyFunction1(ptr);
        MyFunction2(ptr);
    }
    

    But returning makes the pointer non-fixed again.

    Logically, this makes sense: there is no alternative way for the CLR to decide when the pointer should become non-fixed after you have returned it from the call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if it's safe to do the following iteration to find the
I'm wondering what is the best way to make data thread-safe. Specifically, I need
I am pretty new to thread-safe programming, and was wondering if I have something
Wondering if anyone has gotten the infamous database is locked error from Trac and
I was wondering, whether the following code are safe. public class GUIBundle { //
I was wondering if it is safe to do this... delete p_pointer; p_pointer =
I'm wondering whether it's safe to mark bounce messages containing This is a permanent
I'm wondering if this is the right approach to writing a thread-safe queue in
I have a class like the below and am wondering, will this be thread-safe
I've stumbled upon an interesting situation when object could release itself and was wondering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.