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

  • SEARCH
  • Home
  • 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 8092321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:15:34+00:00 2026-06-05T20:15:34+00:00

I have a sample project here on github where I created a c++ wrapper

  • 0

I have a sample project here on github where I created a c++ wrapper class for an external C++ library that I want to use in Objective-C.

I don’t understand why my returned pointers are sometimes correct and sometimes wrong. Here’s sample output:

Test Data = 43343008
In Compress 43343008
Returned Value = 43343008
Casted Value = 43343008

Test Data = 2239023
In Compress 2239023
Returned Value = 2239023
Casted Value = 2239023

Test Data = 29459973
In Compress 29459973
Returned Value = 29459973
Casted Value = l.remote

Test Data = 64019670
In Compress 64019670
Returned Value = 
Casted Value = stem.syslog.master

In the above output you can see that the 1st and 2nd click of the button outputs the results I was expecting. In each of the other clicks either the returned value or casted value are invalid. I’m assuming this is because my pointer is pointing to an address I wasn’t expecting. when running the app multiple times, any button click could be right or wrong.

I also tried with a single thread but experienced similar results.

The complete code is on github but here are the important bits.

ViewController.m

#import "ViewController.h"

extern const char * CompressCodeData(const char * strToCompress);

@implementation ViewController

...

// IBAction on the button
- (IBAction)testNow:(id)sender 
{
    [self performSelectorInBackground:@selector(analyze) withObject:nil];
}

- (void)analyze
{
    @synchronized(self) {

        const char *testData = [[NSString stringWithFormat:@"%d", 
                      (int)(arc4random() % 100000000)] UTF8String];
        NSLog(@"Test Data = %s", testData);

        const char *compressed = CompressCodeData(testData);
        NSLog(@"Returned Value = %s", compressed);

        NSString *casted = [NSString stringWithCString:compressed
                                          encoding:NSASCIIStringEncoding];
        NSLog(@"Casted Value = %@\n\n", casted);

    }
}

@end

SampleWrapper.cpp

#include <iostream>
#include <string.h>
#include <CoreFoundation/CoreFoundation.h>

using namespace std;

extern "C" 
{
    extern void NSLog(CFStringRef format, ...); 

    /**
     * This function simply wraps a library function so that 
     * it can be used in objective-c.
     */
    const char * CompressCodeData(const char * strToCompress) 
    {
        const string s(strToCompress);

        // Omitted call to static method in c++ library
        // to simplify this test case.

        //const char *result = SomeStaticLibraryFunction(s);
        const char *result = s.c_str();

        NSLog(CFSTR("In Compress %s"), result);
        return result;
    }

}
  • 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-05T20:15:34+00:00Added an answer on June 5, 2026 at 8:15 pm

    You are returning a pointer to at object that has been deallocated.

    const string s(strToCompress);
    …
    const char *result = s.c_str();
    
    NSLog(CFSTR("In Compress %s"), result);
    return result;
    

    s does not exist after CompressCodeData() function is over, so the pointer to it’s internal memory is invalid.


    You could allocate a chunk of memory to hold the response, but it would be up to the caller to release it.

    char *compressed = CompressCodeData(testData);
    NSLog(@"Returned Value = %s", compressed);
    
    NSString *casted = [NSString stringWithCString:compressed
                                          encoding:NSASCIIStringEncoding];
    free(compressed);
    NSLog(@"Casted Value = %@\n\n", casted);
    
    …
    
    const char * CompressCodeData(const char * strToCompress)
    …
    char *result = strdup(s.c_str());
    

    Another solution is to pass in the memory to store the data into.

    char compressed[2048]; // Or whatever!
    CompressCodeData(testData, compressed, sizeof(compressed));
    NSLog(@"Returned Value = %s", compressed);
    
    NSString *casted = [NSString stringWithCString:compressed
                                          encoding:NSASCIIStringEncoding];
    NSLog(@"Casted Value = %@\n\n", casted);
    
    …
    
    void CompressCodeData(const char * strToCompress, char *result, size_t size)
    …
    s.copy(result, size - 1);
    result[s.length() < size ? s.length() : size-1] = '\0';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bare-bones sample project here: http://dl.dropbox.com/u/7834263/ExpandingCells.zip In this project, a UITableView has
I've started working on a little ruby project that will have sample implementations of
I have opened a sample ASP.NET MVC project. In HomeController I have created a
A project on GitHub that I have a fork of has a new pull
I have created a project on GitHub so I can learn how to optimize
I have a simple java project (adapted from the example here ), which is
I have the sample address book project loaded up and it is failing to
Visual Studio 2008. I downloaded a sample project and unzipped it. I have to
Im building a sample pad type instrument for a project, i have four channels
I have a simple Scala project that runs without any problems inside Eclipse, however,

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.