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 9021675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:18:42+00:00 2026-06-16T05:18:42+00:00

I’m trying to wrap a C API in Objective-C but am getting EXC_BAD_ACCESS in

  • 0

I’m trying to wrap a C API in Objective-C but am getting EXC_BAD_ACCESS in objc_release.

Field.h:

#ifdef __cplusplus
extern "C" {
#endif

struct sc_field
{
    char *title;
};

typedef struct sc_field sc_field_t;

sc_field_t* sc_create_field();
void sc_destroy_field(sc_field_t *field);
const char* sc_get_title(const sc_field_t *field);
void sc_set_title(sc_field_t *field, const char *title);

#ifdef __cplusplus
}

#include <string>

class Field
{
public:
    explicit Field();
    virtual ~Field();

    std::string title() const;
    void setTitle(const std::string &title);

private:
    class Private;
    Private *d;
};

Field.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Field.h"

sc_field_t* sc_create_field()
{
    sc_field_t *field = (sc_field_t*)malloc(sizeof(sc_field_t));
    field->title = (char*)calloc(1, sizeof(char));
    return field;
}

void sc_destroy_field(sc_field_t *field)
{
    if (field)
    {
        free(field->title);
        free(field);
    }
}

const char* sc_get_title(const sc_field_t *field)
{
    if (!field)
    {
        fprintf(stderr, "%s: fatal error, field is NULL\n", __FUNCTION__);
        return NULL;
    }

    return field->title;
}

void sc_set_title(sc_field_t *field, const char *title)
{
    if (!field)
    {
        fprintf(stderr, "%s: fatal error, field is NULL\n", __FUNCTION__);
        return;
    }

    field->title = (char*)realloc(field->title, sizeof(char) * sizeof(title));
    strcpy(field->title, title);
}

SCField.h:

#import "Field.h"
#import <Foundation/Foundation.h>

@interface SCField : NSObject
{
@private
    sc_field_t *field;
}

@property (nonatomic, copy) NSString *title;

- (id)init;
- (id)initWithTitle:(NSString *)title;
- (void)dealloc;

@end

SCField.m:

#import "SCField.h"

@implementation SCField

@synthesize title;

- (id)init
{
    self = [super init];
    if (self)
    {
        field = sc_create_field();
    }

    return self;
}

- (id)initWithTitle:(NSString *)aTitle
{
    self = [self init];
    if (self)
    {
        [self setTitle:aTitle];
    }

    return self;
}

- (void)dealloc
{
    sc_destroy_field(field);
    [super dealloc];
}

- (NSString *)title
{
    return [NSString stringWithUTF8String:sc_get_title(field)];
}

- (void)setTitle:(NSString *)aTitle
{
    sc_set_title(field, [aTitle UTF8String]);
}

@end

Test:

SCField *field = [[SCField alloc] initWithTitle:@"Hello world!"];
NSLog(@"%@\n", [field title]);
[field release];

The code seems to work fine until I release my SCField instance. If I remove the title and setTitle: implementations as if I were storing an NSString* directly in the SCField the code works fine (I tried assign instead of copy in the @property, too – no luck). What am I doing wrong?

  • 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-06-16T05:18:43+00:00Added an answer on June 16, 2026 at 5:18 am

    I think the issue is that calling UTF8String will give you memory that will be autoreleased (according to the documentation).

    Rather than using realloc() in sc_set_title(), just malloc() a new buffer and copy it. Also, your realloc() parameters are incorrect anyway, so it probably ends up being a no-op. You need to allocate strlen(title) + 1; sizeof won’t do anything for you since you’re just asking the compiler to give you 1 (sizeof(char)) * 4 bytes (sizeof(title) – a pointer).

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to select an H1 element which is the second-child in its group

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.